Android: Efficient way to compress Bitmap - android

Is there a decent way to compress a Bitmap without re-scaling it without eating too much memory that cause OOM?
Bitmap compressedImageFile;
try {
compressedImageFile = MediaStore.Images.Media.getBitmap(getContentResolver(), fileUri);
//Converting bitmap to array stream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//Decreasing the quality for thumbnail
compressedImageFile.compress(Bitmap.CompressFormat.PNG, 50, byteArrayOutputStream);
//Convert Bitmap to Byte array
byte[] thumbData = byteArrayOutputStream.toByteArray();
...
byteArrayOutputStream.toByteArray() is causing OOM, this can be avoid by reducing the bitmap size in a half but probably there is a more decent approach?
This is where I am using it

Related

how to reduce size of image so that it can fit to blob type..?

I'm compressing an image by using this code
imageText=encodeToBase64(bitmap, Bitmap.CompressFormat.PNG,100);
but when I store this image into my database and attribute type blobmedium then it takes too much time for displaying (loading) the image.
Even if my original image size is 32 Kb, it stores as 259 Kb in the database.
When I use tinyblob or blob type for image attribute in database it stores the value but does not show any results in the UI.
How can I reduce the size of image so that it fit to tinyblobb or blob?
So that I can store and fetch image successfully.
use the below code to compress an image:
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
// call to base64 image conversion method:
String encodeToBase64 = bitmapToBase64(thumbnail);
and then encode it to the base64 format as:
private String bitmapToBase64(Bitmap bmp)
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
Note that: A BLOB can be 65535 bytes maximum. If you need more consider using a MEDIUMBLOB for 16777215 bytes or a LONGBLOB for 4294967295 bytes

android bitmap to byte array without compression

How can we convert bitmap image to byte array in Android without compressing the image? Image to byte array in Android:
Bitmap photo = (Bitmap) data.getExtras().get("data");
img_view.setImageBitmap(photo);
Bitmap resizedImage = Bitmap.createScaledBitmap(photo,512,512 , true);
ByteArrayOutputStream bos = new ByteArrayOutputStream(photo.getWidth()*photo.getHeight());
resizedImage.compress(Bitmap.CompressFormat.PNG,100, bos);
bArray = bos.toByteArray();
Set image size between 300 to 400 kb.

Bitmap to byteArray without compress

I am aware of the code below
Bitmap photo = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
but the issue is that this code reduces the image quality so much that it is unrecognizable how can i avoid
photo.compress(Bitmap.CompressFormat.PNG,100, stream);
You can use CompressFormat.PNG, where the compress quality doesn't matter, its always lossless as described in the documentation
array = new byte[w*h*4];
Buffer dst = ByteBuffer.wrap(array);
bmp.copyPixelsToBuffer(dst);

Bitmap compress doesn't changing bitmap byte size

I'm trying to reduce bitmap size by using compress method.
This is my code:
public Bitmap compressImage(Bitmap image) {
Bitmap immagex = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Log.i("before compress", immagex.getByteCount()+"");
boolean compress = immagex.compress(Bitmap.CompressFormat.JPEG, 10, baos);
if(compress)
Log.i("after compress", immagex.getByteCount()+"");
else
Log.i("bad compress", "bad compress");
return immagex;
}
When i check my logs i get:
11-28 11:10:38.252: I/before compress(2429): 374544
11-28 11:10:38.262: I/after compress(2429): 374544
Why is the compress does not work?
UPDATE:
I tried this code:
public Bitmap compressImage(Bitmap image) {
Bitmap immagex = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Log.i("before compress", immagex.getByteCount()+"");
boolean compress = immagex.compress(Bitmap.CompressFormat.JPEG, 10, baos);
Log.i("after compress 2", decodeSampledBitmapFromByte(image.getWidth(), image.getHeight(), baos.toByteArray()).getByteCount()+"");
return immagex;
}
Still the same byte count
11-28 11:33:04.335: I/before compress(3472): 374544
11-28 11:33:04.395: I/after compress 2(3472): 374544
Following is the code for reducing the bitmap size and convert it to base64,
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 50, 50, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArray = baos.toByteArray();
String imageEncoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
Log.e("Base 64 String", imageEncoded);
Using Bitmap.compress() you just specify compression algorithm and by the way compression operation takes rather big amount of time. If you need to play with sizes for reducing memory allocation for your image, you exactly need to use scaling of your image using Bitmap.Options, computing bitmap bounds at first and then decoding it to your specified size.
The best sample that I found on StackOverflow is this one.
Strange out of memory issue while loading an image to a Bitmap object
Compression does not change the size of the bitmap. You should look at the size of the output byte stream which contains compressed data.

How to keep image quality same in BitmapFactory

I've converted an bitmap image into string to save it:
............
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Then I retrieve the bitmap from string to set an activity's background just like that:
byte[] temp = Base64.decode(encodedImage, Base64.DEFAULT);
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0,
temp.length, options);
Drawable d = new BitmapDrawable(getResources(), bitmap);
getWindow().setBackgroundDrawable(d);
Everything works fine but the image quality reduces tremendously. How can I keep the image quality same as the original image? Did I do something wrong here that have reduced the quality?
JPEG is lossy, no matter what quality settings you use. If you want to keep the image unchanged, you have to use lossless compression. for example Bitmap.CompressFormat.PNG
You are having here a tradeoff situation between picture quality and memory usage. Take a look at this line:
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
photo.compress is obviously decreasing your image resolution in a factor given by the second parameter, unfortunately, this is the best quality you can get, since between 0 - 100, 100 stands for the best quality you can get. Now, you have another option, depending on the original picture's size you can just save the image without compressing it, but be aware that most cases this doesn't work and Jalvik can throw an OutofMemoryException,
hope this helps.

Categories

Resources