处理通过Android中的Camera获得的位图

时间:2012-04-15 01:47:35

标签: android bitmap android-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

谢谢!

0 个答案:

没有答案