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

/**
 * convert (encode) image to string/* w  ww.  j av  a2 s.c om*/
 * **/
public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality) {
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    image.compress(compressFormat, quality, byteArrayOS);
    return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}

From source file:Main.java

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

From source file:Main.java

private static byte[] bitmap2Bytes(Bitmap bm) {
    if (bm == null) {
        return null;
    }//from  w ww .ja  v  a2 s .c  o m
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

private static byte[] getBytesFromDriveImageUri(Context context, Uri uri) {
    InputStream inputStream = null;
    try {//w ww .j a v a2 s.  co  m
        inputStream = context.getContentResolver().openInputStream(uri);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    Bitmap bmp = BitmapFactory.decodeStream(inputStream);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);

    return stream.toByteArray();
}

From source file:Main.java

public static byte[] compressBmpToBytes(Bitmap bitmap, int maxSize) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int options = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
    while (baos.toByteArray().length / 1024 >= maxSize) {
        baos.reset();//from  w  ww.  ja v  a 2  s  .  c o m
        options -= 5;
        bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
    }

    return baos.toByteArray();
}

From source file:Main.java

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

From source file:Main.java

public static void writeBitmapToFile(String fileName, Bitmap bitmap) {
    FileOutputStream out = null;//from   w ww. java 2s. c o m
    try {
        out = new FileOutputStream(fileName);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

/**
 * Compresses a bitmap into a PNG and converts into a Base64 encoded string.
 * The encoded string can be decoded using {@link decodeBitmapFromString(String)}.
 * @param bitmap The Bitmap to compress and encode.
 * @return the String encoding the Bitmap.
 *//*  w w w.j  a v a2s  .  c  o  m*/
public static String encodeBitmapAsString(Bitmap bitmap) {
    if (bitmap == null)
        return "";
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
    return Base64.encodeToString(output.toByteArray(), Base64.DEFAULT);
}

From source file:Main.java

public static byte[] toBytes(Bitmap bmp) {
    // Default size is 32 bytes
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {/*from   w  w  w .j a  va 2s.com*/
        bmp.compress(CompressFormat.JPEG, 100, bos);
        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bos.toByteArray();
}

From source file:Main.java

public static void save(Bitmap bitmap, String filepath) {
    try {/* ww  w  . j  a  va  2s .com*/
        FileOutputStream fos = new FileOutputStream(filepath);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        bitmap.recycle();
        fos.close();
    } catch (FileNotFoundException e) {

    } catch (IOException e) {

    }
}