【发布时间】:2012-09-10 04:41:33
【问题描述】:
好的,所以我的问题与此几乎相同: Converting preview frame to bitmap
但是他的回答不好,尝试使用它并不能解决我的问题。
所以我现在要做的是将每一帧作为位图发送到一个方法来检测是否有任何面孔,但首先我需要创建一个位图,这意味着我必须使用 decodeYUV420sp 方法,这似乎无法正常工作,我所有的图像都只是以绿色和黄色的扎染图像的形式出现。这是我的代码:
这是来自 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);
这是decodeYUV方法:
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);
}
}
}
这是我调用来保存位图以查看它们的样子的方法:
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
【问题讨论】:
-
我也无法通过以下方式解决这个问题:parameters.setPreviewFormat(ImageFormat.RGB_565);我的三星 nexus 不支持此功能。
-
好的,所以问题是我从这里的另一个 stackoverflow 帖子中获得的 decodeYUV 方法:stackoverflow.com/questions/9325861/… 不太有效。但我将其替换为我认为必须是这里的原始 decodeYUV 方法:code.google.com/p/android/issues/detail?id=823 并且图像正常显示。
-
很好,那么问题现在解决了吗?请添加 [关闭]e 主题以反映这一点!
-
顺便说一句,Android 人脸识别仅适用于灰度,因此您可以通过忽略 u、v 值并将每个像素的 r、g、b 分量设置为 y 值来提高性能。
-
谢谢,我的声誉也太低了,无法回答我自己的问题并再解决几个小时,所以我会尽快解决
标签: android android-camera face-detection