Android 相机 2 API setRepeatingRequest
Android Camera2 API setRepeatingRequest
我正在使用 camera2 制作 android 相机应用。
我的代码与官方 camera2 示例几乎相同,但将片段传输到 activity。
我计划将缩放设置为默认参数,以便在 openCamera(w, h) 中实现缩放功能。下面是我设置相机默认缩放的代码。
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 次后,应用 运行 就可以了。
我怀疑这个异常可能会发生,因为没有预览或其他东西。但我不知道如何解决这个错误。
任何意见都必须非常有帮助。提前致谢。
有点晚了但是对于那些担心的人,我自己修复了这个错误。
检查这个:
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);
因为我制作了camea来变焦,所以准备相机需要一些时间。
在我让相机在 500 毫秒延迟后打开后,它完美无误地工作。
我正在使用 camera2 制作 android 相机应用。 我的代码与官方 camera2 示例几乎相同,但将片段传输到 activity。 我计划将缩放设置为默认参数,以便在 openCamera(w, h) 中实现缩放功能。下面是我设置相机默认缩放的代码。
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 次后,应用 运行 就可以了。 我怀疑这个异常可能会发生,因为没有预览或其他东西。但我不知道如何解决这个错误。 任何意见都必须非常有帮助。提前致谢。
有点晚了但是对于那些担心的人,我自己修复了这个错误。 检查这个:
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);
因为我制作了camea来变焦,所以准备相机需要一些时间。 在我让相机在 500 毫秒延迟后打开后,它完美无误地工作。