第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

保存的位圖方向不正確

保存的位圖方向不正確

慕無忌1623718 2021-09-03 10:07:51
這個(gè)問題已被問過無數(shù)次,但沒有一個(gè)真正解決保存Bitmap.這是我最初將 a 保存Bitmap到我的設(shè)備的方式: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了錯(cuò)誤的方向。然后我看到了這個(gè)答案,但問題是它將已經(jīng)保存的旋轉(zhuǎn)Bitmap到正確的方向。例如,如果您想將 設(shè)置Bitmap為 a ImageView,這很好。但是,如果我想共享Bitmap,或者想在設(shè)備庫中打開它,方向仍然是不正確的。然后我將不得不執(zhí)行與上述相同的過程 -FileOutputStream等等。這將導(dǎo)致同樣的問題。如何Bitmap以正確的方向?qū)?a 保存到設(shè)備?
查看完整描述

3 回答

?
哈士奇WWW

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊

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;

    }

}

它對(duì)我有用


rotateBitmap(mBitmap,getBitmapOriention(mPath));


查看完整回答
反對(duì) 回復(fù) 2021-09-03
?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊

您需要ExifInterface用于旋轉(zhuǎn)。


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();

        }

}

檢查位圖的旋轉(zhuǎn):


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;

        }

    }


查看完整回答
反對(duì) 回復(fù) 2021-09-03
  • 3 回答
  • 0 關(guān)注
  • 201 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)