SoFunction
Updated on 2025-03-01

Detailed explanation of Android BitmapUtils tool class usage

This article shares the specific code of Android BitmapUtils tool class for your reference. The specific content is as follows

public final class BitmapUtils {
  public static final String TAG = "BitmapUtil";
  private static int sShotScreenWidth = 480;
  private static int sShotScreenHeight = 720;
  private static int sShotScreenSize = sShotScreenWidth * sShotScreenHeight;

  @SuppressLint("StaticFieldLeak")
  private static Context mContext;
  @SuppressLint("StaticFieldLeak")
  private static Activity mActivity;

  public void init(Context context,Activity ac) {
    mContext=context;
    mActivity=ac;

    DisplayMetrics dm = new DisplayMetrics();
    ().getDefaultDisplay().getMetrics(dm);
    //Get screen resolution    sShotScreenWidth = ;
    sShotScreenHeight = ;
    sShotScreenSize = sShotScreenWidth * sShotScreenHeight;
  }

  /**
    * Image synthesis
    *
    * @param bitmap Bitmap 1
    * @param mark Bitmap 2
    * @return Bitmap
    */
  public static Bitmap createBitmap(Bitmap bitmap, Bitmap mark) {
    int w = ();
    int h = ();
    int mW = ();
    int mH = ();
    Bitmap newbitmap = (w, h, Config.ARGB_8888);// Create a bitmap with the same length and width
    Canvas cv = new Canvas(newbitmap);
    (bitmap, 0, 0, null);// Start drawing bitmap at 0, 0 coordinates    (mark, w - mW , h - mH , null);// Draw a watermark mark in the lower right corner    (Canvas.ALL_SAVE_FLAG);// Save    ();// Storage    return newbitmap;
  }

