Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.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
Java Camera.FaceDetectionListener不工作_Java_Android_Android Camera - Fatal编程技术网

Java Camera.FaceDetectionListener不工作

Java Camera.FaceDetectionListener不工作,java,android,android-camera,Java,Android,Android Camera,我正在使用Android手机的ICECREAMSANDWICH版本。在这方面,我正在检查人脸检测行为。此外,请参考谷歌提供的代码 在使用我的手机进行调试期间,int getMaxNumDetectedFaces()返回3。因此,我的手机支持此功能 那么下面的代码就不起作用了 public void onFaceDetection(Face[] faces, Camera face_camera1) { // TODO Auto-generated method stub

我正在使用Android手机的
ICECREAMSANDWICH
版本。在这方面,我正在检查人脸检测行为。此外,请参考谷歌提供的代码

在使用我的手机进行调试期间,
int getMaxNumDetectedFaces()
返回
3
。因此,我的手机支持此功能

那么下面的代码就不起作用了

public void onFaceDetection(Face[] faces, Camera face_camera1) {
    // TODO Auto-generated method stub
            if(faces.length>0)
            {
                Log.d("FaceDetection","face detected:" +faces.length + "Face 1 location X:"+faces[0].rect.centerX()+"Y:"+faces[0].rect.centerY());
            }
        }

在此faces.length retruning zero中,告诉您一些解决此错误的建议。

我不久前曾使用过FacesDetection。当我在研究onFaceDetection对我来说不起作用时,我找到了另一种方法来研究它

我使用了PreviewCallback,该方法获取每个帧,您可以使用它来识别人脸。这里唯一的问题是格式,默认格式是NV21,您可以通过setPreviewFormat(int)更改它,但这对我也不起作用,因此,我必须进行反转换以获取接收FaceDetector的位图类型。这是我的密码:

public PreviewCallback mPreviewCallback = new PreviewCallback(){
@Override
public void onPreviewFrame(byte[] data, Camera camera) {

  Camera.Size size = camera.getParameters().getPreviewSize();
   Bitmap mfoto_imm = this.getBitmapFromNV21(data, size.width, size.height, true);  //here I get the Bitmap from getBitmapFromNV21 that is the conversion method    
    Bitmap mfoto= mfoto_imm.copy(Bitmap.Config.RGB_565, true); 
    imagen.setImageBitmap(mfoto);
    int alto= mfoto.getHeight();
    int ancho= mfoto.getWidth();
    int count;

   canvas= new Canvas(mfoto);
       dibujo.setColor(Color.GREEN);
   dibujo.setAntiAlias(true);
   dibujo.setStrokeWidth(8);
   canvas.drawBitmap(mfoto, matrix, dibujo);


    FaceDetector mface= new FaceDetector(ancho,alto,1);
    FaceDetector.Face [] face= new FaceDetector.Face[1];
    count = mface.findFaces(mfoto, face);


     PointF midpoint = new PointF();
     int fpx = 0;
         int fpy = 0;

     if (count > 0) {

face[count-1].getMidPoint(midpoint); // you have to take the last one less 1
         fpx= (int)midpoint.x;    // middle pint of  the face in x.
         fpy= (int)midpoint.y;    // middle point of the face in y.

     }

           canvas.drawCircle(fpx, fpy, 10, dibujo); // here I draw a circle on the middle of the face
     imagen.invalidate();
} }

下面是转换方法

public Bitmap getBitmapFromNV21(byte[] data, int width, int height, boolean rotated) {

      Bitmap bitmap = null;
      int[] pixels = new int[width * height];

      // Conver the  array 
      this.yuv2rgb(pixels, data, width, height, rotated);


      if(rotated)
      {
        bitmap = Bitmap.createBitmap(pixels, height, width, Bitmap.Config.RGB_565);
      }
      else
      {
        bitmap = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.RGB_565);
      }

      return bitmap;
    }


