我正在开发一个应用程序,它应该从camera2 api捕获相机预览/视频,并将ByteArray发送到通过网络连接的另一个设备(最好是wifi)。但我不能在弃用的api中获得类似于PreviewCallBack的正确预览。我知道代码不会有太多帮助,但我仍在添加它。请指导我完成任何解决方案。任何链接也将不胜感激。感谢..
private void createPreview() throws CameraAccessException {
SurfaceTexture surfaceTexture = mTexture.getSurfaceTexture();
assert surfaceTexture != null;
surfaceTexture.setDefaultBufferSize(size.getWidth(), size.getHeight());
//Surface surface = new Surface(surfaceTexture);
ImageReader imageReader = ImageReader.newInstance(size.getWidth(), size.getHeight(), ImageFormat.JPEG, 2);
imageReader.setOnImageAvailableListener(reader -> {
Image image = reader.acquireLatestImage();
ByteBuffer byteBuffer = image.getPlanes()[0].getBuffer();
image.close();
}, null);
captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
captureRequestBuilder.addTarget(imageReader.getSurface());
cameraDevice.createCaptureSession(Arrays.asList(imageReader.getSurface()), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
cameraCaptureSession = session;
updatePreview();
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
}
}, null);
}