Android Camera AE和AF的设置

在这里插入图片描述
以geekcamera为例,当关闭闪光灯时,flash mode = 0,代表flash为off模式;
ae mode = 1,代表ae为off模式,flash state = 2,代表flash处于 ready状态,表示没有打闪。
在这里插入图片描述
当闪光灯设置为auto时,是通过设置ae mode 控制的。flash mode = 0,代表flash为off模式;
ae mode = 2,代表ae为on模式,flash state = 2,代表flash处于 ready状态,表示没有打闪;flash state = 3,代表flash处于 fired状态,表示打闪了。在这里插入图片描述
在这里插入图片描述

具体代码逻辑

switch(flash_value) {
	case "flash_off":
		builder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_ON);
		builder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
		break;
	case "flash_auto":
		// note we set this even in fake flash mode (where we manually turn torch on and off to simulate flash) so we
		// can read the FLASH_REQUIRED state to determine if flash is required
	/*if( use_fake_precapture || CameraController2.this.want_expo_bracketing )
		builder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_ON);
	else*/
		builder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_ON_AUTO_FLASH);
		//这里FLASH_MODE可以设置为任意值,因为AE_MODE优先级高
		builder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
		break;
	case "flash_on":
		// see note above for "flash_auto" for why we set this even fake flash mode - arguably we don't need to know
		// about FLASH_REQUIRED in flash_on mode, but we set it for consistency...
	/*if( use_fake_precapture || CameraController2.this.want_expo_bracketing )
		builder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_ON);
	else*/
		builder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
		builder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
		break;
	case "flash_torch":
		builder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_ON);
		builder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
		break;
	case "flash_red_eye":
		// not supported for expo bracketing or burst
		if( CameraController2.this.burst_type != BurstType.BURSTTYPE_NONE )
			builder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_ON);
		else
			builder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE);
		builder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
		break;
	case "flash_frontscreen_auto":
	case "flash_frontscreen_on":
	case "flash_frontscreen_torch":
		builder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_ON);
		builder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
		break;
}

只要flash mode不是auto,并且需要打闪的时候,就需要做预闪(precapture)

boolean needs_flash = capture_result_ae != null && capture_result_ae != CaptureResult.CONTROL_AE_STATE_CONVERGED;
if( camera_settings.flash_value.equals("flash_auto") && !needs_flash ) {
	// if we call precapture anyway, flash wouldn't fire - but we tend to have a pause
	// so skipping the precapture if flash isn't going to fire makes this faster
	if( MyDebug.LOG )
		Log.i(TAG, "flash auto, but we don't need flash");
	call_takePictureAfterPrecapture = true;
}
else {
	call_runPrecapture = true;
}

if( call_runPrecapture ) {
	runPrecapture();
}

runPrecapture逻辑

runPrecapture() {
	final CaptureRequest.Builder precaptureBuilder = mCameraDevice.createCaptureRequest(previewIsVideoMode ? CameraDevice.TEMPLATE_VIDEO_SNAPSHOT : CameraDevice.TEMPLATE_STILL_CAPTURE);
	precaptureBuilder.set(CaptureRequest.CONTROL_CAPTURE_INTENT, CaptureRequest.CONTROL_CAPTURE_INTENT_STILL_CAPTURE);

	camera_settings.setupBuilder(precaptureBuilder, false);
	precaptureBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_IDLE);
	precaptureBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_IDLE);

	precaptureBuilder.addTarget(getPreviewSurface());

	state = STATE_WAITING_PRECAPTURE_START;
	precapture_state_change_time_ms = System.currentTimeMillis();

	// first set precapture to idle - this is needed, otherwise we hang in state STATE_WAITING_PRECAPTURE_START, because precapture already occurred whilst autofocusing, and it doesn't occur again unless we first set the precapture trigger to idle
	if( MyDebug.LOG )
	Log.i(TAG, "capture with precaptureBuilder");
	captureSession.capture(precaptureBuilder.build(), previewCaptureCallback, handler);
	captureSession.setRepeatingRequest(precaptureBuilder.build(), previewCaptureCallback, handler);

	// now set precapture
	precaptureBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_START);
	captureSession.capture(precaptureBuilder.build(), previewCaptureCallback, handler);
}

这里将state 设置为了STATE_WAITING_PRECAPTURE_START,当captureSession.capture()调用完后,会执行到CameraCaptureSession的回调函数onCaptureCompleted(),在此回调函数中我们需要判断AE的状态是否等于CaptureResult.CONTROL_AE_STATE_PRECAPTURE,如果等于,那么执行takePictureAfterPrecapture()
takePictureAfterPrecapture()的逻辑

takePictureAfterPrecapture(){
	stillBuilder = mCameraDevice.createCaptureRequest(previewIsVideoMode ? CameraDevice.TEMPLATE_VIDEO_SNAPSHOT : CameraDevice.TEMPLATE_STILL_CAPTURE);
	stillBuilder.set(CaptureRequest.CONTROL_CAPTURE_INTENT, CaptureRequest.CONTROL_CAPTURE_INTENT_STILL_CAPTURE);
	stillBuilder.setTag(new RequestTagObject(RequestTagType.CAPTURE));
	camera_settings.setupBuilder(stillBuilder, true);
	stillBuilder.addTarget(imageReader.getSurface());
	captureSession.capture(stillBuilder.build(), previewCaptureCallback, handler);
	playSound(MediaActionSound.SHUTTER_CLICK);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值