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 Bitmap to byte array//from  w  w  w .  j  a  va  2s.  co  m
 * @param bitmap
 * @return
 */
public static byte[] bitmapToByte(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }

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

From source file:com.lewie9021.videothumbnail.VideoThumbnail.java

public static String encodeTobase64(Bitmap image) {
    Bitmap bmImage = image;

    ByteArrayOutputStream byteArrayData = new ByteArrayOutputStream();
    bmImage.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayData);
    byte[] byteData = byteArrayData.toByteArray();
    String encodedImage = Base64.encodeToString(byteData, Base64.DEFAULT);

    return encodedImage;
}

From source file:Main.java

public static String saveToLocal(Bitmap bm) {
    String path = "/sdcard/test.jpg";
    try {//www .  j  ava  2  s  .co m
        FileOutputStream fos = new FileOutputStream(path);
        bm.compress(CompressFormat.JPEG, 75, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    return path;
}

From source file:Main.java

public static File saveBitmapFile(Bitmap bitmap, String path) {
    FileOutputStream out = null;/*from w ww .  j a va 2s . c  o m*/
    try {
        out = new FileOutputStream(path);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return new File(path);
}

From source file:Main.java

public static boolean saveDrawabletoFile(Context c, Drawable d, File file) {
    //create a file to write bitmap data
    try {//from w  ww. j  a v  a2s .c  o  m
        file.createNewFile();
        //Convert bitmap to byte array
        Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
        byte[] bitmapdata = bos.toByteArray();

        //write the bytes in file
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
        return true;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return false;
}

From source file:Main.java

public static boolean saveImageTo(Bitmap photo, String spath) {
    try {/*from   w  w  w .  j  a  v a 2s .  c  om*/
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(spath, false));
        photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
        bos.flush();
        bos.close();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

private static byte[] resizeImage(byte[] input, int length) {
    Bitmap original = BitmapFactory.decodeByteArray(input, 0, input.length);
    Bitmap resized = Bitmap.createScaledBitmap(original, length, length, true);

    ByteArrayOutputStream blob = new ByteArrayOutputStream();
    resized.compress(Bitmap.CompressFormat.JPEG, 100, blob);

    return blob.toByteArray();
}

From source file:Main.java

/**
 * Writes an image to file//www . j  a v  a 2s.c o m
 *
 * @param src  the image to write
 * @param file the destination file
 * @return true in case of success, false otherwise
 */
private static boolean writeBitmapToFile(Bitmap src, File file) {
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(file);
        src.compress(Bitmap.CompressFormat.JPEG, 70, out); // bmp is your Bitmap instance
        return true;
    } catch (Exception e) {
        Log.d(TAG, "Error writing image", e);
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            Log.d(TAG, "Error writing image", e);
        }
    }
    return false;
}

From source file:Main.java

public static String base64Encode(ImageView imageView) {
    if (imageView.getDrawable() != null) {
        Bitmap avatarBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        avatarBitmap.compress(Bitmap.CompressFormat.PNG, 70, baos);
        byte[] avatarByteArray = baos.toByteArray();
        return Base64.encodeToString(avatarByteArray, Base64.DEFAULT);
    } else {//  w  w  w.ja  v  a 2 s  .  c o m
        return null;
    }
}

From source file:Main.java

public static byte[] getByteArray(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }//from ww w.ja v a2 s . co  m

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