我正在尝试使用相机api2来捕捉图像。代码在MOTO g4上工作正常,但是当我在NEXUS 6上测试代码时,在session.capture之后没有调用onImageAvailable,也没有保存图像。非常沮丧,非常感谢有人可以提供帮助!非常感谢。
ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
List<Surface> outputSurfaces = new ArrayList<Surface>(2);
outputSurfaces.add(reader.getSurface());
outputSurfaces.add(new Surface(textureView.getSurfaceTexture()));
final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(reader.getSurface());
captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
// Orientation
int rotation = getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));
sendUri = getImageUri();
final File file = new File(Environment.getExternalStorageDirectory()+"/" + sendUri +".jpg");
ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
Image image = null;
try {
image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
save(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (image != null) {
image.close();
}
}
}
private void save(byte[] bytes) throws IOException {
OutputStream output = null;
try {
output = new FileOutputStream(file);
output.write(bytes);
} finally {
if (null != output) {
output.close();
}
}
}
};
reader.setOnImageAvailableListener(readerListener, mBackgroundHandler);
final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
//Toast.makeText(MainActivity.this, "Saved:" + file, Toast.LENGTH_SHORT).show();
toast1("Saved:"+file);
//createCameraPreview();
}
};
cameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
try {
session.capture(captureBuilder.build(), captureListener, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
}
}, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
以下是运行相机功能时来自nexus 6的完整logcat输出,似乎没什么用处......
03-07 13:04:34.526 1838-1838/examples.hello E/AndroidCameraApi: is camera open
03-07 13:04:34.527 1838-1838/examples.hello I/CameraManagerGlobal: Connecting to camera service
03-07 13:04:34.659 1838-1838/examples.hello E/AndroidCameraApi: openCamera X
03-07 13:04:34.694 1838-1838/examples.hello E/AndroidCameraApi: onOpened
03-07 13:04:34.722 1838-1883/examples.hello D/OpenGLRenderer: endAllActiveAnimators on 0x931bee80 (RippleDrawable) with handle 0x92c2cea0
03-07 13:04:43.952 1838-1838/examples.hello E/AndroidCameraApi: onPause
答案 0 :(得分:1)
如果你从失败的设备中包含完整的logcat输出会有所帮助,但我怀疑问题是这样的:
ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
您正在创建一个本地ImageReader对象,而不是将其保存以供进一步使用。你从它得到一个Surface,但是Surface很像一个弱指针;如果没有引用它,它将不会阻止ImageReader获取GC(请参阅note in the developer references)
所以我怀疑如果你只是创建一个类成员mReader变量,并将读者存储在其中,一切都会正常工作。
由于GC算法的差异,它可能正在使用Moto手机;运行时还没有完全清除读者对象,所以它有时间调用回调。
答案 1 :(得分:1)
像这样覆盖onConfigureFailed()
:
@Override
public void onConfigureFailed(CameraCaptureSession session) {
ImageReader mReader = ImageReader.newInstance(640, 480, ImageFormat.JPEG, 1);
takePicture() // function to get image
createCameraPreview(); // function to set camera Preview on screen
}
调用createCameraPreview函数重新启动相机,否则它将卡住。 您可以使用新值更改ImageReader
ImageReader mReader = ImageReader.newInstance(640, 480, ImageFormat.JPEG, 1);
然后再次调用takePicture()
函数,以便用户不必再次单击即可捕获图像。