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

private static byte[] bitmap2Bytes(Bitmap bm) {
    try {// w w w  . j  a  v a  2s  .  com
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bytes = baos.toByteArray();
        baos.close();
        return bytes;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void saveThumbnail(String filePath, Bitmap bitmap) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();

    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

    // you can create a new file name "test.jpg" in sdcard folder.

    File f = new File(filePath);

    try {/*from  ww  w . j  a  va  2  s  .c  om*/

        f.createNewFile();
        // write the bytes in file
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Converts and Android bitmap file to an array of raw bytes that are ready to be sent over bluetooth,
 * wifi, usb, etc.//from ww  w.j a v  a 2  s  . c o m
 * 
 * @param bitmap The bitmap to translate
 * @param format The format of the bitmap
 * @return The raw byte representation of the bitmap
 */
public static byte[] bitmapToRawBytes(Bitmap bitmap, CompressFormat format) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(format, 100, baos);
    byte[] result = baos.toByteArray();
    return result;
}

From source file:Main.java

/**
 * Converts a bitmap into an inputstream
 * @param image the bitmap/*from   w w w .ja  v  a2s  .  com*/
 * @return the inputstream
 */
public static InputStream convertBitmap(Bitmap image) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    image.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
    byte[] bitmapdata = bos.toByteArray();
    return new ByteArrayInputStream(bitmapdata);
}

From source file:Main.java

public static void bitmapToFile(Bitmap bitmap, File file) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
    byte[] bitmapdata = bos.toByteArray();
    FileOutputStream fos;//from  w  ww.jav a 2s  .c  o m
    try {
        fos = new FileOutputStream(file);
        fos.write(bitmapdata);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * TODO doc/*from  www. ja v a 2  s  .  c o  m*/
 *
 * @param photoBitmap
 * @return
 */
public static byte[] toByteArray(Bitmap photoBitmap) throws IOException {
    ByteArrayOutputStream blob = new ByteArrayOutputStream();
    photoBitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, blob);
    byte[] photoByteArray = blob.toByteArray();
    blob.close();
    return photoByteArray;
}

From source file:Main.java

/**
 * Decodes a bitmap to its corresponding representation in bytes, in the format sent as parameter
 *
 * @param bitmap Bitmap to decode//from w w w . j av  a 2s.  co  m
 * @param format Format with which this image is going to be decoded
 * @return A byte array containing the bytes of the decoded bitmap
 */
public static byte[] bitmapToByteArray(Bitmap bitmap, Bitmap.CompressFormat format) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(format, 100, stream);
    return stream.toByteArray();
}

From source file:Main.java

public static byte[] compressToBytes(Bitmap bitmap, int quality) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(65536);
    bitmap.compress(CompressFormat.JPEG, quality, baos);
    return baos.toByteArray();
}

From source file:Main.java

/**
 * Convert a Bitmap into a byte Array/*from  w  w  w  .ja  v  a 2s  .  c om*/
 * @param bitmap
 * @return
 */
public static byte[] convertBitmapToByte(Bitmap bitmap) {
    try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        return byteArray;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static byte[] Bitmap2Bytes(Bitmap bm) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (bm != null) {

        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

    }//w w w  .  j ava  2  s.  c  om

    return baos.toByteArray();
}