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 String bitmaptoString(Bitmap bitmap) {
    String string = null;/*from   w  w w. j  ava2s.co m*/
    ByteArrayOutputStream bStream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 50, bStream);
    byte[] bytes = bStream.toByteArray();
    string = Base64.encodeToString(bytes, Base64.DEFAULT);
    return string;
}

From source file:Main.java

public static Bitmap codec(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 String bitmapToBase64(Bitmap bitmap) {

    try {/*from  ww  w.  j av a2 s  . c om*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        baos.close();
        byte[] buffer = baos.toByteArray();
        String photo = Base64.encodeToString(buffer, 0, buffer.length, Base64.DEFAULT);
        return photo;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * convert Bitmap to InputStream/*w w  w. j a  v  a  2s .  co  m*/
 */
public static InputStream bitmapToStream(Bitmap bitmap, Bitmap.CompressFormat compressFormat) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(compressFormat, 100, bos);
    return new ByteArrayInputStream(bos.toByteArray());
}

From source file:Main.java

public static Bitmap codec(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 void writeBitmapToFile(Bitmap bitmap, File file) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
    byte[] bitmapData = bos.toByteArray();

    //write the bytes in file
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(bitmapData);// ww w . j a v a  2s . c  o m
    fos.flush();
    fos.close();
}

From source file:Main.java

public static InputStream bitmap2InputStream(Bitmap bm, Bitmap.CompressFormat format, int quality) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(format, quality, baos);
    InputStream is = new ByteArrayInputStream(baos.toByteArray());
    return is;//w ww.ja v a 2  s  .c o  m
}

From source file:Main.java

public static byte[] getBitmapToBytes(Bitmap bitmap) {
    if (bitmap != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        return baos.toByteArray();
    }/* www.  j  a  va2  s.  c  om*/
    return null;
}

From source file:Main.java

public static String encodeBitmap(Bitmap bitmap, Bitmap.CompressFormat compressFormat) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(compressFormat, 100, baos);
    byte[] bytes = baos.toByteArray();
    byte[] encodedImage = Base64.encode(bytes, Base64.DEFAULT);
    return new String(encodedImage);
}

From source file:Main.java

/**
 * Bitmap -> byte//from   w w  w .java 2 s . co m
 * 
 * @param bitmap
 * @return
 */
public static byte[] bitmapToByte(Bitmap bitmap) {
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        bitmap.recycle();
        bitmap = null;
        byte[] array = out.toByteArray();
        return array;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}