Java android中的输出图像为黑色
我首先尝试将jpg转换为rgb值数组,然后尝试将同一数组还原为jpgJava android中的输出图像为黑色,java,android,bitmap,fileoutputstream,Java,Android,Bitmap,Fileoutputstream,我首先尝试将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++) {
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;
}
}
请帮助我进一步了解,谢谢首先让我解释一下这些数据是如何按像素存储的: 每个像素有32位数据来存储一个值:Alpha、红色、绿色和蓝色。每个值只有8位(或一个字节)。(存储颜色信息有很多其他格式,但您指定的格式是
ARGB_8888
)
在此格式中,白色为0xffffffff
,黑色为0xff000000
所以,正如我在评论中所说,阿尔法似乎缺失了。没有任何类似alpha的0x00ff0000
的红色像素将不可见
可以通过首先存储Alpha来添加它:
A=(pix[index]>>24)&0xff;
虽然这个值可能是255(因为JPEG没有alpha),但我认为如果您决定使用另一种有alpha的格式,那么像这样使用它是明智的
然后,您应该将alpha放回:
pix[index]=(A您似乎缺少每个像素的一个字节的数据:alpha。@KompjoeFriek,请您详细解释一下,我不知道这一点。alpha控制像素的可见程度,从0(不可见)到255(完全可见或不透明)。当您使用pix[index]更改像素时=(R@KompjoeFriek,那么我如何在代码中添加这个Alpha。你可以在谷歌上搜索格式
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();
}
}
}