Example usage for android.graphics Bitmap compress

List of usage examples for android.graphics Bitmap compress

Introduction

In this page you can find the example usage for android.graphics Bitmap compress.

Prototype

@WorkerThread
public boolean compress(CompressFormat format, int quality, OutputStream stream) 

Source Link

Document

Write a compressed version of the bitmap to the specified outputstream.

Usage

From source file:Main.java

public static byte[] getByteFromBitmap(Bitmap bitmap, Bitmap.CompressFormat compressFormat) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(compressFormat, 100, baos);

    return baos.toByteArray();

}

From source file:Main.java

public static String openAsBase64(String imgPath) {
    Bitmap photo = BitmapFactory.decodeFile(imgPath);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.JPEG, 85, baos);
    byte[] b = baos.toByteArray();
    photo.recycle();/*from   w w  w.  jav  a 2 s  .c  o m*/
    return Base64.encodeToString(b, Base64.DEFAULT);

}

From source file:Main.java

private static Bitmap compassBitmap(Bitmap src, Bitmap.CompressFormat format, int quality) {

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    src.compress(format, quality, os);

    byte[] array = os.toByteArray();
    return BitmapFactory.decodeByteArray(array, 0, array.length);
}

From source file:Main.java

public static byte[] compressBmpToBytes(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int options = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);

    return baos.toByteArray();
}

From source file:Main.java

public static byte[] bmpToByteArray(Bitmap bitmap, boolean flag) {
    ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
    bitmap.compress(android.graphics.Bitmap.CompressFormat.JPEG, 100, bytearrayoutputstream);
    if (flag)//from  www  . j ava2 s.c om
        bitmap.recycle();
    byte abyte0[] = bytearrayoutputstream.toByteArray();
    try {
        bytearrayoutputstream.close();
    } catch (Exception exception) {
        exception.printStackTrace();
    }
    return abyte0;
}

From source file:Main.java

/**
 * Store bitmap into specified image path
 *
 * @param bitmap//from w  ww. j av  a 2s.c o  m
 * @param outPath
 * @throws FileNotFoundException
 */
public static void storeImage(Bitmap bitmap, String outPath) throws FileNotFoundException {
    FileOutputStream os = new FileOutputStream(outPath);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
}

From source file:Main.java

public static String bitmapChangeString(Bitmap bitmap) {
    if (bitmap != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        String str = new String(Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT));
        return str;
    }//from www  .j  av  a2s. c  o m
    return null;
}

From source file:Main.java

public static byte[] decodeBitmapToBytes(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (bitmap != null) {
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    } else {/*from  w ww .ja  va  2s  . c  o m*/
        Log.e(TAG, "bitmap is null");
        return null;
    }
}

From source file:Main.java

public static File saveBitmap2file(Bitmap bmp) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    File imageFile = null;//  w  w w  . j  a va 2s . c  o m
    try {
        imageFile = File.createTempFile("tempImage" + System.currentTimeMillis(), ".png");
    } catch (IOException e) {
        e.printStackTrace();
    }

    FileOutputStream fstream = null;
    try {
        fstream = new FileOutputStream(imageFile);
        BufferedOutputStream bStream = new BufferedOutputStream(fstream);
        bStream.write(byteArray);
        if (bStream != null) {
            bStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return imageFile;

}

From source file:Main.java

public static byte[] Bitmap2Bytes(Bitmap bm) {
    if (bm == null)
        return null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}