Android的德codeYUV420SP结果以绿色形象? [英] Android decodeYUV420SP results in green images?
问题描述
好了,所以我的问题是pretty的很多相同的: <一href="http://stackoverflow.com/questions/4768165/android-problem-whith-converting-$p$pview-frame-to-bitmap">Android - 问题蒙山转换preVIEW帧位图
Ok so my question is pretty much identical to this: Android - Problem whith converting preview frame to bitmap
然而,他的回答是没有好,并尝试使用它并没有解决我的问题。
However his answer is no good, and trying to use it doesn't solve my problem.
所以,我想要做的那一刻是发送每一帧作为位图的方法来检测是否有任何的面孔,但首先我需要创建一个位图,这意味着我必须使用去codeYUV420sp方法,它似乎并没有正常工作,所有我的图片刚出来为绿色和黄色扎染看图像。这是我的code:
So what I'm trying to do at the moment is to send each frame as a bitmap to a method to detect if there are any faces, but first I need to create a bitmap which means I have to use the decodeYUV420sp method, which doesn't seem to be working properly and all my images just come out as a green and yellow tie dye looking image. Here is my code:
这是从previewFrame:
This is from onPreviewFrame:
Parameters parameters = cam.getParameters();
Integer width = parameters.getPreviewSize().width;
Integer height = parameters.getPreviewSize().height;
Log.i("preview size: ", String.valueOf(width) + "x" + String.valueOf(height));
int[] mIntArray = new int[width*height];
// Decode Yuv data to integer array
decodeYUV420SP(mIntArray, data, width, height);
//Initialize the bitmap, with the replaced color
Bitmap bmp = Bitmap.createBitmap(mIntArray, width, height, Bitmap.Config.ARGB_8888);
saveImage(bmp);
这是德codeYUV方式:
This is decodeYUV method:
static public void decodeYUV420SP(int[] rgba, byte[] yuv420sp, int width,
int height) {
final int frameSize = width * height;
for (int j = 0, yp = 0; j < height; j++) {
int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
for (int i = 0; i < width; i++, yp++) {
int y = (0xff & ((int) yuv420sp[yp])) - 16;
if (y < 0)
y = 0;
if ((i & 1) == 0) {
v = (0xff & yuv420sp[uvp++]) - 128;
u = (0xff & yuv420sp[uvp++]) - 128;
}
int y1192 = 1192 * y;
int r = (y1192 + 1634 * v);
int g = (y1192 - 833 * v - 400 * u);
int b = (y1192 + 2066 * u);
if (r < 0)
r = 0;
else if (r > 262143)
r = 262143;
if (g < 0)
g = 0;
else if (g > 262143)
g = 262143;
if (b < 0)
b = 0;
else if (b > 262143)
b = 262143;
// rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) &
// 0xff00) | ((b >> 10) & 0xff);
// rgba, divide 2^10 ( >> 10)
rgba[yp] = ((r << 14) & 0xff000000) | ((g << 6) & 0xff0000)
| ((b >> 2) | 0xff00);
}
}
}
这是方法我打电话来保存位图,看他们的样子:
and this is the method I'm calling to save the bitmaps to see what they look like:
private void saveImage(Bitmap bmp) {
File myDir=new File("/sdcard/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
下面是一个生成的图像: 的https://docs.google.com/drawings/d/1kyIvb4oHHInW_c71mjfFSVCxVopBgBWX3k1OR_nMgRA/edit
Here is a resulting image: https://docs.google.com/drawings/d/1kyIvb4oHHInW_c71mjfFSVCxVopBgBWX3k1OR_nMgRA/edit
推荐答案
确定这样的问题是,我从这里不同的计算器后得到了德codeYUV方法: <一href="http://stackoverflow.com/questions/9325861/converting-yuv-rgbimage-processing-yuv-during-on$p$pviewframe-in-android">Converting YUV-&GT; RGB(图像处理) - &GT;。YUV期间previewFrame在安卓也不太工作
Ok so the problem was the decodeYUV method which I got from a different stackoverflow post here: Converting YUV->RGB(Image processing)->YUV during onPreviewFrame in android? didn't quite work.
不过,我取代了什么,我认为必须从这里原来的德codeYUV方法: HTTP://$c$c.google.com/p /安卓/问题/详细信息?ID = 823 一个
But I replaced that with what I think must be the original decodeYUV method from here: http://code.google.com/p/android/issues/detail?id=823 an
这篇关于Android的德codeYUV420SP结果以绿色形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!