Flip image stored as byte [] array

I have an image that is stored as a byte [] array, and I want to flip the image before sending it for processing elsewhere (as a byte [] array).

I searched around and cannot find a simple solution without manipulating every bit in the byte [] array.

How to convert an array of bytes [] into an image type of some type, flip it using the existing flip method, and then convert it back to a byte [] array?

Any tips?

Hooray!

+7
source share
1 answer

Byte array for bitmap:

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 

Use this to rotate the image by providing a right angle (180):

 public Bitmap rotateImage(int angle, Bitmap bitmapSrc) { Matrix matrix = new Matrix(); matrix.postRotate(angle); return Bitmap.createBitmap(bitmapSrc, 0, 0, bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true); } 

Then go back to the array:

 ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] flippedImageByteArray = stream.toByteArray(); 
+9
source

All Articles