2013-06-05 43 views
7

我有一个存储为byte []数组的图像,我想在发送它之前将图像翻转以在别处处理(作为byte []数组) 。存储为字节[]阵列的翻转图像

我四处搜索,找不到一个简单的解决方案,而无需操纵byte []数组中的每一位。

将字节数组[]转换为某种类型的图像类型,使用现有的翻转方法翻转,然后将其转换回byte []数组?

有什么建议吗?

干杯!

+0

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

使用此通过提供正确的角度(180)旋转图像“翻转”是什么意思? – fge

+0

旋转图像,使图像从“颠倒”图像变为“直立”图像。 – LKB

+2

'将字节数组[]转换为某种类型的图像类型,翻转使用现有的翻转方法,然后将其转换回byte []数组?'是的。转换为位图,旋转,然后转换回数组。 – Voicu

回答

9

字节数组的位图:

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

然后回到数组​​::

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

我的图像是Gray8格式,从字节数组到Bitmap的转换过程不会影响格式,对不对?非常感谢您的帮助。 :) – LKB

+0

伊克,返回位图时的NullPointerException - 返回Bitmap.createBitmap(的BitmapSource,0,0,bitmapSource.getWidth(), \t \t \t \t bitmapSource.getHeight(),矩阵,TRUE); – LKB

+1

您很可能向'rotateImage'方法发送了null引用,因为在第一步中图像无法解码为“Bitmap”。 – Voicu