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 boolean bitmapToFile(String fileName, Bitmap bmp) {
    try {//from   w w  w. j  av  a  2s.  c o m
        FileOutputStream out = new FileOutputStream(fileName);
        bmp.compress(Bitmap.CompressFormat.JPEG, 90, out);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

}

From source file:Main.java

public static boolean writeImage(File file, Bitmap mBitmap) {
    try {// w  w w .  j av a  2  s  .  c  om
        FileOutputStream fo = new FileOutputStream(file);
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fo);
        fo.flush();
        fo.close();
    } catch (Exception e) {
        System.out.println(e.getLocalizedMessage());
        return false;
    }
    return true;
}

From source file:Main.java

private static String storeImage(Context context, Bitmap bitmap) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);

    File f = null;//w  ww. j a va  2  s  .  c  om
    try {
        f = File.createTempFile("citationsImg", ".png", context.getExternalCacheDir());
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return f.getAbsolutePath();
}

From source file:Main.java

public static InputStream Bitmap2IS(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // What about 50...
    bm.compress(Bitmap.CompressFormat.JPEG, 50, baos);
    InputStream sbs = new ByteArrayInputStream(baos.toByteArray());
    return sbs;//from  www  .  j  ava2s  .  co m
}

From source file:Main.java

public static byte[] bitmapToByteArray(Bitmap bitmap, Bitmap.CompressFormat format) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    bitmap.compress(format, 100, out);
    return out.toByteArray();
}

From source file:Main.java

/**
 * Method to convert a Bitmap into a byte[].
 * Attention: this method is very expensive!!!
 *
 * @param source The Bitmap to convert.//  ww  w. j ava2 s.co  m
 * @return The byte[] that represents the source Bitmap.
 */
public static byte[] toByteArray(Bitmap source) throws IOException {
    ByteArrayOutputStream blob = new ByteArrayOutputStream();
    source.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, blob);
    byte[] photoByteArray = blob.toByteArray();
    blob.close();
    return photoByteArray;
}

From source file:Main.java

public static Bitmap compressImage(Bitmap image) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    int options = 100;
    while (baos.toByteArray().length / 1024 > 100) {
        options -= 10;/*from   w ww .  ja v  a2 s  .  c  o  m*/
        if (options > 0) {
            baos.reset();
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);
        }
    }
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
    return BitmapFactory.decodeStream(isBm, null, null);
}

From source file:Main.java

public static String saveToFile(String filename, Bitmap bmp) {
    try {//from  w  ww . j a  v  a 2  s .c om
        FileOutputStream out = new FileOutputStream(filename);
        bmp.compress(CompressFormat.PNG, 100, out);
        out.flush();
        out.close();

        return filename;
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static void saveCover(Bitmap cover, String path) {
    try {//from w  w  w.j  a va2  s.c o m
        FileOutputStream out = new FileOutputStream(path);
        cover.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.close();
    } catch (Exception e) {
        // Do nothing
    }
}

From source file:Main.java

/**
 * Encodes given <tt>Bitmap</tt> to array of bytes using given
 * compression <tt>quality</tt> in PNG format.
 * @param bmp the bitmap to encode.//from w  ww  . j  av a  2 s . co m
 * @param quality encoding quality in range 0-100.
 * @return raw bitmap data PNG encoded using given <tt>quality</tt>.
 */
public static byte[] convertToBytes(Bitmap bmp, int quality) {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, quality, bout);
    return bout.toByteArray();
}