最近在做一个产品,需要Android主板外接红外热成像摄像头,保存红外摄像头拍下的照片,这里记一下实现的方法。
/**
* 保存照片
* author:qingjiaorousi
* 2021.10.28
*/
private void savePicture(){
mCamera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
String filePath = Environment.getExternalStorageDirectory()
.getPath() +"/";
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date curDate = new Date(System.currentTimeMillis());
String nowTime = format.format(curDate);
String path = Environment.getExternalStorageDirectory()
.getPath() +"/focus/";
File pathDir = new File(path);
if (!pathDir.exists()){
pathDir.mkdirs();
}
final Bitmap mBitmap;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Matrix matrix = new Matrix();
mBitmap = Bitmap.createBitmap(bitmapResult, 0, 0, bitmapResult.getWidth(), bitmapResult.getHeight(), matrix, true);
saveBitmap(path+ nowTime + ".jpg",mBitmap);
}
});
}
public void saveBitmap(String filePath ,Bitmap mBitmap) {
File f = new File(filePath);
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "拍照成功!", Toast.LENGTH_LONG).show();
}