public void yuv2rgb(int[] out, byte[] in, int width, int height, boolean rotated) 
           throws NullPointerException, IllegalArgumentException 
         { 
          final int size = width * height; 


          if(out == null) throw new NullPointerException("buffer 'out' == null"); 
          if(out.length < size) throw new IllegalArgumentException("buffer 'out' length < " + size); 
          if(in == null) throw new NullPointerException("buffer 'in' == null"); 
          if(in.length < (size * 3 / 2)) throw new IllegalArgumentException("buffer 'in' length != " + in.length + " < " + (size * 3/ 2)); 

          // YCrCb
          int Y, Cr = 0, Cb = 0;


          int Rn = 0, Gn = 0, Bn = 0;
          for(int j = 0, pixPtr = 0, cOff0 = size - width; j < height; j++) { 
           if((j & 0x1) == 0)
            cOff0 += width;
           int pixPos = height - 1 - j; 
           for(int i = 0, cOff = cOff0; i < width; i++, cOff++, pixPtr++, pixPos += height) { 

            // Get Y
            Y = 0xff & in[pixPtr]; // 0xff es por el signo

            // Get Cr y Cb
            if((pixPtr & 0x1) == 0) { 
             Cr = in[cOff]; 
             if(Cr < 0) Cr += 127; else Cr -= 128;
             Cb = in[cOff + 1]; 
             if(Cb < 0) Cb += 127; else Cb -= 128; 

             Bn = Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6);
             Gn = - (Cb >> 2) + (Cb >> 4) + (Cb >> 5) - (Cr >> 1) + (Cr >> 3) + (Cr >> 4) + (Cr >> 5);
             Rn = Cr + (Cr >> 2) + (Cr >> 3) + (Cr >> 5);
            } 


            int R = Y + Rn; 
            if(R < 0) R = 0; else if(R > 255) R = 255; 
            int B = Y + Bn; 
            if(B < 0) B = 0; else if(B > 255) B = 255; 
            int G = Y + Gn; 
            if(G < 0) G = 0; else if(G > 255) G = 255; //At this point the code could apply some filter From the separate components of the image.For example, they could swap 2 components or remove one

            int rgb = 0xff000000 | (R << 16) | (G << 8) | B; //Depending on the option the output buffer is filled or not applying the transformation
            if(rotated)
             out[pixPos] = rgb; 
            else
             out[pixPtr] = rgb;
           } 
          } 
         }
};
公共位图getBitmapFromNV21(字节[]数据,整数宽度,整数高度,布尔旋转){
位图=空;
int[]像素=新int[宽度*高度];
//变换阵列
yuv2rgb(像素、数据、宽度、高度、旋转);
如果(旋转)
{
bitmap=bitmap.createBitmap(像素、高度、宽度、bitmap.Config.RGB_565);
}
其他的
{
bitmap=bitmap.createBitmap(像素、宽度、高度、bitmap.Config.RGB_565);
}
返回位图;
}
public void yuv2rgb(int[]out,byte[]in,int-width,int-height,布尔旋转)
抛出NullPointerException、IllegalArgumentException
{ 
最终整型尺寸=宽度*高度;
如果(out==null)抛出新的NullPointerException(“buffer'out'==null”);
如果(out.length>1)+(Cb>>2)+(Cb>>6);
Gn=-(Cb>>2)+(Cb>>4)+(Cb>>5)-(Cr>>1)+(Cr>>3)+(Cr>>4)+(Cr>>5);
Rn=Cr+(Cr>>2)+(Cr>>3)+(Cr>>5);
} 
int R=Y+Rn;
如果(R<0)R=0;否则如果(R>255)R=255;
int B=Y+Bn;
如果(B<0)B=0;否则如果(B>255)B=255;
int G=Y+Gn;
如果(G<0)G=0;如果(G>255)G=255;//此时,代码可以从映像的单独组件中应用一些筛选器。例如,它们可以交换两个组件或删除一个组件
int rgb=0xff000000|(R