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

/**
 * bitmap -> bytes/*from   w  ww. j  av a2s.  c o  m*/
 * 
 * */
public static byte[] bitmap2Bytes(Bitmap bitmap) {
    if (null == bitmap) {
        return null;
    }

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);

    return byteArrayOutputStream.toByteArray();
}

From source file:Main.java

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

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

From source file:Main.java

public static byte[] bitmap2Bytes(Bitmap bitmap, Bitmap.CompressFormat format) {
    if (bitmap == null)
        return null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(format, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

private static boolean tryToSaveBitmap(Bitmap bitmap, String savePath) {
    FileOutputStream output = null;
    try {/*from   w w w  .ja v  a  2  s  .c  o  m*/
        output = new FileOutputStream(savePath);
        return bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
    } catch (IOException ioe) {
        // do nothing
    } finally {
        try {
            if (output != null) {
                output.flush();
                output.close();
            }
        } catch (Exception e) {
            // ignore...
        }
    }
    return false;
}

From source file:Main.java

public static byte[] encodeToBytes(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }/*  ww w  .  java 2  s  .  co m*/

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

From source file:Main.java

/**
 * Bitmap into compressed jpeg//from w  w w  .  j a va2s  .co  m
 *
 * @param image the Bitmap
 * @param quality quality of compression
 * @return byte array of jpeg
 */
public static byte[] bitmapToJpg(final Bitmap image, final int quality) {
    if (image == null)
        return null;

    ByteArrayOutputStream ba = new ByteArrayOutputStream();

    if (image.compress(CompressFormat.JPEG, quality, ba))
        return ba.toByteArray();
    else
        return null;
}

From source file:Main.java

public static void saveBmpToFile(Bitmap bm, File f) throws FileNotFoundException, IOException {
    if (f.exists()) {
        return;//  w ww . j av a2 s. co  m
    }

    FileOutputStream fos = new FileOutputStream(f);
    bm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.close();
}

From source file:Main.java

public static byte[] compressToBytes(Bitmap bitmap, int quality) {
    ByteArrayOutputStream baos = null;
    try {//from  w  w w . ja  v  a2 s . c om
        baos = new ByteArrayOutputStream(65536);
        bitmap.compress(CompressFormat.PNG, quality, baos);
        return baos.toByteArray();
    } finally {
        try {
            baos.flush();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static boolean saveBitmapToFile(Bitmap bitmap, String path) {
    File file = new File(path);
    FileOutputStream fOut;//from w  ww.j  a va  2 s. c o m
    try {
        fOut = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 95, fOut);
        fOut.flush();
        fOut.close();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;

}

From source file:Main.java

/**
 * @param maxSize unit kb/*from   ww w.  j av a  2  s .  c  o m*/
 */
public static boolean compressSizeToFile(Bitmap bitmap, int maxSize, File outFile) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int quality = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
    while (baos.toByteArray().length / 1024.0 > maxSize) {
        if (quality < 30) {
            break;
        }
        quality -= 20;
        baos.reset();
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
    }
    try {
        FileOutputStream fileOutputStream = new FileOutputStream(outFile);
        fileOutputStream.write(baos.toByteArray());
        fileOutputStream.close();
    } catch (Exception e) {
        return false;
    }
    return true;
}