在我使用 camera2api 构建相机应用程序的过程中,我在尝试保存原始图像时遇到了一个小问题。
我将捕获结果分配给以下代码中的成员。
@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
mCaptureResult = result;
Toast.makeText(getApplicationContext(),
"Image Captured",Toast.LENGTH_SHORT).show();
}
};
当我在这个位置调试它时,成员被分配而不是 null。DngCreator
但是,当它在类中引发空错误时ImageSaver
。
我的听众:
private ImageReader mImageReader;
private ImageReader.OnImageAvailableListener mOnImageAvailableListener =
new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader imageReader) {
mBackgroundHandler.post(new ImageSaver(imageReader.acquireNextImage(),mUiHandler,
mCaptureResult, mCameraCharacteristics));
}
};
private ImageReader mRawImageReader;
private ImageReader.OnImageAvailableListener mOnRawImageAvailableListener =
new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader imageReader) {
mBackgroundHandler.post(new ImageSaver(imageReader.acquireNextImage(),mUiHandler,
mCaptureResult, mCameraCharacteristics));
}
};
我的图像保护程序类:
private ImageSaver(Image image, Handler handler, CaptureResult captureResult,
CameraCharacteristics cameraCharacteristics) {
mImage = image;
mHandler =handler;
mCaptureResult = captureResult;
mCameraCharacteristics = cameraCharacteristics;
}
@Override
public void run() {
int format = mImage.getFormat();
switch(format){
case ImageFormat.JPEG:
ByteBuffer byteBuffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(mImageFile);
fileOutputStream.write(bytes);
}
catch (IOException e){
e.printStackTrace();
}
finally {
mImage.close();
if(fileOutputStream != null){
try{
fileOutputStream.close();
}
catch (IOException e){
e.printStackTrace();
}
}
//Message message = mHandler.obtainMessage();
//message.sendToTarget();
}
break;
case ImageFormat.RAW_SENSOR:
DngCreator dngCreator = new DngCreator(mCameraCharacteristics,mCaptureResult);
FileOutputStream rawFileOutputStream = null;
try {
rawFileOutputStream = new FileOutputStream(mRawImageFile);
dngCreator.writeImage(rawFileOutputStream, mImage);
}
catch (IOException e){
e.printStackTrace();
}
finally{
mImage.close();
if(rawFileOutputStream != null){
try {
rawFileOutputStream.close();
}
catch (IOException e){
e.printStackTrace();
}
}
}
break;
}
}
}
现在,当我尝试初始化 DngCreator 并且堆栈跟踪如下时,它给了我一个错误。
FATAL EXCEPTION: Camera2 Background Thread
Process: com.something.something, PID: 5162
java.lang.IllegalArgumentException: Null argument to DngCreator constructor
at android.hardware.camera2.DngCreator.<init>(DngCreator.java:89)
at com.something.something.ControlCameraActivity$ImageSaver.run(ControlCameraActivity.java:328)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
有人可以帮我解决这个问题吗?如果您需要我提供更多信息,请立即告诉我。谢谢