我首先尝试将jpg转换为rgb值数组,然后尝试将相同的数组恢复为jpg
picw = selectedImage.getWidth();
pich = selectedImage.getHeight();
int[] pix = new int[picw * pich];
selectedImage.getPixels(pix, 0, picw, 0, 0, picw, pich);
int R, G, B;
for (int y = 0; y < pich; y++) {
for (int x = 0; x < picw; x++) {
int index = y * picw + x;
R = (pix[index] >> 16) & 0xff;
G = (pix[index] >> 8) & 0xff;
B = pix[index] & 0xff;
pix[index] = (R << 16) | (G << 8) | B;
}
}
直到这一点,一切都很好(我通过Loging the array检查),但是当我创建位图以jpg压缩它时,输出是黑色图像。
Bitmap bmp = Bitmap.createBitmap(pix, picw, pich,Bitmap.Config.ARGB_8888);
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(folder,"Wonder.jpg");
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
请帮我进一步,谢谢
答案 0 :(得分:0)
首先让我解释一下每个像素的数据存储方式:
每个像素具有32位数据,用于存储以下值:Alpha,Red,Green和Blue。这些值中的每一个仅为8位(或一个字节)。 (有很多其他格式来存储颜色信息,但您指定的格式是ARGB_8888
)。
在此格式中,白色为0xffffffff
,黑色为0xff000000
。
所以,就像我在评论中所说的那样,alpha似乎不见了。没有任何像0x00ff0000
这样的alpha的红色像素将无法显示。
可以通过首先存储它来添加Alpha:
A = (pix[index] >> 24) & 0xff;
虽然该值可能是255(因为JPEG没有alpha),但我认为如果您决定使用另一种具有alpha的格式,这样使用它会是明智的。
然后你应该把alpha放回来:
pix[index] = (A << 24) | (R << 16) | (G << 8) | B;
这应该将完全相同的值写入它已包含的pix[index]
,而不是更改任何内容。但它会留下原始图像,而不仅仅是黑色。