如何在相机api android中调整触摸对焦区域

时间:2015-09-08 07:06:56

标签: android android-camera

我有一个代码可以将相机聚焦到屏幕上的触摸位置。在该左右触摸位置正确地对准触摸。但是上下触摸位置无法正常工作。任何人都可以帮助解决这个问题

 // Surface view on touch
 @Override
 public boolean onTouchEvent(MotionEvent event) {
     ((CameraActivity) getContext()).touchFocus(event);
     return true;
 }

public void touchFocus(MotionEvent event) {

    if (event.getAction() != MotionEvent.ACTION_DOWN) {
        return;
    }

    float x = event.getX();
    float y = event.getY();


    Rect touchRect = new Rect(
            (int) (x - 100),
            (int) (y - 100),
            (int) (x + 100),
            (int) (y + 100));

    final Rect targetFocusRect = new Rect(
            touchRect.left * 2000 /mPreview.getWidth()  - 1000,
            ((touchRect.top * 2000) /mPreview.getHeight()) - 1000,
            touchRect.right * 2000 /mPreview.getWidth() - 1000,
            ((touchRect.bottom * 2000) /mPreview.getHeight()) - 1000);        

    final List<Camera.Area> focusList = new ArrayList<Camera.Area>();
    Camera.Area focusArea = new Camera.Area(targetFocusRect, 1000);
    focusList.add(focusArea);

    Camera.Parameters para = mCamera.getParameters();
    para.setFocusAreas(focusList);
    para.setMeteringAreas(focusList);
    try {
        mCamera.setParameters(para);
        mCamera.autoFocus(mAutoFocusTakePictureCallback);
    } catch (Exception e) {
        AppController.log(TAG, "focusOnTouch : " + e.getLocalizedMessage());
    }
}

private Camera.AutoFocusCallback mAutoFocusTakePictureCallback = new Camera.AutoFocusCallback() {
    @Override
    public void onAutoFocus(boolean success, Camera camera) {
        if (success) {
            AppController.log(TAG, "Success");
        } else {
            AppController.log(TAG, "Failed");
        }
    }
};

0 个答案:

没有答案