Flip image stored as a byte[] array

情到浓时终转凉″ 提交于 2019-12-01 01:31:30

问题


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!


回答1:


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();


来源:https://stackoverflow.com/questions/16950953/flip-image-stored-as-a-byte-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!