Android 11.0 Camera2 默认选择拍照尺寸修改及流程分析

您所在的位置:网站首页 相机拍摄图片大小怎么调 Android 11.0 Camera2 默认选择拍照尺寸修改及流程分析

Android 11.0 Camera2 默认选择拍照尺寸修改及流程分析

2024-04-02 05:52| 来源: 网络整理| 查看: 265

一、前言

最近有一个需求是修改默认的照片尺寸,这篇文章就总结一下Camara 选择默认的拍照尺寸的逻辑吧。

二、定位代码

定位图片

2.1、根据“照片大小” text 可以定位到布局文件 picturesize_preference.xml.

代码路径: android\vendor\mediatek\proprietary\packages\apps\Camera2\feature\setting\picturesize\res\xml\picturesize_preference.xml`**

Java 引用文件为: 代码路径: android\vendor\mediatek\proprietary\packages\apps\Camera2\feature\setting\picturesize\src\com\mediatek\camera\feature\setting\picturesize\PictureSizeSettingView.java

三、流程逻辑分析 3.1、PictureSizeSettingView 照片尺寸列表值的初始化

PictureSizeSettingView 的 setEntryValues 是传入照片尺寸列表的,这里是在其他地方调用传入值得。 代码路径: android\vendor\mediatek\proprietary\packages\apps\Camera2\feature\setting\picturesize\src\com\mediatek\camera\feature\setting\picturesize\PictureSizeSettingView.java

/** * Set the picture sizes supported. * * @param entryValues The picture sizes supported. */ public void setEntryValues(List entryValues) { mEntryValues = entryValues; } 3.2、PictureSize.java 设置默认尺寸和可选尺寸

全局搜一下可以发现是在PictureSize.java 中传入EntryValue的 代码路径: vendor/mediatek/proprietary/packages/apps/Camera2/feature/setting/picturesize/src/com/mediatek/camera/feature/setting/picturesize/PictureSize.java

总结一下流程:

1、获取屏幕的尺寸来计算出宽高比作为全屏的宽高比。 2、获取Sensor 传上来的可支持的照片尺寸supportedPictureSize。 3、从supportedPictureSize这里遍历选出可以用的尺寸里面最接近全屏宽高比的尺寸。 4、将全屏宽高比里面尺寸作为默认选中的尺寸。

/** * Invoked after setting's all values are initialized. * * @param supportedPictureSize Picture sizes which is supported in current platform. */ public void onValueInitialized(List supportedPictureSize) { LogHelper.d(TAG, "[onValueInitialized], supportedPictureSize:" + supportedPictureSize); //1、这里获取屏幕的尺寸来计算出宽高比作为全屏的宽高比。 double fullRatio = PictureSizeHelper.findFullScreenRatio(mActivity); List desiredAspectRatios = new ArrayList(); desiredAspectRatios.add(fullRatio); desiredAspectRatios.add(PictureSizeHelper.RATIO_4_3); PictureSizeHelper.setDesiredAspectRatios(desiredAspectRatios); PictureSizeHelper.setFilterParameters(DEGRESSIVE_RATIO, MAX_COUNT); if (sFilterPictureSize) { supportedPictureSize = PictureSizeHelper.filterSizes(supportedPictureSize); LogHelper.d(TAG, "[onValueInitialized], after filter, supportedPictureSize = " + supportedPictureSize); } ... /* **已省略部分代码...... */ ... //2、获取Sensor 传上来的可支持的照片尺寸supportedPictureSize。 setSupportedPlatformValues(supportedPictureSize); setSupportedEntryValues(supportedPictureSize); setEntryValues(supportedPictureSize); refreshViewEntry(); String valueInStore = mDataStore.getValue(getKey(), null, getStoreScope()); Log.d("PictureSize 1,","onValueInitialized valueInStore="+valueInStore); if (valueInStore != null && !supportedPictureSize.contains(valueInStore)) { LogHelper.d(TAG, "[onValueInitialized], value:" + valueInStore + " isn't supported in current platform"); valueInStore = null; mDataStore.setValue(getKey(), null, getStoreScope(), false); } if (valueInStore == null) { // Default picture size is the max full-ratio size. List entryValues = getEntryValues(); //3、这里遍历选出可以用的尺寸里面最接近全屏宽高比的尺寸。 for (String value : entryValues) { if (PictureSizeHelper.getStandardAspectRatio(value) == fullRatio) { valueInStore = value; break; } } } // If there is no full screen ratio picture size, use the first value in // entry values as the default value. if (valueInStore == null) { valueInStore = getEntryValues().get(0); } Log.d("PictureSize 2,","onValueInitialized valueInStore="+valueInStore); //4、将全屏宽高比里面尺寸作为默认选中的尺寸。 setValue(valueInStore); } 四、Any question ?

1、全屏的宽高比如何计算的 ? 2、默认的尺寸如果全屏宽高比有好几个怎么选择 ?

4.1、全屏的宽高比如何计算的

在上面提到获取全屏宽高比的地方是 PictureSizeHelper.findFullScreenRatio

//1、这里获取屏幕的尺寸来计算出宽高比作为全屏的宽高比。 double fullRatio = PictureSizeHelper.findFullScreenRatio(mActivity);

代码路径: android\vendor\mediatek\proprietary\packages\apps\Camera2\feature\setting\picturesize\src\com\mediatek\camera\feature\setting\picturesize\PictureSizeHelper.java

在这里可以很清楚的看到计算全屏宽高比的步骤。

1、首选通过Display获取屏幕的尺寸。 2、计算屏幕尺寸的宽高比。 3、根据绝对值函数选取最靠近屏幕宽高比的数值作为全屏宽高比。

/** * Compute full screen aspect ratio. * * @param context The instance of {@link Context}. * @return The full screen aspect ratio. */ public static double findFullScreenRatio(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); //1、首选通过Display获取屏幕的尺寸。 Display display = wm.getDefaultDisplay(); DisplayMetrics dm = new DisplayMetrics(); display.getRealMetrics(dm); int width = Math.max(dm.widthPixels, dm.heightPixels); int height = Math.min(dm.widthPixels, dm.heightPixels); //2、计算屏幕尺寸的宽高比。 double displayRatio = (double) width / (double) height; Log.d("PictureSizeHelper",",findFullScreenRatio width="+width+",height="+height+",displayRatio="+displayRatio); double find = RATIO_4_3; //3、根据绝对值函数选取最靠近屏幕宽高比的数值作为全屏宽高比。 for (int i = 0; i maxSize) { + maxSize = temp; + maxIndex = i; + } + } + return supportedEntryValues.get(maxIndex); + } + //add end. + private static List pickUpToThree(List sizes) { if (sDegressiveRatio == 0 || sMaxCount == 0) { return sizes; 六、结语

Okay,So much for this !



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3