android bitmap to byte array without compression - android

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.

Related

Android: Efficient way to compress Bitmap

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

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

Encoding and Decoding image

I want to insert image into 'MYSQL' database using json and retrive the inserted image into 'ImageView'.I done encoding as well as inserting image but not able to do the decoding and set the image to 'ImageView'.
//Encoding the imageView
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.id.imageVieweditprofileuserphoto);
BitmapDrawable drawable = (BitmapDrawable) imageofuser.getDrawable();
bitmap = drawable.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,90, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
String image_str= Base64.encodeToString(byte_arr, 1);
//Image after encoding at server side(Database MySql)
iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAIAAABt+uBvAAAAA3NCSVQICAjb4U/gAAAQPElEQVR4
nO1c6W9cR3Kv6u735s0Mh4dFUjy0luUj9lo+13YQJIGdYAUs8ilAPuRPyJ+XAPFugBjYrLO2s+s1
1rZsyxJ18BDP4XDueWcflQ/9ZjjiIUp8b0jCqx9IDTVHv+rfVFVXV1c9rFar8AzHg523ABcdzwg6
Ac8IOgHPCDoBzwg6Ac8IOgHP
//Decoding the imageView
imageofuser=(ImageView)view.findViewById(R.id.imageVieweditprofileuserphoto);
byte[] imagefromserver=Base64.decode(Utility.userimage, Base64.NO_WRAP);
Bitmap bmp=BitmapFactory.decodeByteArray(imagefromserver, 0, imagefromserver.length);
imageofuser.setImageBitmap(bmp);

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.

ByteArray to Bitmap gives null

I am trying to convert byte array to bitmap to display an image in android app. But while converting it is returning the null value. I have used the following code:
operations = new DataBaseOperations();
byte image[] = operations.fetchimage(); // gets byte array from the database
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length, options);
Herebitmap is null, why?
Try this link. It will solve your problem
How to convert byte array to Bitmap
or just check this code
Bitmap bitmap = BitmapFactory.decodeFile("/path/images.jpg");
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, blob);
byte[] bitmapdata = blob.toByteArray();
//if bitmapdata is the byte array then getting bitmap goes like this
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);
Returns The decoded bitmap, or null if the image could not be decode.

Categories

Resources