本文主要是介绍AndroidR展讯平台Camera管理众多设置项,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
我们知道,Camera里面有很多设置,比如HDR、Flash、白平衡、倒计时,水印、PictureSize 等等,不仅有在界面上展现出来给用户设置的开关项,还有默认的没有界面展示的比如3DNR、帧率设置等等。那么众多的设置项是如何管理的呢?
先来看下APP中直接管理Settings数据的关系链。
跟我们之前看的CameraAgent那条线十分类似。
在介绍之前,我们先来说下在AndroidCamera2Settings中一个比较重要的成员变量
com.android.ex.camera2.utils.Camera2RequestSettingsSet;
该类前面的包名也说明了该类的路径:
frameworks\ex\camera2\utils\src\com\android\ex\camera2\utils
Camera2RequestSettingsSet 主要是维护了一个Map对象,提供了对这个map的 set、get等操作方法。同时,还提供了一个构建CaptureRequest的方法,这个方法我们在后面还会在提到。
public CaptureRequest createRequest(CameraDevice camera, int template, Surface... targets)throws CameraAccessException {if (camera == null) {throw new NullPointerException("Tried to create request using null CameraDevice");}Builder reqBuilder = camera.createCaptureRequest(template);for (Key<?> key : mDictionary.keySet()) {setRequestFieldIfNonNull(reqBuilder, key);}for (Surface target : targets) {if (target == null) {throw new NullPointerException("Tried to add null Surface as request target");}reqBuilder.addTarget(target);}return reqBuilder.build();}private <T> void setRequestFieldIfNonNull(Builder requestBuilder, Key<T> key) {T value = get(key);if (value != null) {requestBuilder.set(key, value);}}
我们以设置美颜等级为例,来跟踪下UI上操作一个开关,是如何到底层去生效的。
设置美颜等级,在PhotoModule中有接口
@Override
public void onBeautyValueChanged(int[] value) {Log.i(TAG, "onBeautyValueChanged setParameters Value = " + Arrays.toString(value));if (mCameraSettings != null) {mCameraSettings.setSkinWhitenLevel(value);if (mCameraDevice != null) {if (!mFirstHasStartCapture){mCameraDevice.applySettings(mCameraSettings);}}}
}
代码很简洁
- mCameraSettings.setSkinWhitenLevel(value);
- mCameraDevice.applySettings(mCameraSettings);
一,mCameraSettings.setSkinWhitenLevel(value);
追踪这个方法的具体实现是在SprdCameraSettings中
public void setSkinWhitenLevel(String level) {if (level == null) {return;} else if (level.equals("0")) {setSkinWhitenLevel(MAKE_UP_DEFAULT_VALUE);} else {String[] beatuyLevel = level.split(",");for (int i = 0; i < beatuyLevel.length; i++) {mBeatuyLevel[i] = Integer.parseInt(beatuyLevel[i]);}}
}
就是将我们传进来的level保存到 mBeatuyLevel 对象中去。(记住这个mBeatuyLevel ,后面有地方再来拿这个值设置到底层去)
二, mCameraDevice.applySettings(mCameraSettings);
这个是重点了。
mCameraDevice是Agent那条线中 proxy 的内部类。我们在AndroidCamera2ProxyAgent中找到了实现。
@Override
public boolean applySettings(CameraSettings settings) {if (settings == null) {Log.w(TAG, "null parameters in applySettings()");return false;}if (!(settings instanceof AndroidCamera2Settings)) {Log.e(TAG, "Provided settings not compatible with the backing framework API");return false;}// Wait for any state that isn't OPENEDif (applySettingsHelper(settings, ~AndroidCamera2StateHolder.CAMERA_UNOPENED)) {mLastSettings = settings;return true;}return false;
}
关键是 applySettingsHelper 方法的实现。
我们追到到 CameraAgent中的实现
protected boolean applySettingsHelper(CameraSettings settings, final int statesToAwait) {if (settings == null) {Log.v(TAG, "null argument in applySettings()");return false;}if (!getCapabilities().supports(settings)) {Log.w(TAG, "Unsupported settings in applySettings()");return false;}final CameraSettings copyOfSettings = settings.copy();try {getDispatchThread().runJob(new Runnable() {@Overridepublic void run() {CameraStateHolder cameraState = getCameraState();// Don't bother to wait since camera is in bad state./** SPRD fix bug622519 should not wait when state is unopend @{* Original Code*if (cameraState.isInvalid()) {return;}*/if (cameraState.isInvalid() || cameraState.getState() == 1) {return;}/* @} */cameraState.waitForStates(statesToAwait);getCameraHandler().obtainMessage(CameraActions.APPLY_SETTINGS, copyOfSettings).sendToTarget();}});} catch (final RuntimeException ex) {getAgent().getCameraExceptionHandler().onDispatchThreadException(ex);}return true;
}
其实就是发送了 CameraActions.APPLY_SETTINGS 消息。
消息在AndroidCamera2Agent中的Handler处理
case CameraActions.APPLY_SETTINGS: {AndroidCamera2Settings settings = (AndroidCamera2Settings) msg.obj;applyToRequest(settings);break;}
关键代码来了 applyToRequest 方法
private void applyToRequest(AndroidCamera2Settings settings) {// TODO: If invoked when in PREVIEW_READY state, a new preview size will not take effectmPersistentSettings.union(settings.getRequestSettings()); // ****展开1*****mPreviewSize = settings.getCurrentPreviewSize();mPhotoSize = settings.getCurrentPhotoSize();mThumbnailSize = settings.getExifThumbnailSize();mNeedThumb = settings.getNeedThumbCallBack();mIsVideMode = settings.getEnterVideoMode();mIsEISenable = settings.getEOISEnable();mCallbackSize = settings.getCurrentCallbackSize();String slowmotionValue = settings.getCurrentVideoSlowMotion();if (slowmotionValue != null) {mSlowmotion = Integer.parseInt(slowmotionValue);}if (mCameraState.getState() >= AndroidCamera2StateHolder.CAMERA_PREVIEW_ACTIVE) {// If we're already previewing, reflect most settings immediatelytry {if (mCameraState.getState() == CAMERA_RECODERING) {startRecoderRequest();} else {int template = CameraDevice.TEMPLATE_PREVIEW;if (mIsVideMode) {template = CameraDevice.TEMPLATE_RECORD;}ArrayList<Surface> surfaceList = new ArrayList<>();surfaceList.add(mPreviewSurface);if(mCameraState.getState() > AndroidCamera2StateHolder.CAMERA_PREVIEW_READY){if(mCameraProxy.isMotionPhotoOn && mCameraProxy.recordSurfaces != null){for (int i = 0; i< mCameraProxy.recordSurfaces.size(); i++){surfaceList.add(mCameraProxy.recordSurfaces.get(i));}}}Surface[] surfaces = new Surface[surfaceList.size()];surfaceList.toArray(surfaces);mSession.setRepeatingRequest(mPersistentSettings.createRequest(mCamera,template, surfaces),/*listener*/mCameraResultStateCallback, /*handler*/this);// ****展开2*****}} catch (CameraAccessException ex) {Log.e(TAG, "Failed to apply updated request settings", ex);}} else if (mCameraState.getState() < AndroidCamera2StateHolder.CAMERA_PREVIEW_READY) {// If we're already ready to preview, this doesn't regress our statechangeState(AndroidCamera2StateHolder.CAMERA_CONFIGURED);}
}
- mPersistentSettings.union(settings.getRequestSettings()); // 展开1*
这个 settings 就是CameraSettings 的对象,我们来看下其 getRequestSettings 方法
public Camera2RequestSettingsSet getRequestSettings() {updateRequestSettingOrForceToDefault(CONTROL_AE_REGIONS,legacyAreasToMeteringRectangles(mMeteringAreas));updateRequestSettingOrForceToDefault(CONTROL_AF_REGIONS,legacyAreasToMeteringRectangles(mFocusAreas));updateRequestSettingOrForceToDefault(CONTROL_AE_TARGET_FPS_RANGE,new Range(mPreviewFpsRangeMin, mPreviewFpsRangeMax));// TODO: mCurrentPreviewFormatupdateRequestSettingOrForceToDefault(JPEG_QUALITY, mJpegCompressQuality);// TODO: mCurrentPhotoFormatmRequestSettings.set(SCALER_CROP_REGION, mCropRectangle);// TODO: mCurrentZoomIndexupdateRequestSettingOrForceToDefault(CONTROL_AE_EXPOSURE_COMPENSATION,mExposureCompensationIndex);updateRequestFlashMode();updateRequestFocusMode();updateRequestSceneMode();updateRequestWhiteBalance();updateRequestSettingOrForceToDefault(CONTROL_VIDEO_STABILIZATION_MODE,mVideoStabilizationEnabled ?CONTROL_VIDEO_STABILIZATION_MODE_ON : CONTROL_VIDEO_STABILIZATION_MODE_OFF);// OIS shouldn't be on if software video stabilization is.mRequestSettings.set(LENS_OPTICAL_STABILIZATION_MODE,mVideoStabilizationEnabled ? LENS_OPTICAL_STABILIZATION_MODE_OFF :null);updateRequestSettingOrForceToDefault(CONTROL_AE_LOCK, mAutoExposureLocked);updateRequestSettingOrForceToDefault(CONTROL_AWB_LOCK, mAutoWhiteBalanceLocked);// TODO: mRecordingHintEnabledupdateRequestGpsData();if (mExifThumbnailSize != null) {updateRequestSettingOrForceToDefault(JPEG_THUMBNAIL_SIZE,new android.util.Size(mExifThumbnailSize.width(), mExifThumbnailSize.height()));} else {updateRequestSettingOrForceToDefault(JPEG_THUMBNAIL_SIZE, null);}return mRequestSettings;}
我们看到很多 update开头,并且参数里面带有 大写常量的TAG类型 的方法,随便看一个方法的实现
updateRequestFlashMode:
private void updateRequestFlashMode() {Integer aeMode = null;Integer flashMode = null;int flashLcdMode = 0;if (mCurrentFlashMode != null) {switch (mCurrentFlashMode) {case AUTO: {aeMode = CONTROL_AE_MODE_ON_AUTO_FLASH;flashLcdMode = 1;break;}case OFF: {aeMode = CONTROL_AE_MODE_ON;flashMode = FLASH_MODE_OFF;flashLcdMode = 0;break;}case ON: {aeMode = CONTROL_AE_MODE_ON_ALWAYS_FLASH;flashMode = FLASH_MODE_SINGLE;flashLcdMode = 2;break;}case TORCH: {flashMode = FLASH_MODE_TORCH;break;}case RED_EYE: {aeMode = CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE;break;}default: {Log.w(TAG, "Unable to convert to API 2 flash mode: " + mCurrentFlashMode);break;}}}if (mCurrentFlashType == VALUE_FRONT_FLASH_MODE_LED) {//set flash mode for ledmRequestSettings.set(CONTROL_AE_MODE, aeMode);mRequestSettings.set(FLASH_MODE, flashMode);} else {//set flash mode for lcdmRequestSettings.set(SprdCaptureRequest.ANDROID_SPRD_FLASH_LCD_MODE,(byte)flashLcdMode);}}
方法中前面的case判断都是为了后面 将值set到mRequestSettings(Camera2RequestSettingsSet类型)中,依次类推,其他update方法也是将相应的TAG设置到 Camera2RequestSettingsSet 成员变量中。
在SprdAndroidCamera2Settings中复写了 AndroidCamera2Settins中的getRequestSettings方法,主要是为了补充一些TAG的update。
其中我们关注的美颜设置也在其中
requestSettings.set(CONTROL_SKIN_WHITEN_MODE, mBeatuyLevel);
这个mBeatuyLevel便是我们从PhotoModule中传过来的值了。
因此,我们得知,getRequestSettings方法的作用就是将设置项的最新值都更新到 requestSettings(Camera2RequestSettingsSet类型)中。
在回到AndroidCamera2AgentImpl#applyToRequest中,
其中也有一个 Camera2RequestSettingsSet 对象:mPersistentSettings = new Camera2RequestSettingsSet();
mPersistentSettings.union(settings.getRequestSettings());
将我们刚刚更新过得到的 Camera2RequestSettingsSet 对象,通过union方法同步到 mPersistentSettings中。
- // 展开2*
mSession.setRepeatingRequest( mPersistentSettings.createRequest(mCamera, template, surfaces),mCameraResultStateCallback, /*handler*/this
通过在 展开1 中得到的 mPersistentSettings 对象,构建CaptureRequest,然后请求预览。
mPersistentSettings 对象,构建CaptureRequest,我们在本文最开始介绍 Camera2RequestSettingsSet 对象的时候就贴出代码了,现在截取其中的关键部分看下:
public CaptureRequest createRequest(CameraDevice camera, int template, Surface... targets)throws CameraAccessException {......Builder reqBuilder = camera.createCaptureRequest(template);for (Key<?> key : mDictionary.keySet()) {setRequestFieldIfNonNull(reqBuilder, key);}.......}private <T> void setRequestFieldIfNonNull(Builder requestBuilder, Key<T> key) {T value = get(key);if (value != null) {requestBuilder.set(key, value);}}
- 通过CameraDevice对象创建Builder
Builder reqBuilder = camera.createCaptureRequest(template); - 遍历 Camera2RequestSettingsSet 中维护的Map对象,将其中的值依次set到Builder中。当然,这其中就包括我们从PhotoModule中设置过来的美颜等级的key-value: CONTROL_SKIN_WHITEN_MODE, mBeatuyLevel 。
也就是说我们设置项美颜等级成功的set到了CaptureRequest的Builder中了,那么在使用这个CaptureRequest去setRepeatingRequest,新的美颜等级效果就会生效了。
好了,Camera中设置项的管理流程就是这样的了。
这篇关于AndroidR展讯平台Camera管理众多设置项的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!