2016-08-05 1766 views
3

我有一個能夠拍攝圖像和錄製視頻的攝像頭應用程序。但是,當從設備前置攝像頭捕捉圖像或錄製視頻時,結果會翻轉,就像您在看鏡子一樣。我想再次翻轉,所以看起來很正常。我設法與圖像要做到這一點,通過使用Matrix翻轉Bitmap從Android的前置攝像頭拍攝的鏡像翻轉視頻

public Bitmap flip(Bitmap bitmap) { 
     int w = bitmap.getWidth(); 
     int h = bitmap.getHeight(); 
     Matrix matrix = new Matrix(); 
     float[] mirrorY = {-1, 0, 0, 0, 1, 0, 0, 0, 1}; 
     Matrix matrixMirrorY = new Matrix(); 
     matrixMirrorY.setValues(mirrorY); 
     matrix.postConcat(matrixMirrorY); 
     matrix.postRotate(90); 
     return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); 
    } 

我無法弄清楚如何翻轉由MediaRecorder拍攝的視頻,我知道我可以運行ffmpeg命令:

-i /pathtooriginalfile/originalfile.mp4 -vf hflip -c:a copy /pathtosave/flippedfile.mp4 

但我不知道如何從代碼運行ffmpeg命令,我找不到另一種方法。有很多議題討論這個問題,但我找不到解決方案。注意:Snapchat有可能以某種方式工作。

謝謝。

P.S對不起,使用前置攝像頭,我的英語

+0

嗨,你找到了解決方案嗎?謝謝 –

+0

@dangling_refrenz Nope,從來沒有試圖解決這個問題後,這個線程和我在這裏得到的答案,無論如何,祝你好運。 – DAVIDBALAS1

+0

@ DAVIDBALAS1我面臨同樣的問題。你找到解決方案嗎? – Piyush

回答

0

拍攝圖像或錄製視頻將總是給人翻轉的圖像,其作爲前置攝像頭的工作原理像你照鏡子的預期。

從相機獲取圖片後,通過將圖像繪製到新的上下文中將圖像翻轉回來。

請嘗試下面的代碼片段。請參考Android- Taking a picture from front facing camera rotates the photo

Matrix rotateRight = new Matrix(); 
rotateRight.preRotate(90); 

if(android.os.Build.VERSION.SDK_INT>13 && CameraActivity.frontCamera) { 
    float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1}; 
    rotateRight = new Matrix(); 
    Matrix matrixMirrorY = new Matrix(); 
    matrixMirrorY.setValues(mirrorY); 

    rotateRight.postConcat(matrixMirrorY); 

    rotateRight.preRotate(270); 

} 

final Bitmap rImg= Bitmap.createBitmap(img, 0, 0, 
       img.getWidth(), img.getHeight(), rotateRight, true); 
+0

再次閱讀。我設法翻轉圖像而不是視頻。 – DAVIDBALAS1

+0

您無法一次翻轉視頻,因爲您必須重寫onPreviewFrame()回調,然後在寫入緩衝區之前需要翻轉每一幀。 –

+0

好的,謝謝..我想這會花太長的時間。 – DAVIDBALAS1

相關問題