我需要从手机的相机中获取图像。我使用公式从YUV转换为RGB。然后我将RGB放入IntBuffer。然后我将IntBuffer中的像素复制到Bitmap中。这样可以得到正确的结果。 但我需要使用的不是IntBuffer,而是一个常见的int []数组。在这种情况下,函数bitmap.setPixels会产生不正确的结果。颜色是错误的。 第一张图片是正确的。第二个图像是int []的结果。
Android 4.1.1 HTC Desire X
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
Size previewSize = parameters.getPreviewSize();
int imageWidth = previewSize.width;
int imageHeight = previewSize.height;
if(toggleButton1.isChecked())
{
if(ck==0)
{
Bitmap preview_bitmap = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
Bitmap preview_bitmap2 = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
final byte alpha = (byte) 255;
int numPixels = imageWidth*imageHeight;
IntBuffer intBuffer = IntBuffer.allocate(imageWidth*imageHeight);
intBuffer.position(0);
int buff[]= new int[imageWidth*imageHeight];
int i=0;
for (int y = 0; y < imageHeight; y++) {
for (int x = 0; x < imageWidth; x++) {
int Y = data[y*imageWidth + x] & 0xff;
int xby2 = x/2;
int yby2 = y/2;
float U = (float)(data[numPixels + 2*xby2 + yby2*imageWidth] & 0xff) - 128.0f;
float V = (float)(data[numPixels + 2*xby2 + 1 + yby2*imageWidth] & 0xff) - 128.0f;
// Do the YUV -> RGB conversion
float Yf = 1.164f*((float)Y) - 16.0f;
int R = (int)(Yf + 1.596f*V);
int G = (int)(Yf - 0.813f*V - 0.391f*U);
int B = (int)(Yf + 2.018f*U);
R = R < 0 ? 0 : R > 255 ? 255 : R;
G = G < 0 ? 0 : G > 255 ? 255 : G;
B = B < 0 ? 0 : B > 255 ? 255 : B;
intBuffer.put(alpha*16777216 + R*65536 + G*256 + B);
buff[i]= (alpha*16777216 + R*65536 + G*256 + B);
i++;
}
}
intBuffer.flip();
preview_bitmap.copyPixelsFromBuffer(intBuffer);
preview_bitmap2.setPixels(buff,0,imageWidth,0,0,imageWidth,imageHeight);
//imageView1.setImageBitmap(preview_bitmap);
//imageView2.setImageBitmap(preview_bitmap2);
save_SDcard(preview_bitmap,"pic1.jpg");
save_SDcard(preview_bitmap2,"pic2.jpg");
ck++;
}
}
}
答案 0 :(得分:2)
当我遇到类似的问题时,这是我用来交换红色和蓝色通道的简单代码
for (int i = 0; i < totalPixels; ++i) {
// The alpha and green channels' positions are preserved while the red and blue are swapped
pixelsBuffer[i] = ((pixelsBuffer[i] & 0xff00ff00)) | ((pixelsBuffer[i] & 0x000000ff) << 16) | ((pixelsBuffer[i] & 0x00ff0000) >> 16);
}
答案 1 :(得分:0)
您可以使用int [],然后只使用IntBuffer.wrap创建IntBuffer引用而不分配新内存。 Bitmap.copyPixelsFromBuffer()复制原始像素而不修改它们,setPixels正在进行alpha乘法,可能还有格式转换(所有外观都有某种ARGB / BGRA转换。
尝试这些调整以继续使用copyPixelsFromBuffer()和int []:
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
Size previewSize = parameters.getPreviewSize();
int imageWidth = previewSize.width;
int imageHeight = previewSize.height;
if(toggleButton1.isChecked())
{
if(ck==0)
{
Bitmap preview_bitmap = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
Bitmap preview_bitmap2 = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
final byte alpha = (byte) 255;
int numPixels = imageWidth*imageHeight;
int buff[]= new int[numPixels];
int i=0;
for (int y = 0; y < imageHeight; y++) {
for (int x = 0; x < imageWidth; x++) {
int Y = data[y*imageWidth + x] & 0xff;
int xby2 = x/2;
int yby2 = y/2;
float U = (float)(data[numPixels + 2*xby2 + yby2*imageWidth] & 0xff) - 128.0f;
float V = (float)(data[numPixels + 2*xby2 + 1 + yby2*imageWidth] & 0xff) - 128.0f;
// Do the YUV -> RGB conversion
float Yf = 1.164f*((float)Y) - 16.0f;
int R = (int)(Yf + 1.596f*V);
int G = (int)(Yf - 0.813f*V - 0.391f*U);
int B = (int)(Yf + 2.018f*U);
R = R < 0 ? 0 : R > 255 ? 255 : R;
G = G < 0 ? 0 : G > 255 ? 255 : G;
B = B < 0 ? 0 : B > 255 ? 255 : B;
// better implementation than multiplying
buff[i] = Color.argb(alpha,R,G,B);
i++;
}
}
// just wrap your buff
IntBuffer intBuffer = IntBuffer.wrap(buff);
preview_bitmap.copyPixelsFromBuffer(intBuffer);
preview_bitmap2.setPixels(buff,0,imageWidth,0,0,imageWidth,imageHeight);
//imageView1.setImageBitmap(preview_bitmap);
//imageView2.setImageBitmap(preview_bitmap2);
save_SDcard(preview_bitmap,"pic1.jpg");
save_SDcard(preview_bitmap2,"pic2.jpg");
ck++;
}
}
}