我正在用camera2制作安卓相机应用程序。我的代码与官方的camera2示例几乎相同,但将片段转换为activity。我计划将zoom设置为默认参数,以便在openCamera(w,h)中使用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);
}
}
使用变焦和相机没有问题。但是当我第一次使用android studio安装我的应用程序并运行应用程序时,它总是在setreapeatingrequest()处出现空点异常。再尝试2或3次后,应用程序运行正常。我怀疑这个异常的发生可能是因为没有预览之类的东西。但我不知道如何修复这个错误。任何意见都必须非常有帮助。提前谢谢。
发布于 2020-05-26 14:30:44
现在有点晚了,但对于那些关心这个问题的人,我自己修复了这个错误。请检查以下内容:
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延迟后打开后,它工作得很好,没有错误。
https://stackoverflow.com/questions/60611654
复制相似问题