天天看點

短視訊系統源代碼,實作前置攝像頭水準翻轉的相關代碼

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;
}           

繼續閱讀