在下面的代码中,我刚刚读取图像并将其保存到图库中。
public void onImageAvailable(ImageReader reader) {
Image image = null;
try {
image = reader.acquireLatestImage();
buffer = image.getPlanes()[0].getBuffer();
bytes = new byte[buffer.capacity()];
buffer.get(bytes);
save(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
} finally {
if (image != null) {
image.close();
}
}
private void save(byte [] bytes)引发IOException {
if (!myDir.exists()) {
myDir.mkdirs();
}
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".png";
File file = new File(myDir,fname);
if (file.exists()) file.delete();
OutputStream output = null;
try {
output = new FileOutputStream(file);
output.write(bytes);
scanFile(file.getabsolutePath());
//最终int旋转= getImageOrientation(file.getabsolutePath());
} finally {
if (null != output) {
output.close();
}
}
}