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命令

發佈了32 篇原創文章 · 獲贊 8 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章