【小驰笔记】【Android Camera开发】【Android Camera2】【camera2】
本课程内容由 @小驰笔记 出品,欢迎关注,获取更多交流信息~
欢迎访问个人博客:www.xiaochibiji.com
关于数码变焦,前面专门写过一篇文章:
https://mp.weixin.qq.com/s/XZzQytKVz-F0sUMW61FFRQ
CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM
CaptureRequest.SCALER_CROP_REGION
/**
* 进行镜头缩放
* @param zoom 缩放系数(0~1.0)
**/
public void applyZoom(float zoom) {
float old = mZoomValue;
mZoomValue = zoom;
if(mCameraCharacteristics != null){
float maxZoom = mCameraCharacteristics.get(
CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM);
// converting 0.0f-1.0f zoom scale to the actual camera digital zoom scale
// (which will be for example, 1.0-10.0)
float calculatedZoom = (mZoomValue * (maxZoom - 1.0f)) + 1.0f;
Rect newRect = getZoomRect(calculatedZoom, maxZoom);
mPreviewBuilder.set(CaptureRequest.SCALER_CROP_REGION, newRect);
mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), null, mBackgroundHandler);
}
}
/**
* 获取缩放矩形
**/
private Rect getZoomRect(float zoomLevel, float maxDigitalZoom) {
Rect activeRect = new Rect();
activeRect = mCameraCharacteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
int minW = (int) (activeRect.width() / maxDigitalZoom);
int minH = (int) (activeRect.height() / maxDigitalZoom);
int difW = activeRect.width() - minW;
int difH = activeRect.height() - minH;
// When zoom is 1, we want to return new Rect(0, 0, width, height).
// When zoom is maxZoom, we want to return a centered rect with minW and minH
int cropW = (int) (difW * (zoomLevel - 1) / (maxDigitalZoom - 1) / 2F);
int cropH = (int) (difH * (zoomLevel - 1) / (maxDigitalZoom - 1) / 2F);
returnnew Rect(cropW, cropH, activeRect.width() - cropW,
activeRect.height() - cropH);
}
推荐阅读:
深圳上班,
从事Android Camera相关软件开发工作,
记录生活和工作的点滴,
点击关注“小驰笔记”,期待和你相遇~