亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

保存的位圖方向不正確

保存的位圖方向不正確

慕無忌1623718 2021-09-03 10:07:51
這個問題已被問過無數次,但沒有一個真正解決保存Bitmap.這是我最初將 a 保存Bitmap到我的設備的方式:FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();mmr.setDataSource(mStringFilePath);//Time is usint mPresentationTime = mPlayer.getPresentationTime();Bitmap mBitmap = mmr.getFrameAtTime(mPresentationTime, FFmpegMediaMetadataRetriever.OPTION_CLOSEST);File mFileBitmap = new File(directoryToStore, "test.png");try {    FileOutputStream outputStream = new FileOutputStream(mFileBitmap);    mBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);    outputStream.close();} catch (Exception e) {    e.printStackTrace();}以上保存.png了錯誤的方向。然后我看到了這個答案,但問題是它將已經保存的旋轉Bitmap到正確的方向。例如,如果您想將 設置Bitmap為 a ImageView,這很好。但是,如果我想共享Bitmap,或者想在設備庫中打開它,方向仍然是不正確的。然后我將不得不執行與上述相同的過程 -FileOutputStream等等。這將導致同樣的問題。如何Bitmap以正確的方向將 a 保存到設備?
查看完整描述

3 回答

?
哈士奇WWW

TA貢獻1799條經驗 獲得超6個贊

public static int getBitmapOriention(String path){

    ExifInterface exif = null;

    int orientation = 0;

    try {

        exif = new ExifInterface(path);

        orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,

                ExifInterface.ORIENTATION_UNDEFINED);

        //Log.e("getBitmapOriention", "getBitmapOriention: "+orientation );

    } catch (Exception e) {

        e.printStackTrace();

    }

    return orientation;

}

/**

 * @param bitmap bitmap from path

 * @param orientation ExifInterface

 * @return oriention flag

 */

public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {


    Matrix matrix = new Matrix();

    switch (orientation) {

        case ExifInterface.ORIENTATION_NORMAL:

            return bitmap;

        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:

            matrix.setScale(-1, 1);

            break;

        case ExifInterface.ORIENTATION_ROTATE_180:

            matrix.setRotate(180);

            break;

        case ExifInterface.ORIENTATION_FLIP_VERTICAL:

            matrix.setRotate(180);

            matrix.postScale(-1, 1);

            break;

        case ExifInterface.ORIENTATION_TRANSPOSE:

            matrix.setRotate(90);

            matrix.postScale(-1, 1);

            break;

        case ExifInterface.ORIENTATION_ROTATE_90:

            matrix.setRotate(90);

            break;

        case ExifInterface.ORIENTATION_TRANSVERSE:

            matrix.setRotate(-90);

            matrix.postScale(-1, 1);

            break;

        case ExifInterface.ORIENTATION_ROTATE_270:

            matrix.setRotate(-90);

            break;

        default:

            return bitmap;

    }

    try {

        Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

        bitmap.recycle();

        return bmRotated;

    }

    catch (OutOfMemoryError e) {

        e.printStackTrace();

        return null;

    }

}

它對我有用


rotateBitmap(mBitmap,getBitmapOriention(mPath));


查看完整回答
反對 回復 2021-09-03
?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

您需要ExifInterface用于旋轉。


public static void normalizeImageForUri(Context context, Uri uri, ImageView imgImageView) {

        try {

//            Log.d(TAG, "URI value = " + uri.getPath());

            ExifInterface exif = new ExifInterface(getRealPathFromURI(context, uri));

//            Log.d(TAG, "Exif value = " + exif);

            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

            Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);

            Bitmap rotatedBitmap = rotateBitmap(bitmap, orientation);

            imgImageView.setImageBitmap(rotatedBitmap);

        } catch (IOException e) {

            e.printStackTrace();

        }

}

檢查位圖的旋轉:


private static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {

        Matrix matrix = new Matrix();

        switch (orientation) {

            case ExifInterface.ORIENTATION_NORMAL:

                return bitmap;

            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:

                matrix.setScale(-1, 1);

                break;

            case ExifInterface.ORIENTATION_ROTATE_180:

                matrix.setRotate(180);

                break;

            case ExifInterface.ORIENTATION_FLIP_VERTICAL:

                matrix.setRotate(180);

                matrix.postScale(-1, 1);

                break;

            case ExifInterface.ORIENTATION_TRANSPOSE:

                matrix.setRotate(90);

                matrix.postScale(-1, 1);

                break;

            case ExifInterface.ORIENTATION_ROTATE_90:

                matrix.setRotate(90);

                break;

            case ExifInterface.ORIENTATION_TRANSVERSE:

                matrix.setRotate(-90);

                matrix.postScale(-1, 1);

                break;

            case ExifInterface.ORIENTATION_ROTATE_270:

                matrix.setRotate(-90);

                break;

            default:

                return bitmap;

        }

        try {

            Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

            bitmap.recycle();


            return bmRotated;

        } catch (OutOfMemoryError e) {

            e.printStackTrace();

            return null;

        }

    }


查看完整回答
反對 回復 2021-09-03
  • 3 回答
  • 0 關注
  • 209 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號