短视频系统源代码,实现前置摄像头水平翻转的相关代码
代码语言:javascript复制private List<String> imgPaths = new ArrayList<>();
File file = new File(FILE_PATH);
if (file.exists()) {
if (mBitmap != null) {
mBitmap.recycle();
mBitmap = null;
}
mBitmap = BitmapFactory.decodeFile(FILE_PATH); // FILE_PATH,为拍照后获取的图片地址
if (mBitmap != null) {
Bitmap temp = convertBmp(mBitmap);
if (temp != null) {
mBitmap.recycle();
mBitmap = temp;
}
}
String newFiltPath = getNewFilePath(mBitmap);
imgPaths.add(newFiltPath); // 将翻转后新图片的地址,放到列表中,展示在预览控件中
}
public Bitmap convertBmp(Bitmap bmp) { // 对图片进行水平翻转
int w = bmp.getWidth();
int h = bmp.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(-1, 1); // 镜像水平翻转
Bitmap convertBmp = Bitmap.createBitmap(bmp, 0, 0, w, h, matrix, true);
return convertBmp;
}
private String getNewFilePath(Bitmap bitmap) { // 然后将翻转后的图片,存到本地,并获取新图片的地址
String rootDir = "";
try {
File outFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), System.currentTimeMillis() ".jpg");
rootDir = outFile.getAbsolutePath();
outFile.createNewFile();
FileOutputStream outStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.close();
} catch (IOException e) {
}
return rootDir;
}