聚焦相机,无需触摸交互

时间:2018-05-17 15:02:55

标签: java android autofocus

是否可以在没有任何触摸交互的情况下在Android相机应用程序中设置焦点区域,而是通过网络接收的坐标?

更新 到目前为止,我尝试了以下代码片段:

public void doFocus(final Rect focusRect) {

    try {
        if (camera != null) {
            List<Area> focusList = new ArrayList<Area>();
            Area focusArea = new Area(focusRect, 1000);
            focusList.add(focusArea);
            Parameters param = camera.getParameters();
            //param.setFocusMode(Parameters.FOCUS_MODE_AUTO);
            Log.d(TAG, param.getMaxNumFocusAreas() + ";" + param.getMaxNumMeteringAreas() + " >> " + focusRect.toString());
            param.setFocusAreas(focusList);
            param.setMeteringAreas(focusList);
            camera.setParameters(param);
            camera.autoFocus(autoFocusCallback);
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.i(TAG, "Unable to autofocus");
    }
}

此方法在此处调用:

// Fixation of focus with touch
public void setCoordinates() {
    float x = new MainActivity().coordinates().get(0);
    float y = 1490f - (new MainActivity().coordinates().get(1));         //previewHeight minus input to get y coord in Android

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


    final Rect targetFocusRect = new Rect(
            rect.left * 2000 / previewWidth - 1000,
            rect.top * 2000 / previewHeight - 1000,
            rect.right * 2000 / previewWidth - 1000,
            rect.bottom * 2000 / previewHeight - 1000);

    out.println("FINAL RECT:" + targetFocusRect);

    cameraPreview.doFocus(targetFocusRect);

}

此代码适用于触摸输入,但由于我修改了它并仅放置特定坐标,因此不会设置焦点。

1 个答案:

答案 0 :(得分:0)

您可能需要自动对焦(非手动),但在程序设置区域,无需用户输入。这是可能的:

Parameters parameters = camera.getParameters();
parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
parameters.setFocusAreas(Lists.newArrayList(new Camera.Area(focusRect, 1000)));

camera.setParameters(parameters);
camera.autoFocus(this);

对于网络部分,您将在线找到足够的资源:)