Android 7.1 默认自拍镜像
近来收到客户需求反馈需要将相机前摄成像默认为镜像显示,大致思路我们可以在保存数据前将前摄拍的照片转为镜像,保存数据是通过PhotoMode.java文件中的saveData方法实现,具体修改参照如下:
首先添加将图片转为镜像显示的方法,具体功能代码参照如下:
/vendor/mediatek/proprietary/packages/apps/Camera2/common/src/com/mediatek/camera/common/utils/CameraUtil.java
+public static byte[] mirrorJpegData(byte[] jpegData, int orientation) {
+ byte[] dest = jpegData;
+ BitmapFactory.Options opts = new BitmapFactory.Options();
+ opts.inMutable = true;
+ Bitmap b = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length, opts);
+ b = PhotoModeHelper.rotateAndMirror(b, 0, true);
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ b.compress(Bitmap.CompressFormat.JPEG, 100, baos);
+ dest = baos.toByteArray();
+ b.recycle();
+
+ return dest;
+ }
其次我们在相机保存数据前将前摄拍的照片转为镜像,拍摄时使用的是前摄还是后摄可以通过CameraId来区分,CameraId值为0是后摄,CameraId值为1是前摄,具体修改参照如下:
/vendor/mediatek/proprietary/packages/apps/Camera2/common/src/com/mediatek/camera/common/mode/photo/PhotoMode.java
private void saveData(byte[] jpegData) {
if (jpegData != null) {
//check memory to decide whether it can take next picture.
//if not, show saving
ISettingManager.SettingController controller = mISettingManager.getSettingController();
String dngState = controller.queryValue(KEY_DNG);
long saveDataSize = jpegData.length;
+ if(mCameraId.equals("1")){
+ jpegData = CameraUtil.mirrorJpegData(jpegData,0);
+ }
if (dngState != null && "on".equalsIgnoreCase(dngState)) {
saveDataSize = saveDataSize + DNG_IMAGE_SIZE;
}
最后成像后在相机界面预览的缩略图我们也要做相应的镜像处理,具体修改参照如下:
/vendor/mediatek/proprietary/packages/apps/Camera2/common/src/com/mediatek/camera/common/mode/photo/PhotoMode.java
private void updateThumbnail(byte[] jpegData) {
Bitmap bitmap = BitmapCreator.createBitmapFromJpeg(jpegData, mIApp.getAppUi()
.getThumbnailViewWidth());
+ if(mCameraId.equals("1")){
+ bitmap = PhotoModeHelper.rotateAndMirror(bitmap, 0, true);
+ }
mIApp.getAppUi().updateThumbnail(bitmap);
}
重新编译验证,修改生效,已实现相机前摄成像默认为镜像显示