gpt4 book ai didi

android - Surface 没有适用于 Camera2 的有效原生 Surface

转载 作者:行者123 更新时间:2023-11-30 00:02:48 32 4
gpt4 key购买 nike

我已经将 camera2 与 textureVIew 集成在一起。它适用于所有设备,但在平板电脑上,当我们第一次捕获图像时,它会崩溃并显示以下日志。

 Fatal Exception: java.lang.IllegalArgumentException: Surface had no valid native Surface.
at android.hardware.camera2.legacy.LegacyCameraDevice.nativeGetSurfaceId(LegacyCameraDevice.java)
at android.hardware.camera2.legacy.LegacyCameraDevice.getSurfaceId(LegacyCameraDevice.java:658)
at android.hardware.camera2.legacy.LegacyCameraDevice.containsSurfaceId(LegacyCameraDevice.java:678)
at android.hardware.camera2.legacy.RequestThreadManager$2.onPictureTaken(RequestThreadManager.java:220)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1248)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.hardware.camera2.legacy.CameraDeviceUserShim$CameraLooper.run(CameraDeviceUserShim.java:136)
at java.lang.Thread.run(Thread.java:818)

以下代码用于捕获图像。

 protected void takePicture() {
if (getContext() == null || cameraDevice == null) return;

lockFocus();
CameraManager manager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE);
try {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());
if (characteristics != null) {
sizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(ImageFormat.JPEG);
}

ImageReader reader = getImageReader();
if (reader == null) return;

List<Surface> outputSurfaces = getSurfaces(reader);
final CaptureRequest.Builder captureBuilder = getCaptureBuilder(reader);

final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
}
};
cameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
try {
session.capture(captureBuilder.build(), captureListener, null);
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onConfigureFailed(CameraCaptureSession session) {
}
}, null);
} catch (Exception e) {
e.printStackTrace();
}
}

如有任何帮助,我们将不胜感激。

最佳答案

如果 ImageReader 在相机图片捕获完成之前收集垃圾,就会发生这种情况。

getImageReader 方法是否将图像读取器永久存储在某处(比如作为类成员)?如果不是,则 ImageReader 的 Surface 就像一个弱引用,并且不会阻止它被删除。

关于android - Surface 没有适用于 Camera2 的有效原生 Surface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49606565/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com