【发布时间】:2016-03-31 19:07:39
【问题描述】:
我首先尝试将 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;
}
}
到目前为止,一切都很好(我通过记录数组进行了检查),但是当我创建位图以将其压缩为 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();
}
}
}
请帮助我进一步移动,谢谢
【问题讨论】:
-
您似乎缺少每个像素的一个字节数据:alpha。
-
@KompjoeFriek,请你详细解释一下,我不知道。
-
Alpha 控制像素可见的程度,从 0(不可见)到 255(完全可见或不透明)。当您使用
pix[index] = (R << 16) | (G << 8) | B;更改像素时,您将 alpha 的值保留为 0。 -
@KompjoeFriek,那么我如何在这段代码中添加这个 Alpha。
-
你可以googleRGBA格式
标签: java android bitmap fileoutputstream