Byte array to bitmap converting issue - android

I am trying to implement Converting bitmap to byteArray android. I need to convert my byte[] to bitmap without compression. But everytime im getting whole black image. How to do it?
What I am doing:
int bytes = bmp.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
bmp.copyPixelsToBuffer(buffer);
byte[] resarray = buffer.array();
And here how I get it to bitmap:
BitmapFactory.Options options = new
BitmapFactory.Options();
options.inScaled = false;
Bitmap bmp = BitmapFactory.decodeByteArray(barray,0, barray.length,options);
ImageView imageView = (ImageView) findViewById(R.id.resdetayimage);
imageView.setImageBitmap(bmp);`
EDIT
Bitmap factory decode only compressed things. Thats why my code not work. So I need something like:
Bitmap.Config configBmp = Bitmap.Config.valueOf(bitmap.getConfig().name());
Bitmap bitmap_tmp = Bitmap.createBitmap(width, height, configBmp);
ByteBuffer buffer = ByteBuffer.wrap(byteArray);
bitmap_tmp.copyPixelsFromBuffer(buffer);
But the code still not working. How can I implement this ? I got byte[] from intent.

Convert bitmap to byteArray
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, bStream);
byte[] mByteArray = bStream.toByteArray();
Convert byteArray to Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(mByteArray , 0, mByteArray.length);

Use copyPixelsFromBuffer() as it is the reverse of copyPixelsToBuffer().

Related

Android: Compress Bitmap from Uri

Take a picture with the normal smartphone camera.
Ok I've been Googling this for a while now, and everyone seems to use something like the following:
Bitmap bm = BitmapFactory.decodeStream(getContentResolver().openInputStream(fileUri));
ByteArrayOutputStream out = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 25, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
I use this to check the file size:
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
protected int sizeOf(Bitmap data) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
return data.getRowBytes() * data.getHeight();
} else {
return data.getByteCount();
}
}
The Bitmap is not getting any smaller, before and after:
Log.d("image", sizeOf(bm)+"");
Log.d("image", sizeOf(decoded)+"");
Results:
11-05 02:51:52.739: D/image(2558): 20155392
11-05 02:51:52.739: D/image(2558): 20155392
Pointers?
Answer:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 50;
Bitmap bmpSample = BitmapFactory.decodeFile(fileUri.getPath(), options);
Log.d("image", sizeOf(bmpPic)+"");
ByteArrayOutputStream out = new ByteArrayOutputStream();
bmSample.compress(Bitmap.CompressFormat.JPEG, 1, out);
byte[] byteArray = out.toByteArray();
Log.d("image", byteArray.length/1024+"");
The compress method, as mentioned in the documentation:
Write a compressed version of the bitmap to the specified outputstream. The bitmap can be reconstructed by passing a corresponding inputstream to BitmapFactory.decodeStream()
Thus the variable out now contains the compressed bitmap. Since you check the size after calling decodeStream the bitmap is decompressed and returned to you. Therefore the size is same.

Image Size getting Smaller after Converting to Bytes

In my android application i want to convert image into byte array and encode into string so that i can save it on database. But after compressing image it's size becomes too small..i want to keep original size.. please help me..
final Bitmap image=(images.get(position));
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, bytes);
byte[] b = bytes.toByteArray();
encodedImageString = Base64.encodeToString(b, Base64.DEFAULT);
You can use the following to store it in the database without compression:
Bitmap bitmap; // obtain bitmap object
int size = bitmap.getRowBytes() * bitmap.getHeight();
ByteBuffer b = ByteBuffer.allocate(size);
bitmap.copyPixelsToBuffer(b);
byte[] bytes = new byte[size];
b.get(bytes, 0, bytes.length);
Then you can store bytes in the database.

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.

How to convert Bitmap to string without losing quality

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. I know it is because photo.compress(Bitmap.CompressFormat.JPEG, 100, baos); as I am compressing this image(though in best compressing resolution). So how can I do this without compressing the image? I have also tried the following code but it returns nothing
.......
Bitmap photo = extras.getParcelable("data");
int bytes = photo.getWidth() * photo.getHeight() * 4;
ByteBuffer buffer = ByteBuffer.allocate(bytes);
photo.copyPixelsToBuffer(buffer);
byte[] b = buffer.array();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Also I am using sharedPreference to save the image. I thought of sqlite or internal storage also, but in both case compressing is needed as I found in internet

How to convert image file data in a byte array to a Bitmap?

I want to store image in SQLite DataBase.
I tried to store it using BLOB and String, in both cases it store the
image and can retrieve it but when i convert it to Bitmap using
BitmapFactory.decodeByteArray(...) it return null.
I have used this code, but it returns null
Bitmap bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
Just try this:
Bitmap bitmap = BitmapFactory.decodeFile("/path/images/image.jpg");
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /* Ignored for PNGs */, blob);
byte[] bitmapdata = blob.toByteArray();
If bitmapdata is the byte array then getting Bitmap is done like this:
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);
Returns the decoded Bitmap, or null if the image could not be decoded.
The answer of Uttam didnt work for me. I just got null when I do:
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);
In my case, bitmapdata only has the buffer of the pixels, so it is imposible for the function decodeByteArray to guess which the width, the height and the color bits use. So I tried this and it worked:
//Create bitmap with width, height, and 4 bytes color (RGBA)
Bitmap bmp = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
ByteBuffer buffer = ByteBuffer.wrap(bitmapdata);
bmp.copyPixelsFromBuffer(buffer);
Check https://developer.android.com/reference/android/graphics/Bitmap.Config.html for different color options

Categories

Resources