数码电脑
Android Camera开发入门 第十篇:Camera2 zoom变焦
2024-06-09 05:37  

Android Camera开发入门 第十篇:Camera2 zoom变焦

【小驰笔记】【Android Camera开发】【Android Camera2】【camera2】

本课程内容由 @小驰笔记 出品,欢迎关注,获取更多交流信息~
欢迎访问个人博客:www.xiaochibiji.com

关于数码变焦,前面专门写过一篇文章:

一、获取支持的最大数码变焦倍数

CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM

二、请求裁剪范围

CaptureRequest.SCALER_CROP_REGION

三、示例代码


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);
}

【本文来源于互联网转载,如侵犯您的权益或不适传播,请邮件通知我们删除】

发表评论
0评