0

我从 camera 获得了位图。现在我希望在拍摄的照片中检测人脸。我已经编写了相同的代码。获得 Bitmap 后,我使用 FaceDetector.face 检测图像中的人脸,但每次尝试在模拟器上运行程序时程序都会崩溃。

这是我的代码。

public class Main extends Activity {
    /** Called when the activity is first created. */
    final int CAMERA_PIC_REQUEST = 1337; 
    Bitmap new1Bitmap;
    private int imageWidth, imageHeight;
    private int numberOfFace = 1;
    private FaceDetector myFaceDetect; 
    private FaceDetector.Face[] myFace;
    float myEyesDistance;
    int numberOfFaceDetected;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode,Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST) {
            // do something
            new1Bitmap = (Bitmap) data.getExtras().get("data");  // get the Image from Camera
            imageWidth = new1Bitmap.getWidth();
            imageHeight = new1Bitmap.getHeight();
            myFace = new FaceDetector.Face[numberOfFace];
            myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
            numberOfFaceDetected = myFaceDetect.findFaces(new1Bitmap, myFace); 

            ImageView image = (ImageView) findViewById(R.id.imageView1);
            //image.setImageBitmap(new1Bitmap); 
            Canvas canvas = null; 
            canvas.drawBitmap(new1Bitmap, 0, 0, null);
            Paint myPaint = new Paint();
            myPaint.setColor(Color.RED);
            myPaint.setStyle(Paint.Style.STROKE);
            myPaint.setStrokeWidth(5);

            for(int i=0; i < numberOfFaceDetected; i++)
            {
                Face face = myFace[i];
                PointF lEye=new PointF();
                PointF rEye=new PointF();

                PointF myMidPoint = new PointF();
                face.getMidPoint(myMidPoint);
                myEyesDistance = face.eyesDistance();
                canvas.drawCircle((float)(myMidPoint.x-myEyesDistance/2.0), myMidPoint.y, (float)20.0, myPaint);
                canvas.drawCircle((float)(myMidPoint.x+myEyesDistance/2.0), myMidPoint.y, (float)20.0, myPaint);
            }
        }
    }
}

谁能指出这段代码中的错误?

logcat 中的错误

04-14 23:12:02.052: E/AndroidRuntime(941): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1337, result=-1, data=Intent { act=inline-data (has extras) }} to activity {live.face.detection/live.face.detection.Main}: java.lang.NullPointerException

谢谢 !

4

0 回答 0