我用camera2 API構建了我自己的相機應用程序。我從樣本「camera2Raw」開始,我添加了YUV_420_888支持而不是JPEG。但現在我想知道如何將圖像保存在ImageSaver中!?如何保存YUV_420_888圖片?
這裏是我的run方法的代碼:
@Override
public void run() {
boolean success = false;
int format = mImage.getFormat();
switch(format) {
case ImageFormat.RAW_SENSOR:{
DngCreator dngCreator = new DngCreator(mCharacteristics, mCaptureResult);
FileOutputStream output = null;
try {
output = new FileOutputStream(mFile);
dngCreator.writeImage(output, mImage);
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
mImage.close();
closeOutput(output);
}
break;
}
case ImageFormat.YUV_420_888:{
ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
FileOutputStream output = null;
try {
output = new FileOutputStream(mFile);
output.write(bytes);
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
mImage.close();
closeOutput(output);
}
break;
}
default:
Log.e(TAG, "Cannot save image, unexpected image format:" + format);
}
// Decrement reference count to allow ImageReader to be closed to free up resources.
mReader.close();
// If saving the file succeeded, update MediaStore.
if (success) {
MediaScannerConnection.scanFile(mContext, new String[] { mFile.getPath()},
/*mimeTypes*/ null, new MediaScannerConnection.MediaScannerConnectionClient() {
@Override
public void onMediaScannerConnected() {
// Do nothing
}
@Override
public void onScanCompleted(String path, Uri uri) {
Log.i(TAG, "Scanned " + path + ":");
Log.i(TAG, "-> uri=" + uri);
}
});
}
}
我試圖保存YUV圖像像JPEG,但這種方式,我只能得到一個平面和保存的數據沒有任何意義我...
什麼是保存YUV圖像的正確方法?把它轉換成RGB(YUV的意義是什麼?)?或者與YuvImage類?
非常酷!我只添加了一個「FileOutputStream輸出= null;」第一次嘗試之前,一切正常!非常感謝你! – esihemohen
啊,是的,試圖從其他代碼段中剪切片段的危險。對於那個很抱歉!我將修改上面的代碼,使其對於未來發現它的人來說是相當獨立的。 – rcsumner
如果我以這種方式將'YUV_420_888'圖像保存到'.jpg'文件中,則此圖像文件不可讀。 –