Flip image stored as a byte[] array

后端 未结 1 448
灰色年华
灰色年华 2021-01-12 08:16

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 ar

相关标签:
1条回答
  • 2021-01-12 08:54

    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();
    
    0 讨论(0)
提交回复
热议问题