首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >安卓Camera2接口setRepeatingRequest

安卓Camera2接口setRepeatingRequest
EN

Stack Overflow用户
提问于 2020-03-10 12:42:51
回答 1查看 371关注 0票数 0

我正在用camera2制作安卓相机应用程序。我的代码与官方的camera2示例几乎相同,但将片段转换为activity。我计划将zoom设置为默认参数,以便在openCamera(w,h)中使用zoom函数。下面是我设置相机默认变焦的代码。

代码语言:javascript
运行
复制
private void openCamera(int width, int height) {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        requestCameraPermission();
        return;
    }
    setUpCameraOutputs(width, height);
    configureTransform(width, height);
    Activity activity = this;
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        if (!cameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
            throw new RuntimeException("Time out waiting to lock camera opening.");
        }
        manager.openCamera(cameraId, stateCallback, backgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
    }

    try{
        CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
        float maxzoom = (characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM));
        android.graphics.Rect m = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
        float zoomLevel = maxzoom/3;
        float ratio = (float) 1/zoomLevel;
        int cropWidth = m.width() - Math.round((float)m.width()*ratio);
        int cropHeight = m.height() - Math.round((float)m.height()*ratio);
        android.graphics.Rect zoom = new android.graphics.Rect(cropWidth/2, cropHeight/2, m.width() - cropWidth/2, m.height() - cropHeight/2);
        previewRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom);
        try{
            captureSession.setRepeatingRequest(previewRequestBuilder.build(), captureCallback, null); // Null Exception occur
        } catch (CameraAccessException e) {
            e.printStackTrace();
        } catch (NullPointerException ex) {
            ex.printStackTrace();
        }
    } catch (CameraAccessException e) {
        throw new RuntimeException("can not access camera.", e);
    }

}

使用变焦和相机没有问题。但是当我第一次使用android studio安装我的应用程序并运行应用程序时,它总是在setreapeatingrequest()处出现空点异常。再尝试2或3次后,应用程序运行正常。我怀疑这个异常的发生可能是因为没有预览之类的东西。但我不知道如何修复这个错误。任何意见都必须非常有帮助。提前谢谢。

EN

Stack Overflow用户

回答已采纳

发布于 2020-05-26 14:30:44

现在有点晚了,但对于那些关心这个问题的人,我自己修复了这个错误。请检查以下内容:

代码语言:javascript
运行
复制
delayHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            try {
                CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
                float maxzoom = (characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM));
                android.graphics.Rect m = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
                float zoomLevel = maxzoom / 6;
                float ratio = (float) 1 / zoomLevel;
                int cropWidth = m.width() - Math.round((float) m.width() * ratio);
                int cropHeight = m.height() - Math.round((float) m.height() * ratio);
                android.graphics.Rect zoom = new android.graphics.Rect(cropWidth / 2, cropHeight / 2, m.width() - cropWidth / 2, m.height() - cropHeight / 2);
                previewRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom);
                try {
                    captureSession.setRepeatingRequest(previewRequestBuilder.build(), captureCallback, null);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                } catch (NullPointerException ex) {
                    ex.printStackTrace();
                }
            } catch (CameraAccessException e) {
                throw new RuntimeException("can not access camera.", e);
            }
        }
    }, 500);

因为我的相机是用来变焦的,所以需要一些时间来准备相机。在我使相机在500ms延迟后打开后,它工作得很好,没有错误。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60611654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档