2015-09-08 59 views
3

我有一個代碼可以讓相機對焦在屏幕上的觸摸位置。在該左右觸摸位置上正確對焦。但上下觸摸位置無法正常工作。任何一個可以幫助如何解決這個問題如何調整相機api的觸摸對焦區域android

// 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"); 
     } 
    } 
}; 

回答

1

我找到了觸摸對焦的解決方案,我們也需要採取相機的方向。

private void setFocusing(int focusWidth, int focusHeight, int x, int y, int width, int height) {  
    if (mMatrix == null) { 
     mMatrix = new Matrix(); 
     mPreview.setMatrix(width, height, cameraOrientation); 
    } 

    Rect targetFocusRect = new Rect(); 
    calculateTapArea(focusWidth, focusHeight, 1f, x, y, width, height, targetFocusRect); 

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

    Camera.Parameters parameters = mCamera.getParameters(); 
    if (parameters.getMaxNumFocusAreas() > 0) { 
     parameters.setFocusAreas(focusList); 
    } 

    if (parameters.getMaxNumMeteringAreas() > 0) { 
     parameters.setMeteringAreas(focusList); 
    } 

    try { 
     mCamera.setParameters(parameters); 
     if (parameters.getMaxNumFocusAreas() > 0) { 
      mCamera.autoFocus(autoFocus); 
     } 
    } catch (Exception e) { 
     Log.d(TAG, "focusOnTouch : " + e.getLocalizedMessage()); 
    } 
} 

Camera.AutoFocusCallback autoFocus = new Camera.AutoFocusCallback() { 
    @Override 
    public void onAutoFocus(boolean success, Camera camera) { 
     Log.d(TAG, "Focus Status : " + success); 
    } 
}; 

private void calculateTapArea(int focusWidth, int focusHeight, float areaMultiple, int x, int y, int previewWidth, int previewHeight, Rect rect) { 
    int areaWidth = (int) (focusWidth * areaMultiple); 
    int areaHeight = (int) (focusHeight * areaMultiple); 
    int left = clamp(x - areaWidth/2, 0, previewWidth - areaWidth); 
    int top = clamp(y - areaHeight/2, 0, previewHeight - areaHeight); 

    RectF rectF = new RectF(left, top, left + areaWidth, top + areaHeight); 
    mMatrix.mapRect(rectF); 
    rectFToRect(rectF, rect); 
} 

public static int clamp(int x, int min, int max) { 
    if (x > max) return max; 
    if (x < min) return min; 
    return x; 
} 

public static void rectFToRect(RectF rectF, Rect rect) { 
    rect.left = Math.round(rectF.left); 
    rect.top = Math.round(rectF.top); 
    rect.right = Math.round(rectF.right); 
    rect.bottom = Math.round(rectF.bottom); 
} 
+0

我試過使用該方法。但在我的情況下,targetFocusRect正在以許多理由越過邊界。我想在這個問題之後的mMatrix值。你能分享你的mMatrix的用法嗎?所以我可以得到一個線索。 –

+0

你可以看看你爲這個「focusWidth,focusHeight,x,y,width,height」傳遞的值是多少? – Kamalanathan

+0

使用數值(假設爲30)作爲focusWidth nad focusHeight,x,y是觸摸點和寬度,height是相機預覽寬度和高度。我的問題是有些時候我得到了一個超過限制的Rect(-1000:1000)。謝謝 –