gpt4 book ai didi

Android camera2 api触摸对焦示例?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:03:06 24 4
gpt4 key购买 nike

您好,我正在使用 camera2basic 示例来实现我的 camera2 应用程序。我找不到任何用 camera2 api 实现触摸对焦的好例子。目前我用于触摸对焦的代码是这样的:

    private void setFocusArea(MotionEvent event) {
if (mCameraId == null) return;
CameraManager cm = (CameraManager)getActivity().getSystemService(Context.CAMERA_SERVICE);
CameraCharacteristics cc = null;
try {
cc = cm.getCameraCharacteristics(mCameraId);
} catch (CameraAccessException e) {
e.printStackTrace();
}

int myX = (int)event.getX();
int myY = (int)event.getY();
MeteringRectangle focusArea = new MeteringRectangle(myX-100,myY-100,200,200,MeteringRectangle.METERING_WEIGHT_DONT_CARE);
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
try {
mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
mBackgroundHandler);
// After this, the camera will go back to the normal state of preview.
mState = STATE_PREVIEW;
} catch (CameraAccessException e){
// log
}

if (isMeteringAreaAESupported(cc)) {
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_REGIONS,
new MeteringRectangle[]{focusArea});
}
if (isMeteringAreaAFSupported(cc)) {
mPreviewRequestBuilder
.set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{focusArea});
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_AUTO);
}
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CameraMetadata.CONTROL_AF_TRIGGER_START);
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_START);
try {
mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback,
mBackgroundHandler);
mManualFocusEngaged = true;
} catch (CameraAccessException e) {
// error handling
}
}

但问题是它表现出奇怪的行为,自动闪光灯会无限次地重复自动对焦序列,而且它似乎没有对焦在触摸区域上。我试着改变

mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);

到:

mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);

这停止了重复的自动对焦序列,但它仍然没有对焦在触摸区域上,闪光灯只闪烁了不到一秒钟,而不是正常的对焦序列。请帮助我解决这个问题,或者指导我使用工作方式来集中注意力的例子。谢谢

最佳答案

您的问题是设置 AF 区域的控制。

  1. 计算出你想设置焦点的区域
  2. 停止当前 session mPreviewSession.stopRepeating()
  3. 启动自动对焦触发!!!

3.1。安全地开始使 AF 区域 IDLE

3.2。然后启动自动对焦触发

        mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_IDLE);
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
  1. 捕捉一次以应用您的设置

  2. 检查是否支持AF和AE区域如果支持则应用该区域

    if ( isMeteringAreaAESupported()) {
    //System.out.println("AE regions are supported");
    mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_REGIONS, new MeteringRectangle[]{focusArea});
    }
    if ( isMeteringAreaAFSupported()) {
    //System.out.println("AF regions are supported");

    mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{focusArea});
    mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
    }
  3. 再次捕捉一次以设置焦点

     mPreviewCaptureSession.capture(mCaptureRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);
  4. mCaptureCallback 中您应该取消 AF 触发器,但文档说 AF 触发器在某些设备中可以为空,所以我喜欢

    mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_IDLE);
    mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
    mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, null);
  5. 最后的事情是 mPreviewCaptureSession.setRepeatingRequest(mCaptureRequestBuilder.build(), null, mBackgroundHandler);

编辑

这是工作示例

private void setFocusArea(int focus_point_x, int focus_point_y) throws CameraAccessException {

if (cameraId == null || mManualFocusEngaged) return;

if (mCameraManager == null){
mCameraManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
}

MeteringRectangle focusArea = null;

if (mCameraManager != null) {
if (mCameraCharacteristics == null) {
mCameraCharacteristics = mCameraManager.getCameraCharacteristics(cameraId);
}

final Rect sensorArraySize = mCameraCharacteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);

int y = focus_point_x;
int x = focus_point_y;

if (sensorArraySize != null) {
y = (int)(((float)focus_point_x / currentWidth) * (float)sensorArraySize.height());
x = (int)(((float)focus_point_y / currentHeight) * (float)sensorArraySize.width());
}

final int halfTouchLength = 150;
focusArea = new MeteringRectangle(Math.max(x - halfTouchLength, 0),
Math.max(y - halfTouchLength, 0),
halfTouchLength * 2,
halfTouchLength * 2,
MeteringRectangle.METERING_WEIGHT_MAX - 1);
}

CameraCaptureSession.CaptureCallback mCaptureCallback = new CameraCaptureSession.CaptureCallback() {

@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);

mManualFocusEngaged = false;

if (request.getTag().equals(FOCUS_TAG)) { // previously getTag == "Focus_tag"
//the focus trigger is complete -
//resume repeating (preview surface will get frames), clear AF trigger
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_IDLE);
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, null);// As documentation says AF_trigger can be null in some device
try {
mCurrentCameraCaptureSession.setRepeatingRequest(mCaptureRequestBuilder.build(), null, mBackgroundHandler);
} catch (CameraAccessException e) {
// error handling
}
}
}

@Override
public void onCaptureFailed(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull CaptureFailure failure) {
super.onCaptureFailed(session, request, failure);
mManualFocusEngaged = false;
}

};

mCurrentCameraCaptureSession.stopRepeating(); // Destroy current session
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_IDLE);
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
mCurrentCameraCaptureSession.capture(mCaptureRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); //Set all settings for once

if ( isMeteringAreaAESupported()) {
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_REGIONS, new MeteringRectangle[]{focusArea});
}
if ( isMeteringAreaAFSupported()) {
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{focusArea});
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
}

mCaptureRequestBuilder.setTag(FOCUS_TAG); //it will be checked inside mCaptureCallback
mCurrentCameraCaptureSession.capture(mCaptureRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);

mManualFocusEngaged = true;
}




private boolean isMeteringAreaAFSupported() { // AF stands for AutoFocus

Integer afRegion = mCameraCharacteristics.get(CameraCharacteristics.CONTROL_MAX_REGIONS_AF);
return afRegion != null && afRegion >= 1;

}


private boolean isMeteringAreaAESupported() {//AE stands for AutoExposure

Integer aeState = mCameraCharacteristics.get(CameraCharacteristics.CONTROL_MAX_REGIONS_AE);
return aeState!=null && aeState >=1;

}

希望对您有所帮助。享受编码

关于Android camera2 api触摸对焦示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41649691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com