Android Bitmap to Byte Array Convert bitmap2Bytes(Bitmap bm, Bitmap.CompressFormat format)

Here you can find the source of bitmap2Bytes(Bitmap bm, Bitmap.CompressFormat format)

Description

bitmap Bytes

Declaration

public static byte[] bitmap2Bytes(Bitmap bm,
        Bitmap.CompressFormat format) 

Method Source Code



import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.graphics.Matrix;

import java.io.ByteArrayOutputStream;

public class Main {

    public static byte[] bitmap2Bytes(Bitmap bm,
            Bitmap.CompressFormat format) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(format, 100, baos);//from w  ww. j av  a  2 s.  c  o m
        return baos.toByteArray();
    }

    public static Bitmap compress(String filePath, int width, int height) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);

        int bmpWidth = options.outWidth;
        int bmpHeight = options.outHeight;

        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(filePath, options);

        float scaleWidth = bmpWidth;
        float scaleHeight = bmpHeight;

        
        if (bmpWidth > width) {
            scaleWidth = width / scaleWidth;
        } else {
            scaleWidth = 1;
        }

        
        if (bmpHeight > height) {
            scaleHeight = height / scaleHeight;
        } else {
            scaleHeight = 1;
        }

        Matrix matrix = new Matrix();

        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth,
                bmpHeight, matrix, false);

        
        
        if (bitmap != resizeBitmap) {
            bitmap.recycle();
        }
        return resizeBitmap;
    }
}

Related

  1. convertBitmapToByte(Bitmap bitmap)
  2. getBitmapData(Bitmap bmp)
  3. bitmapTobyte(Bitmap bmp)
  4. Bitmap2Bytes(Bitmap bm)
  5. bitmap2Bytes(Bitmap bitmap, Bitmap.CompressFormat mCompressFormat, final boolean needRecycle)
  6. bmpToByteArray(final Bitmap bmp, final boolean needRecycle)
  7. convertBitmapToByteArray(Bitmap bitmap)
  8. bitmap2byte(Bitmap bitmap)
  9. putImagePath(String imagePath)