  /**
    * Zoom in and out of picture
    * @param bitmap
    * @param w New width
    * @param h New heights
    * @return Bitmap
    */
  public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
    int width = ();
    int height = ();
    Matrix matrix = new Matrix();
    float scaleWidht = ((float) w / width);
    float scaleHeight = ((float) h / height);
    (scaleWidht, scaleHeight);
    return (bitmap, 0, 0, width, height, matrix, true);
  }

  /**
    * Rotate the picture
    * @param bitmap The picture to be rotated
    * @param angle Rotation angle
    * @return bitmap
    */
  public static Bitmap rotate(Bitmap bitmap,int angle) {
    Matrix matrix = new Matrix();
    (angle);
    return (bitmap, 0, 0, (),
        (), matrix, true);
  }

  /**
    * Circular picture
    *@param source Bitmap
    * @param strokeWidth Crop range 0Indicates maximum
    * @param bl Is it necessary to stroke
    * @param bl Brush thickness
    * @param bl Color code
    * @return bitmap
    */
  public static Bitmap createCircleBitmap(Bitmap source, int strokeWidth, boolean bl,int edge,int color) {

    int diameter = () < () ? () : ();
    Bitmap target = (diameter, diameter, Config.ARGB_8888);
    Canvas canvas = new Canvas(target);//Create a canvas
    Paint paint = new Paint();
    (true);
    (diameter / 2, diameter / 2, diameter / 2, paint);//Draw a circle    (new PorterDuffXfermode(.SRC_IN));//Take the intersecting crop    (source, strokeWidth, strokeWidth, paint);
    if(bl) {
      if (color == 0) color = 0xFFFEA248;//Default orange      (color);
      ();//Stroke      (edge);
      (diameter / 2, diameter / 2, diameter / 2, paint);
    }
    return target;
  }

  /**
    * Round corner picture
    * @param bitmap
    * @param rx radius in x direction
    * @param ry Rounded corner radius in the y direction
    * @param bl Does the stroke need to be
    * @param bl Brush thickness
    * @param bl Color code
    * @return bitmap
    */
  public static Bitmap createCornerBitmap(Bitmap bitmap,int rx,int ry,boolean bl,int edge,int color) {
    Bitmap output = ((), (), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);//Create a canvas
    Paint paint = new Paint();
    (true);
    Rect rect = new Rect(0, 0, (), ());
    RectF rectF = new RectF(rect);
    (rectF, rx, ry, paint);//Draw rounded rectangle    (new PorterDuffXfermode(Mode.SRC_IN));//Take the intersecting crop    (bitmap, rect, rect, paint);
    if(bl) {
      if (color == 0) color = 0xFFFEA248;//Default orange      (color);
      (color);
      ();//Stroke      (edge);
      (rectF, rx, ry, paint);
    }
    return output;
  }

  /**
    * Crop the picture to scale
    * @param bitmap
    * @param wScale Crop width 0~100%
    * @param hScale Crop height 0~100%
    * @return bitmap
    */
  public static Bitmap cropBitmap(Bitmap bitmap, float wScale, float hScale) {
    int w = ();
    int h = ();

    int wh = (int) (w * wScale);
    int hw = (int) (h * hScale);

    int retX = (int) (w * (1 - wScale) / 2);
    int retY = (int) (h * (1 - hScale) / 2);

    return (bitmap, retX, retY, wh, hw, null, false);
  }

  /**
    * How to get a picture with reflection
    * @param bitmap
    * @param region Reflection area 0.1~1
    * @return bitmap
    */
  public static Bitmap createReflectionBitmap(Bitmap bitmap,float region) {

    int width = ();
    int height = ();
    Matrix matrix = new Matrix();
    (1, -1);//Mirror Scaling    Bitmap reflectionBitmap = (
                         bitmap,0
                        , (int)(height*(1-region))//Which point starts drawing                        , width
                        ,(int) (height*region)//How high is the drawing                        , matrix, false);

    Bitmap reflectionWithBitmap = (width,height+ (int) (height*region),
                              Config.ARGB_8888);
    Canvas canvas = new Canvas(reflectionWithBitmap);
    (bitmap, 0, 0, null);
    (reflectionBitmap, 0, height , null);

    LinearGradient shader = new LinearGradient(0, (), 0,
                        ()
                        , 0x70ffffff, 0x00ffffff, );

    Paint paint = new Paint();
    (shader);
    (new PorterDuffXfermode(Mode.DST_IN));//Take two layers to draw the intersection.  Show the lower layer.    (0, height, width, () , paint);
    return reflectionWithBitmap;
  }

  /**
    * Picture quality compression
    * @param bitmap
    * @param many Percentage
    * @return
    */
  public static Bitmap compressBitmap(Bitmap bitmap, float many){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    (, (int)many*100, baos);
    ByteArrayInputStream isBm = new ByteArrayInputStream(());
    return (isBm, null, null);
  }

  /**
    * Advanced picture quality compression
    *@param bitmap
    * @param maxSize compressed size, unit kb
    */
  public static Bitmap imageZoom(Bitmap bitmap, double maxSize) {
    // Put the bitmap into the array, in order to get the size of the bitmap (the original file actually read is large)    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // Format, quality, output stream    (, 70, baos);
    byte[] b = ();
    // Change bytes to KB    double mid =  / 1024;
    // Get the bitmap size how many times the maximum size is allowed    double i = mid / maxSize;
    // Determine whether the bitmap occupies more than the maximum allowable space. If it is greater than, compress, and if it is less than, it does not compress    doRecycledIfNot(bitmap);
    if (i > 1) {
      // Scaling the picture Use square root here to compress the broadband and height to the corresponding square root times      // (Keep the width and height unchanged, and the maximum space occupied is also reached after scaling)      return scaleWithWH(bitmap,() / (i),
              () / (i));
    }
    return null;
  }

  /***
    * Image zoom
    *@param bitmap
    * @param w New width
    * @param h New heights
    * @return Bitmap
    */
  public static Bitmap scaleWithWH(Bitmap bitmap, double w, double h) {
    if (w == 0 || h == 0 || bitmap == null) {
      return bitmap;
    } else {
      int width = ();
      int height = ();

      Matrix matrix = new Matrix();
      float scaleWidth = (float) (w / width);
      float scaleHeight = (float) (h / height);
      
      (scaleWidth, scaleHeight);
      return (bitmap, 0, 0, width, height,
          matrix, true);
    }
  }

  /**
    * YUV video streaming format to bitmap
    * @param data YUV video streaming format
    * @return width Set width
    * @return width Set height
    */
  public static Bitmap getBitmap(byte[] data, int width, int height) {
    Bitmap bitmap;
    YuvImage yuvimage = new YuvImage(data, ImageFormat.NV21, width, height, null);
    //data is provided by onPreviewFrame parameter    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    (new Rect(0, 0, (), ()), 100, baos);//
    // 80--JPG picture quality [0-100], 100 highest    byte[] rawImage = ();
     options = new ();
    SoftReference<Bitmap> softRef = new SoftReference<Bitmap>((rawImage, 0, rawImage
        .length, options));
    bitmap = ();
    return bitmap;
  }

  /**
    * Image path to bitmap
    * @param file The absolute path to the image
    * @return bitmap
    */
  public static Bitmap getAssetImage(String file) {
    Bitmap bitmap = null;
    AssetManager am = ();
    try {
      InputStream is = (file);
      bitmap = (is);
      ();
    } catch (IOException e) {
      ();
    }
    return bitmap;
  }

  /**
    * bitmap saved to the specified path
    * @param file The absolute path to the image
    * @param file bitmap
    * @return bitmap
    */
  public static boolean saveFile(String file, Bitmap bmp) {
    if((file) || bmp == null) return false;
    
    File f = new File(file);
    if (()) {
      ();
    }else {
      File p = ();
      if(!()) {
        ();
      }
    }
    try {
      FileOutputStream out = new FileOutputStream(f);
      (, 100, out);
      ();
      ();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      ();
      return false;
    }
    return true;
  }

  /**
    * Recycle an unrecycled Bitmap
    *@param bitmap
    */
  public static void doRecycledIfNot(Bitmap bitmap) {
    if (!()) {
      ();
    }
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.