I have a bitmap that I want to send to the server by encoding it to base64 but I do not want to compress the image in either png or jpeg.
Now what I was previously doing was.
ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream(); bitmapPicture.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY, byteArrayBitmapStream); byte[] b = byteArrayBitmapStream.toByteArray(); //then simple encoding to base64 and off to server encodedImage = Base64.encodeToString(b, Base64.NO_WRAP);
Now I just dont want to use any compression nor any format plain simple byte[] from bitmap that I can encode and send to the server.
Any pointers?
Read the image using the read() method of the ImageIO class. Create a ByteArrayOutputStream object. Write the image to the ByteArrayOutputStream object created above using the write() method of the ImageIO class. Finally convert the contents of the ByteArrayOutputStream to a byte array using the toByteArray() method.
Write the contents of the object to the output stream using the writeObject() method of the ObjectOutputStream class. Flush the contents to the stream using the flush() method. Finally, convert the contents of the ByteArrayOutputStream to a byte array using the toByteArray() method.
A byte array is simply a collection of bytes. The bytearray() method returns a bytearray object, which is an array of the specified bytes. The bytearray class is a mutable array of numbers ranging from 0 to 256.
The recycle() method allows an app to reclaim memory as soon as possible. Caution: You should use recycle() only when you are sure that the bitmap is no longer being used. If you call recycle() and later attempt to draw the bitmap, you will get the error: "Canvas: trying to use a recycled bitmap" .
You can use copyPixelsToBuffer()
to move the pixel data to a Buffer
, or you can use getPixels()
and then convert the integers to bytes with bit-shifting.
copyPixelsToBuffer()
is probably what you'll want to use, so here is an example on how you can use it:
//b is the Bitmap //calculate how many bytes our image consists of. int bytes = b.getByteCount(); //or we can calculate bytes this way. Use a different value than 4 if you don't use 32bit images. //int bytes = b.getWidth()*b.getHeight()*4; ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer byte[] array = buffer.array(); //Get the underlying array containing the data.
instead of the following line in @jave answer:
int bytes = b.getByteCount();
Use the following line and function:
int bytes = byteSizeOf(b); protected int byteSizeOf(Bitmap data) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) { return data.getRowBytes() * data.getHeight(); } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return data.getByteCount(); } else { return data.getAllocationByteCount(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With