2014-06-19 111 views
0

我從設備攝像機的圖像:FaceDetector不檢測人臉

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; 
} 

我在Log.e得到始終爲0( 「NumFaces:」,面對+ 「」);雖然我在imageView的臉上看到真實的臉和照片中的臉。我試圖用物理設備,2.3(壞相機)和4.2(好相機高清)

編輯:在4.2設備我在onPreviewFrame獲得800x480幀。

任何想法會發生什麼?

回答

0

我已經通過水平而不是垂直地將設備保持在我的手中來解決了問題。