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[] bitmapToByte(String path) {
    File file = new File(path);
    if (!file.exists()) {
        return null;
    }//from w  w w. j  a  v  a 2  s .  c  om
    Bitmap bitmap = BitmapFactory.decodeFile(path);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
    return bos.toByteArray();
}

From source file:Main.java

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

From source file:Main.java

public static String bitmapToString(Bitmap bitmap) {
    String bitmapString = null;//from   ww  w.j  a  v  a2s .c o m
    ByteArrayOutputStream bStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
    byte[] bytes = bStream.toByteArray();
    bitmapString = Base64.encodeToString(bytes, Base64.DEFAULT);
    return bitmapString;
}

From source file:Main.java

/**
 * Write the given bitmap into the given file. JPEG is used as the compression format with
 * quality set// w w  w. j  a v  a 2  s .  com
 * to 100.
 *
 * @param bm   The bitmap.
 * @param file The file to write the bitmap into.
 */
public static void writeBitmapToFile(Bitmap bm, File file, int quality) throws IOException {

    FileOutputStream fos = new FileOutputStream(file);
    bm.compress(Bitmap.CompressFormat.JPEG, quality, fos);
    fos.flush();
    fos.close();
}

From source file:Main.java

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();//from  w w w.  j  a  va2s  .c om
    }
    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 80, output);
    if (needRecycle) {
        bmp.recycle();//  ww  w . j av  a2  s.  c om
    }
    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static boolean bitmapToFile(Bitmap bmp, String fileName) {
    try {//from w w  w.ja v  a  2  s  . c om
        FileOutputStream out = new FileOutputStream(fileName);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        File iconFile = new File(fileName);
        iconFile.setReadable(true, false);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

public static byte[] convertToBytes(Bitmap bitmap, CompressFormat format, int quality) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(format, quality, stream);
    byte[] data = stream.toByteArray();
    try {/* w w  w. ja v  a2s.c  o  m*/
        stream.close();
    } catch (Exception e) {
        // nothing
    }
    return data;
}

From source file:Main.java

/**
 * Bitmap to Data//from   w  ww.java 2  s  .  co  m
 * @param src      Bitmap
 * @param format   Bitmap.CompressFormat
 * @param quality   int
 * @return         byte[]
 */
public static byte[] chngBmpToData(Bitmap src, Bitmap.CompressFormat format, int quality) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    src.compress(format, quality, output);
    return output.toByteArray();

}

From source file:Main.java

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();/*w ww . j av  a 2  s. c  o  m*/
    }

    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}