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

static public byte[] getJPEGRepresentationFromBitmap(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream);
    return outputStream.toByteArray();
}

From source file:Main.java

private static void saveBitmap2TempFile(File file, Bitmap bitmap) {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 70 /* ignored for PNG */, bos);
    byte[] bitmapdata = bos.toByteArray();

    // write the bytes in file
    FileOutputStream fos = null;//from  w  w  w  .j  a  v  a 2 s  . c  o m
    try {

        fos = new FileOutputStream(file);

        fos.write(bitmapdata);
        fos.flush();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {

        try {
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

From source file:Main.java

public static byte[] getBytes(Bitmap bitmap) {
    if (bitmap == null)
        return null;
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
    return stream.toByteArray();
}

From source file:Main.java

public static Bitmap compressBmpFromBmp(Bitmap image) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int options = 100;
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    while (baos.toByteArray().length / 1024 > 100) {
        baos.reset();//from  w ww. ja v a  2s  .c  o m
        options -= 10;
        image.compress(Bitmap.CompressFormat.JPEG, options, baos);
    }
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
    return bitmap;
}

From source file:Main.java

private static byte[] createByteArrayFromBitmap(Bitmap bitmap) {
    Log.e(LOG_TAG, "creating asset");
    final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
    Log.e(LOG_TAG, "length : " + Integer.toString(byteStream.toByteArray().length));
    return byteStream.toByteArray();
}

From source file:Main.java

public static byte[] Bitmap2Bytes(Bitmap bm) {
    if (bm == null) {
        return null;
    }//from  w  w w.ja v  a 2 s.  c om
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

public static void compressIt(String inputFile, String outputFile) {
    try {// w ww.  j av  a  2  s  .co m
        Bitmap bmp = BitmapFactory.decodeFile(inputFile);
        FileOutputStream out = new FileOutputStream(new File(outputFile));
        bmp.compress(Bitmap.CompressFormat.PNG, 50, out); //100-best quality
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static byte[] bitmap2Bytes(Bitmap bm) {
    if (bm == null) {
        return null;
    }/* w  w  w  . j  a  v  a  2 s .  c o m*/
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] toByteArray(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    return stream.toByteArray();
}

From source file:Main.java

public static byte[] bitmapToBytes(Bitmap bitmap) {

    if (bitmap == null)
        return null;
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
    return os.toByteArray();
}