Home
> Tricks & Tips > Convert Bitmap to byte array and reverse
Convert Bitmap to byte array and reverse
If you want to convert a `Bitmap` to byte array:
public byte[] getByteArray(Bitmap bitmap) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 0, bos); return bos.toByteArray(); }
and if you want the reverse process:
public Bitmap getBitmap(byte[] bitmap) { return BitmapFactory.decodeByteArray(bitmap , 0, bitmap.length); }
Cheers,
Pete Houston
Categories: Tricks & Tips
array, bitmap, byte, convert
Merci beaucoup le gar, thx.
I want to send image from android to matlab. In this i firstly converted that image into bitmap and convert into bytearray.i not know how to send byte array or can i send pixels array.plz help
hey i need to convert a bytebuffer to bitmap.here is code
byte[] bytes = new byte[10];
byte_buffer = ByteBuffer.wrap(bytes);
bytes = new byte[byte_buffer.remaining()];
byte_buffer.get(bytes, 0, bytes.length);
byte_buffer.clear();
bytes = new byte[byte_buffer.capacity()];
byte_buffer.get(bytes, 0, bytes.length);
Bitmap sb = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Error log:
— SkImageDecode : Factory returned null;
This is really interesting, You’re a very skilled blogger.
I’ve joined your feed and look forward to seeking more of
your wonderful post. Also, I have shared your web site in my social networks!
Hi there, I discovered your site by the use of Google
even as searching for a similar topic, your website came up, it seems
good. I have bookmarked it in my google bookmarks.
Hi there, simply changed into alert to your blog
thru Google, and found that it’s really informative.
I am going to be careful for brussels. I’ll be grateful for those who continue this in future.
A lot of folks might be benefited from your writing. Cheers!
Very good post. I absolutely appreciate this
site. Continue the good work!
I converted image into grayscale then read the pixels like this
for (int i = 0; i < image.getHeight(); i++)
{
for (int j = 0; j < image.getWidth(); j++)
{
pixels[k]=(byte) (image.getPixel(j, i));
pixels2[k]=unsignedToBytes(pixels[k]);
k++;
}
}
And performed edge detection now getting positive byte array so how should i again display it as image.
tried this one but not getting result
imageView1.setImageBitmap(BitmapFactory.decodeByteArray(edgeImage1, 0, edgeImage1.length));
please help us…..
I have byte array data of a jpg image. Is it possible to convert the array into the bitmap.
I propose this method:
private byte[] Convert(Bitmap bmp) {
int bytes = bmp.getWidth() * bmp.getHeight() * 4;
Log.e(“Bytes”, String.valueOf(bytes));
ByteBuffer buffer = ByteBuffer.allocate(bytes);
bmp.copyPixelsToBuffer(buffer);
byte[] array = buffer.array();
if (array != null) {
return array;
}
return null;
}
I am getting “SkImageDecoder::Factory returned null “, do u have any idea why it’s coming ,
your bitmap is null
Thanx man.Works like a charm!
I am getting “SkImageDecoder::Factory returned null “, do u have any idea why it’s coming ,
as I am getting size of byte[] as well.