我有这段代码(仅用于实验):
public void captureFromCam(View v) throws CameraAccessException {
try {
cm.openCamera("0", new CameraDevice.StateCallback(){
@Override
public void onClosed(CameraDevice camera) {
super.onClosed(camera);
}
@Override
public void onError(CameraDevice camera, int error) {
}
@Override
public void onOpened(final CameraDevice cam) {
Log.d("CameraApp", "Camera Opened");
ImageReader ir = ImageReader.newInstance(800, 600, 256, 1);
Surface surface = ir.getSurface();
final ArrayList<Surface> l = new ArrayList();
l.add(surface);
try {
cam.createCaptureSession(l, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
Log.e("CameraApp", "Configured");
CaptureRequest.Builder builder = null, b2;
try {
builder = cam.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
} catch (CameraAccessException e) {
e.printStackTrace();
}
builder.addTarget(l.get(0));
CaptureRequest request = builder.build();
try {
session.capture(request, new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
Log.e("CameraApp", "Captured");
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onDisconnected(CameraDevice camera) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
有趣的部分是onCaptureCompleted
,因为我理解结果不知何故是我的照片。
如何将此图片保存到文件?或者如果它不是我的照片,我的照片在哪里以及如何将其保存到文件中?
我知道这个问题已得到解答,但这是API 21,它与旧版本截然不同。
答案 0 :(得分:0)
我写了一个线程类来保存图片。
@Override
public void run() {
mImageReader.setOnImageAvailableListener(ReaderListener, mHandler);
}
/**
* diffrent formats to save diffrent pictures
*/
ImageReader.OnImageAvailableListener ReaderListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader imageReader) {
Image image = imageReader.acquireLatestImage();
checkParentDir();
File file;
if (mFormat == ImageFormat.JPEG) {
checkJpegDir();
file = createJpeg();
//here,maybe null, so trycatch
try {
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();
} catch (Exception e) {
Log.i("Exception e", "ImageFormat.JPEG,,,,,,,,Exception eException e");
e.getStackTrace();
}
} else if (mFormat == ImageFormat.RAW_SENSOR) {
checkDngDir();
file = createDng();
try {
if (mDngCreator != null) {
mDngCreator.writeImage(new FileOutputStream(file), image);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
Log.i("Exception e", "mDngCreator.writeImage(new FileOutputStream(file), image)");
}
try {//image maybe null
mDngCreator.close();
image.close();
} catch (Exception e) {
Log.i("Exception e", "ImageFormat.RAW_SENSOR,,,,,,,,Exception eException e");
e.getStackTrace();
}
}
}
};
答案 1 :(得分:0)
此解决方案对我有效,我已将图像保存在ImageReader.OnImageAvailableListener回调方法中。经过一些研发,我发现了这个,试试看。
函数调用:saveImage(it.acquireLatestImage(),文件)
/**
* Save the image to storage
* @param image Image object from OnImageAvailableListener
* @param file File where image is going to be written
* @return File object pointing to the file URI, null if the file already exists
*/
fun saveImage(image: Image, file: File): File? {
if (file.exists()) {
image.close()
return null
}
val buffer = image.planes[0].buffer
val bytes = ByteArray(buffer.remaining())
buffer.get(bytes)
val output = FileOutputStream(file)
output.write(bytes)
image.close()
output.close()
return file
}