Android Development - Bitmap,byte[],Drawable mutual transformation

In the development of these days, we have encountered a problem that needs to convert the byte[] data obtained by Camera into Bitmap. Now we take a note.
  • 1

In the surface view, the Camera image is obtained by combining Camera. This code is a part of my program

    @Override
    public void onPreviewFrame(final byte[] data, Camera camera) {
        camera.setPreviewCallback(null);

        if (mCamera == null)
            return;

        Camera.Parameters parameters = camera.getParameters();
        int width = parameters.getPreviewSize().width;
        int height = parameters.getPreviewSize().height;

        YuvImage yuv = new YuvImage(data, parameters.getPreviewFormat(), width, height, null);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        yuv.compressToJpeg(new Rect(0, 0, width, height), 50, out);

        byte[] bytes = out.toByteArray();
        final Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

        this.mCamera.setPreviewCallback(this);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

Now I will explain how the three can be converted to each other:

Preparation: concept description

1. Drawable is a paintable object, which may be a bitmap, a shape drawable, or a layer drawable. According to the requirements of drawing, we create corresponding paintable objects
2. Canvas, drawing destination area, for drawing
3. Bitmap, used for graph processing
4. Matrix matrix

1. Get Bitmap from resources

 Resources res = getResources();
 Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);
  • 1
  • 2

2.Bitmap → byte[]

  public byte[] Bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
  }
  • 1
  • 2
  • 3
  • 4
  • 5

3.byte[] → Bitmap

public Bitmap Bytes2Bimap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
            return null;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4.Bitmap scaling

public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidth = ((float) width / w);
        float scaleHeight = ((float) height / h);
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
        return newbmp;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

5. Convert Drawable to Bitmap

public static Bitmap drawableToBitmap(Drawable drawable) {
        // Take the length and width of drawable
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();

        // Take the color format of drawable
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        // Establish corresponding bitmap
        Bitmap bitmap = Bitmap.createBitmap(w, h, config);
        // Create a canvas corresponding to bitmap
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        // Draw drawable content to canvas
        drawable.draw(canvas);
        return bitmap;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

6. Get the rounded corner picture

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, w, h);
        final RectF rectF = new RectF(rect);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

7. Convert bitmap to Drawable

 Bitmap bm=xxx; //xxx according to your situation
 BitmapDrawable bd= new BitmapDrawable(getResource(), bm); 
 //Because BtimapDrawable is a subclass of Drawable, you can use bd object directly.
  • 1
  • 2
  • 3

8.Drawable scaling

public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        // drawable to bitmap
        Bitmap oldbmp = drawableToBitmap(drawable);
        // Creating Matrix objects for manipulating pictures
        Matrix matrix = new Matrix();
        // Calculate scale
        float sx = ((float) w / width);
        float sy = ((float) h / height);
        // Set scaling
        matrix.postScale(sx, sy);
        // Build a new bitmap, the content of which is the zoomed image of the original bitmap
       Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,matrix, true);
        return new BitmapDrawable(newbmp);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
https://blog.csdn.net/xidiancoder/article/details/51649465

Posted on Mon, 30 Mar 2020 07:59:37 -0700 by c4onastick