Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android FaceDetector不检测人脸_Android_Image Processing - Fatal编程技术网

Android FaceDetector不检测人脸

Android FaceDetector不检测人脸,android,image-processing,Android,Image Processing,我从设备摄像头获取图像: public synchronized void onPreviewFrame(byte[] data, Camera camera) { //Get image from data } 将图像解码为RGB: int[] rgb = decodeYUV420SPtoRGB(data, width, height); RGB到位图: Bitmap bitmap = rgbToBitmap(rgb,width, height); 在ImageView中

我从设备摄像头获取图像:

public synchronized void  onPreviewFrame(byte[] data, Camera camera) {

    //Get image from data


}
将图像解码为RGB:

int[] rgb = decodeYUV420SPtoRGB(data, width, height);
RGB到位图:

Bitmap bitmap = rgbToBitmap(rgb,width, height);
在ImageView中打印图像以确保转换正常:

iv.setImageBitmap(bitmap);
检测num个面:

        FaceDetector faceDet = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), 1);
        Face[] faceList = new Face[1];
        int faces = faceDet.findFaces(bitmap, faceList);
        Log.e("NumFaces: ",faces + "");
解码功能:

private  int[] decodeYUV420SPtoRGB(byte[] yuv420sp, int width, int height) {
    if (yuv420sp == null) throw new NullPointerException();

    final int frameSize = width * height;
    int[] rgb = new int[frameSize];

    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 & (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);
        }
    }

    return rgb;

}
 public static Bitmap rgbToBitmap(int[] rgb, int width, int height) {
    if (rgb == null) throw new NullPointerException();

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    bitmap.setPixels(rgb, 0, width, 0, 0, width, height);
    return bitmap;
}
private int[]decodeYUV420SPtoRGB(字节[]yuv420sp,int-width,int-height){
如果(yuv420sp==null)抛出新的NullPointerException();
最终整数帧大小=宽度*高度;
int[]rgb=新的int[frameSize];
对于(int j=0,yp=0;j>1)*宽度,u=0,v=0;
对于(int i=0;i262143)r=262143;
如果(g<0)g=0;
否则,如果(g>262143)g=262143;
如果(b<0)b=0;
否则,如果(b>262143)b=262143;
rgb[yp]=0xff000000|((r>2)和0xff00)|((b>>10)和0xff);
}
}
返回rgb;
}
公共静态位图rgbToBitmap(int[]rgb,int-width,int-height){
如果(rgb==null)抛出新的NullPointerException();
位图Bitmap=Bitmap.createBitmap(宽度、高度、Bitmap.Config.RGB_565);
设置像素(rgb,0,宽度,0,0,宽度,高度);
返回位图;
}
我在Log.e中总是得到0(“NumFaces:,faces+”);当我在imageView中看到人脸时,真实的人脸和照片中的人脸。我用物理设备试过,2.3(坏相机)和4.2(好相机高清)

编辑:在4.2设备中,我在onPreviewFrame中获得800x480帧


知道会发生什么吗

我通过将设备水平而不是垂直地握在手中解决了问题。

这不是解决方案。