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[] bmpToByteArray(Bitmap bmp) {
    if (bmp == null)
        return null;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 80, output);

    byte[] result = output.toByteArray();
    try {/*from  ww w.  j ava 2s . c  o m*/
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

/**
 * Converts Bitmap into Byte array using PNG compress format.
 * @param bitmap Bitmap/*from ww w. j  a v a2s .c o m*/
 * @return byte[]
 */
public static byte[] convertBitmapToByte(Bitmap bitmap) {
    if (bitmap != null) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
        return stream.toByteArray();
    }
    return null;
}

From source file:disono.webmons.com.utilities.helpers.WBFile.java

/**
 * Bitmap URI//from   w  w  w.  j av  a  2  s . c  o m
 *
 * @param context
 * @param inImage
 * @return
 */
public static Uri getBmpUri(Context context, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

From source file:Main.java

/**
 * Encode an image form the given path into a {@link Base64}  string
 *
 * @param imagePath Path of the image to encode (must be absolute)
 * @return a {@link Base64} encoded string.
 *//*from www.  j a  v  a 2  s .c o m*/
public static String encodeBitmap(String imagePath) {
    Bitmap image = BitmapFactory.decodeFile(imagePath);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, bos);
    byte[] b = bos.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    return imageEncoded;
}

From source file:Main.java

public static byte[] bitmapToBytes(Bitmap bm) {
    byte[] bytes = null;
    if (bm != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        bytes = baos.toByteArray();//from  w  w w  .  j  av a  2s  .c  o m
    }
    return bytes;
}

From source file:Main.java

public static byte[] compressToBytes(Bitmap bitmap, int quality) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(65536);
    try {/*from www .j av a  2  s .c  o m*/
        bitmap.compress(CompressFormat.JPEG, quality, baos);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return baos.toByteArray();

}

From source file:Main.java

public final static byte[] BitmapChangeByte(Bitmap btm) {
    if (btm == null) {
        return null;
    }//from   www. ja  va  2  s  .c  o  m
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    btm.compress(CompressFormat.PNG, 100, baos);
    byte[] data2 = baos.toByteArray();
    return data2;
}

From source file:Main.java

public static void putImageInCache(File file, Bitmap bmp) {
    try {//w ww  . ja v  a  2 s  .  co m
        file.createNewFile();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
        Log.d("CACHE", "FILE : " + out.toString());
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean writeBitmap(Bitmap b) {
    String fn = "logo_qrcode.png";
    ByteArrayOutputStream by = new ByteArrayOutputStream();
    b.compress(Bitmap.CompressFormat.PNG, 100, by);
    byte[] stream = by.toByteArray();
    return writeToSdcard(stream, FileSavePath, fn);
}

From source file:Main.java

public static byte[] bitmap2Bytes(Bitmap bm, Bitmap.CompressFormat format, int quality) {
    try {//w  ww . ja va2s  .  co  m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(format, quality, baos);
        return baos.toByteArray();
    } finally {
        bm.recycle();
    }
}