7

I have an image which is stored as a byte[] array, and I want to flip the image before I send it off to be processed elsewhere (as a byte[] array).

I've searched around and can't find a simple solution without manipulating each bit in the byte[] array.

What about converting the byte array[] to an image type of some sort, flipping that using an existing flip method, and then converting that back to a byte[] array?

Any advice?

Cheers!

Ahmad
  • 69,608
  • 17
  • 111
  • 137
LKB
  • 1,020
  • 5
  • 22
  • 46
  • What do you mean by "flip"? – fge Jun 05 '13 at 22:32
  • Rotate the image so it goes from an "upside down" image to an "upright" image. – LKB Jun 05 '13 at 22:34
  • 2
    `What about converting the byte array[] to an image type of some sort, flipping that using an existing flip method, and then converting that back to a byte[] array?` Yes. Convert to bitmap, rotate, then convert back to the array. – Voicu Jun 05 '13 at 22:35
  • Thanks @Voicu, do you know the best way to convert a byte[] array to Bitmap? – LKB Jun 05 '13 at 22:43
  • 1
    Added an answer with some code. – Voicu Jun 05 '13 at 22:44

2 Answers2

11

Byte array to bitmap:

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

Use this to rotate the image by providing the 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 back to the array:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] flippedImageByteArray = stream.toByteArray();
Voicu
  • 16,921
  • 10
  • 60
  • 69
  • My image is Gray8 format, the conversion process from byte array to Bitmap and back again should not affect the format, right? Thank you very much for your help. :) – LKB Jun 05 '13 at 22:58
  • Eek, NullPointerException when returning the bitmap - return Bitmap.createBitmap(bitmapSource, 0, 0, bitmapSource.getWidth(), bitmapSource.getHeight(), matrix, true); – LKB Jun 05 '13 at 23:01
  • 1
    You most likely sent a null reference to the `rotateImage` method because at that first step the image could not be decoded into a `Bitmap`. – Voicu Jun 05 '13 at 23:04
  • Most likely the Gray8 format is the culprit here. Any chance of transforming those images to a PNG/JPEG/GIF format? – Voicu Jun 05 '13 at 23:17
  • Yeah I've inserted the byte array to Bitmap conversion code before my byte array is converted to gray8 format and it works. :) – LKB Jun 05 '13 at 23:24
  • You should not decode the image. Why? because you might run out of memory and therefore you would need to downscale the image while decoding. Then you are flipping the downscaled image. So the final result is correctly flipped, but shitty resolution. – Matthias Aug 07 '14 at 10:46
0

The following is a method used to flip the image which is stored as byte array and return the result in byte array.

private byte[] flipImage(byte[] data, int flip) {
    Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
    Matrix matrix = new Matrix();
    switch (flip){
        case 1: matrix.preScale(1.0f, -1.0f); break; //flip vertical
        case 2: matrix.preScale(-1.0f, 1.0f); break; //flip horizontal
        default: matrix.preScale(1.0f, 1.0f); //No flip
    }

    Bitmap bmp2 = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp2.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    return stream.toByteArray();
}

If you want a vertical flipped image then pass 1 as flip value and for horizontal flip pass 2.

For Eg:

@Override
public void onPictureTaken(byte[] data, Camera camera) {
   byte[] verticalFlippedImage = flipImage(data,1);
   byte[] horizontalFlippedImage = flipImage(data,2);
}
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81