0

我无法触摸以在 Camera2 API 上正常工作。触摸时,我似乎只集中了一秒钟,然后变得非常模糊。手机是 Nexus 5X。这是我的触控代码。

private void refocus(MotionEvent event, View view){

    //Handler for autofocus callback
    CameraCaptureSession.CaptureCallback captureCallbackHandler = new CameraCaptureSession.CaptureCallback() {
        @Override
        public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
            super.onCaptureCompleted(session, request, result);

            if (request.getTag() == "FOCUS_TAG") {
                //the focus trigger is complete -
                //resume repeating (preview surface will get frames), clear AF trigger
                previewRequest.set(CaptureRequest.CONTROL_AF_TRIGGER, null);
                try{
                    mSession.setRepeatingRequest(previewRequest.build(), null, null);}
                catch (Exception e){

                }
            }
        }

        @Override
        public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request, CaptureFailure failure) {
            super.onCaptureFailed(session, request, failure);
            Log.e(TAG, "Manual AF failure: " + failure); }
    };

    try {
        final Rect sensorArraySize = manager.getCameraCharacteristics(mCameraDevice.getId()).get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);

        //Find area size
        int x = (int)(event.getX()/(float)view.getWidth() * (float)sensorArraySize.width());
        int y = (int)(event.getY()/(float)view.getHeight() * (float)sensorArraySize.height());
        final int halfTouchWidth  = 150; //(int)motionEvent.getTouchMajor(); //TODO: this doesn't represent actual touch size in pixel. Values range in [3, 10]...
        final int halfTouchHeight = 150; //(int)motionEvent.getTouchMinor();
        MeteringRectangle rect = new MeteringRectangle(Math.max(x - halfTouchWidth,  0),
                Math.max(y - halfTouchHeight, 0),
                halfTouchWidth  * 2,
                halfTouchHeight * 2,
                MeteringRectangle.METERING_WEIGHT_MAX - 1);
        mSession.stopRepeating();
        transparentLayer.drawFeedback(rect);

        //Cancel requests
        previewRequest.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
        previewRequest.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF);
        mSession.capture(previewRequest.build(), captureCallbackHandler, null);

        //Now add a new AF trigger with focus region
        if (isMeteringAreaAFSupported()) {
            previewRequest.set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{rect});
        }
        previewRequest.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
        previewRequest.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
        previewRequest.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
        previewRequest.setTag("FOCUS_TAG"); //we'll capture this later for resuming the preview

        //then we ask for a single request (not repeating!)
        mSession.capture(previewRequest.build(), captureCallbackHandler, null);

    }catch (Exception e){
        e.printStackTrace();
    }

}

还有另一个辅助功能:

private boolean isMeteringAreaAFSupported() {
    try {
        return manager.getCameraCharacteristics(mCameraDevice.getId()).get(CameraCharacteristics.CONTROL_MAX_REGIONS_AF) >= 1;
    }catch (Exception e){
        return false;
    }
}

焦点在短时间内工作,然后重新启动或变得完全模糊的可能原因是什么?我找不到任何有用的解决方案。

谢谢大家!

4

1 回答 1

0

我会尝试在 onCaptureCompleted 中将 AF_TRIGGER 设置为 IDLE - 完全删除它并不是完全指定的。

除此之外,我不清楚您如何将屏幕触摸坐标转换为测光区域的相机活动阵列坐标。看起来你假设坐标是相同的,这是不正确的。这不应该导致模糊,但会导致您专注于与您想象的不同的区域。

您需要正确缩放 x 和 y(基于在使用数字缩放时定义可见视野的当前裁剪区域以及活动阵列矩形)

于 2018-08-23T19:27:11.307 回答