gpt4 book ai didi

android - 使用 camera2 API 捕获图片而不进行预览

转载 作者:IT王子 更新时间:2023-10-28 23:35:20 27 4
gpt4 key购买 nike

我想要实现的是在不显示预览的情况下捕获图片,直接将输出发送到 ImageReader。我用过Camera2Basic example作为起点。但是,我的 ImageReader 的回调永远不会被调用,因此没有图像被发送到它的 Surface。我真的需要先显示预览吗?

这是相机代码,按照异步回调的流程排序:

private ImageReader imageReader;
private Handler backgroundHandler;
private HandlerThread backgroundThread;
private String cameraId;
private CameraDevice cameraDevice;
private CameraCaptureSession cameraCaptureSession;
@Override
public void onCreate() {
setupCamera2();
}

private void setupCamera2() {
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

try {

for (String cameraId : manager.getCameraIdList()) {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

if (characteristics.get(CameraCharacteristics.LENS_FACING) != CameraCharacteristics.LENS_FACING_FRONT) {
continue;
}

this.cameraId = cameraId;

int[] picSize = Settings.getPictureSize();
int picWidth = picSize[0];
int picHeight = picSize[1];

imageReader = ImageReader.newInstance(picWidth, picHeight, ImageFormat.JPEG, 2);
imageReader.setOnImageAvailableListener(onImageAvailableListener, backgroundHandler);
}

} catch (CameraAccessException | NullPointerException e) {
e.printStackTrace();
}
}


private void openCamera2() {
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {

manager.openCamera(cameraId, cameraStateCallback, backgroundHandler);

} catch (CameraAccessException e) {
e.printStackTrace();
}
}



private final CameraDevice.StateCallback cameraStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice device) {
cameraDevice = device;
createCameraCaptureSession();
}

@Override
public void onDisconnected(CameraDevice cameraDevice) {}

@Override
public void onError(CameraDevice cameraDevice, int error) {}
};



private void createCaptureSession() {
List<Surface> outputSurfaces = new LinkedList<>();
outputSurfaces.add(imageReader.getSurface());

try {

cameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
cameraCaptureSession = session;
}

@Override
public void onConfigureFailed(CameraCaptureSession session) {}
}, null);

} catch (CameraAccessException e) {
e.printStackTrace();
}
}


private final ImageReader.OnImageAvailableListener onImageAvailableListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
createCaptureRequest();
}
};


private void createCaptureRequest() {
try {

CaptureRequest.Builder requestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
requestBuilder.addTarget(imageReader.getSurface());

// Focus
requestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);

// Orientation
int rotation = windowManager.getDefaultDisplay().getRotation();
requestBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));

cameraCaptureSession.capture(requestBuilder.build(), camera2Callback, null);

} catch (CameraAccessException e) {
e.printStackTrace();
}
}

最佳答案

您应该在“onConfigured”函数中拍摄照片,而不是 onImageAvailable。

public void onConfigured(CameraCaptureSession session) {
cameraCaptureSession = session;
createCaptureRequest();
}

在这个函数“onImageAvailable”中,你应该保存图片,

Image image = mImageReader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
try {
save(bytes, file);
} catch (IOException e) {
e.printStackTrace();
}
image.close();

"onImageAvailable"函数将在 session.capture() 之后调用。

关于android - 使用 camera2 API 捕获图片而不进行预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28003186/

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