Android实现本地图片、视频左右镜像翻转

因项目预研需要,采用android camera2进行前置、后置摄像头拍照、拍视频,在用前置摄像头进行拍照时,照片预览是正的,保存本地照片也是正的,只不过照片里的内容进行了左右镜像,现在需要将照片里的内容再左右镜像回来,找了很多的方法,以下两种亲测可行:

第一种:

Bitmap oldMap = BitmapFactory.decodeFile(mFile.getAbsolutePath());
matrix.setScale(-1.0f, 1.0f);
oldMap = Bitmap.createBitmap(oldMap, 0, 0, oldMap.getWidth(), oldMap.getHeight(), matrix, true);
try {
    FileOutputStream out = new FileOutputStream(mFile);
    oldMap.compress(Bitmap.CompressFormat.JPEG, 100, out);
    out.flush();
    out.close();
} catch (Exception e) {
    e.printStackTrace();
}

第二种:

Bitmap oldMap = BitmapFactory.decodeFile(mFile.getAbsolutePath());
Paint paint=new Paint();
Bitmap newMap= Bitmap.createBitmap(oldMap.getWidth(), oldMap.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas=new Canvas(newMap);
canvas.drawColor(Color.WHITE);
Matrix matrix=new Matrix();
matrix.setScale(-1.0f, 1.0f);
matrix.postTranslate(oldMap.getWidth(), 0);
canvas.drawBitmap(oldMap, matrix, paint);
try {
    FileOutputStream out = new FileOutputStream(mFile);
    newMap.compress(Bitmap.CompressFormat.JPEG, 100, out);
    out.flush();
    out.close();
} catch (Exception e) {
    e.printStackTrace();
}

同理,用前置摄像头拍摄视频时,不仅保存的视频是倒的,视频内容也进行了左右镜像翻转,需要将拍摄的视频旋转及镜像翻转回来,由于初次接触,找了很多的资料,终于找到一个亲测可行的方法,如下:

1.在build.gradle里添加:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

2.添加gradle依赖

compile 'com.github.yangjie10930:EpMedia:v0.9.5'

3.视频处理

EpVideo epVideo = new EpVideo(mNextVideoAbsolutePath);
//视频旋转180度,再镜像
epVideo.rotation(180, true);
EpEditor.exec(epVideo, new EpEditor.OutputOption(getVideoFilePath(getContext())), new OnEditorListener() {
                @Override
                public void onSuccess() {
                    Log.e(TAG, "EpEditor onSuccess");
                }

                @Override
                public void onFailure() {
                    Log.e(TAG, "EpEditor onFailure");
                }

                @Override
                public void onProgress(float progress) {
                    Log.e(TAG, "EpEditor onProgress " + progress);
                }
               });

具体参考:

1.EpMedia

2.将FFmpeg移植到Android平台

3.FFmpegAndroid

4.在Android项目中调用FFmpeg命令

你可能感兴趣的:(Android)