简体   繁体   English

Android Camera2 API setRepeatingRequest

[英]Android Camera2 API setRepeatingRequest

I'm making android camera app with camera2.我正在使用camera2制作android相机应用程序。 my code is almost same with official camera2 sample but transfer fragment to activity.我的代码与官方camera2示例几乎相同,但将片段转移到活动中。 I plan to set zoom as default parameter so made zoom function in openCamera(w, h).我计划将缩放设置为默认参数,以便在 openCamera(w, h) 中创建缩放功能。 Below is my code for set camera default zoom.下面是我设置相机默认缩放的代码。

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

}

There's no problem with zoom and camera.变焦和相机没有问题。 But when I install my app with android studio and run app for the first time, it always break with null point exception at setreapeatingrequest().但是当我用 android studio 安装我的应用程序并第一次运行应用程序时,它总是在 setreapeatingrequest() 处以空点异常中断。 After 2 or 3 more try then the app runs ok.再尝试 2 或 3 次后,应用程序运行正常。 I suspect that this exception maybe occurs 'cause there's no preview or something.我怀疑可能会发生此异常,因为没有预览或其他内容。 But I have no idea how can I fix this error.但我不知道如何解决这个错误。 Any opinion must be very helpful.任何意见都必须非常有帮助。 Thanks in advance.提前致谢。

It's bit late but for those who concern, I fixed this error by myself.有点晚了,但对于那些关心的人,我自己修复了这个错误。 Check this :检查这个:

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

Because I made camea to zoom, so it takes some times to prepare camera.因为我做了camea变焦,所以准备相机需要一些时间。 After I made camera opened after 500ms delay it worked perfect without error.在我在 500 毫秒延迟后打开相机后,它完美无误地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM