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 void saveThePicture(Bitmap bitmap, String p) throws Exception {
    File file = new File(p);

    FileOutputStream fos = new FileOutputStream(file);
    // bitmap.//  w w w .ja  v  a 2s.  co  m
    if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos)) {
        fos.flush();
        fos.close();
    }

}

From source file:Main.java

public static byte[] convertBitmapToByteArray(Bitmap bitmap) {
    if (bitmap != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // TODO see if it raise problems
        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
        return baos.toByteArray();
    } else {/*ww  w. j a  v  a2s  . co m*/
        return null;
    }
}

From source file:Main.java

public static boolean bitmap2File(Bitmap bitmap, File imageFile) {
    OutputStream os;//  www. j  av  a2s. c om
    try {
        os = new FileOutputStream(imageFile);
        boolean isOK = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
        os.flush();
        os.close();
        return isOK;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

/**
 * Bitmap into compressed PNG/*from w  ww  . jav a 2  s  . c o m*/
 *
 * @param image the Bitmap
 * @return byte array of PNG
 */
public static byte[] bitmapToPng(final Bitmap image) {
    if (image == null)
        return null;

    ByteArrayOutputStream ba = new ByteArrayOutputStream();

    if (image.compress(CompressFormat.PNG, 100, ba))
        return ba.toByteArray();
    else
        return null;
}

From source file:Main.java

/**
 * convert Bitmap to byte array//from   w  ww .j a v a 2s .  c o m
 *
 * @param b
 * @return
 */
public static byte[] bitmapToByte(Bitmap b) {
    if (b == null) {
        return null;
    }

    ByteArrayOutputStream o = new ByteArrayOutputStream();
    b.compress(Bitmap.CompressFormat.PNG, 100, o);
    return o.toByteArray();
}

From source file:Main.java

public static byte[] bitmapToBytes(Bitmap source) {
    if (source == null) {
        return null;
    } else {/*from w ww . ja v  a 2  s  .com*/
        ByteArrayOutputStream returnByte = new ByteArrayOutputStream();
        source.compress(Bitmap.CompressFormat.JPEG, DEFAULT_QUALITY, returnByte);
        return returnByte.toByteArray();
    }
}

From source file:Main.java

/**
 * Convert a bitmap to a byte array// w w  w .  j  a  v a2s  .c  om
 * @param path the path to the picture on the phone
 * @return the image in a byte array
 */
public static byte[] picToByte(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 10, baos);
    byte[] ba = baos.toByteArray();
    try {
        baos.close();
    } catch (IOException e) {
        Log.e("Error", "Error closing output stream" + e.getMessage());
        return null;
    }
    return ba;

}

From source file:Main.java

public static byte[] Bitmap2Bytes(Bitmap bm, Bitmap.CompressFormat format) {
    if (bm == null) {
        return null;
    }//from www  .  j  a v  a2s.  co m
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(format, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

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

    return null;
}

From source file:Main.java

/**
 * Converts image to byte array.//from  w  ww.ja  v a2s . c  o m
 * <br /><br />
 * <p/>
 * <b>ATTENTION</b>: image conversion is done by compression, witch is very long running operation.
 *
 * @param bmp     original bitmap
 * @param format  compress format
 * @param quality compress quality
 * @return compress image as byte array
 * @see android.graphics.Bitmap#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream)
 */
public static byte[] bmpToByte(Bitmap bmp, Bitmap.CompressFormat format, int quality) {
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(format, quality, stream);

    try {
        stream.flush();
        byte[] data = stream.toByteArray();
        stream.close();

        return data;
    } catch (IOException e) {
        return null;
    }
}