在android中的触摸屏上拍摄图像

时间:2014-08-05 16:02:17

标签: android

我正在关注谷歌的Android开发者指南,以创建相机捕捉应用。 http://developer.android.com/guide/topics/media/camera.html

我创建了CameraPreview类来显示来自相机的实时图像。然后将相机连接到它上面,如下所示

// Create an instance of Camera
    mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);

在示例中,用户按下按钮时单击图像,如下所示: -

Button captureButton = (Button) findViewById(id.button_capture);
captureButton.setOnClickListener(
 new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // get an image from the camera
        mCamera.takePicture(null, null, mPicture);
    }
}

);

我可以以某种方式将触摸监听器设置为相机预览(屏幕),这样当用户触摸屏幕时,我可以获得触摸的坐标,然后进行单独的处理吗?

1 个答案:

答案 0 :(得分:0)

raplace:

Button captureButton = (Button) findViewById(id.button_capture);
captureButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // get an image from the camera
        mCamera.takePicture(null, null, mPicture);
    }
});

使用:

preview.setOnTouchListener(new OnTouchListener(){

@Override
public boolean onTouch(View v, MotionEvent event) {     
Log.i("onTouch","onTouch event:"+event.getAction());

switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:

        // get an image from the camera
        mCamera.takePicture(null, null, mPicture);

        // coordinates
        int x = (int)event.getX();
        int y = (int)event.getY();
        Log.i("onTouch", "ACTION_DOWN");
        break;

    default:
        break;
    }
    return true;
}

});