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

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

Android:從圖庫(kù)加載的位圖在ImageView中旋轉(zhuǎn)

Android:從圖庫(kù)加載的位圖在ImageView中旋轉(zhuǎn)

蕪湖不蕪 2019-10-05 14:57:54
當(dāng)我將媒體庫(kù)中的圖像加載到位圖中時(shí),一切正常,除了將相機(jī)在垂直握住手機(jī)的情況下拍攝的照片旋轉(zhuǎn)之外,即使在垂直方向上看起來(lái)是垂直的,我也總是得到水平的照片。畫(huà)廊。為什么會(huì)這樣,如何正確加載呢?
查看完整描述

3 回答

?
喵喵時(shí)光機(jī)

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

這是一個(gè)完整的解決方案(可從Facebook SDK的Hackbook示例中找到)。它的優(yōu)點(diǎn)是不需要訪問(wèn)文件本身。如果要從內(nèi)容解析器加載圖像,這將非常有用(例如,如果您的應(yīng)用正在響應(yīng)共享照片的意圖)。


public static int getOrientation(Context context, Uri photoUri) {

    /* it's on the external media. */

    Cursor cursor = context.getContentResolver().query(photoUri,

            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);


    if (cursor.getCount() != 1) {

        return -1;

    }


    cursor.moveToFirst();

    return cursor.getInt(0);

}

然后,您可以如下獲得旋轉(zhuǎn)的位圖。這段代碼還將圖片縮小(不幸的是)到MAX_IMAGE_DIMENSION。否則可能會(huì)耗盡內(nèi)存。


public static Bitmap getCorrectlyOrientedImage(Context context, Uri photoUri) throws IOException {

    InputStream is = context.getContentResolver().openInputStream(photoUri);

    BitmapFactory.Options dbo = new BitmapFactory.Options();

    dbo.inJustDecodeBounds = true;

    BitmapFactory.decodeStream(is, null, dbo);

    is.close();


    int rotatedWidth, rotatedHeight;

    int orientation = getOrientation(context, photoUri);


    if (orientation == 90 || orientation == 270) {

        rotatedWidth = dbo.outHeight;

        rotatedHeight = dbo.outWidth;

    } else {

        rotatedWidth = dbo.outWidth;

        rotatedHeight = dbo.outHeight;

    }


    Bitmap srcBitmap;

    is = context.getContentResolver().openInputStream(photoUri);

    if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) {

        float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);

        float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);

        float maxRatio = Math.max(widthRatio, heightRatio);


        // Create the bitmap from file

        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inSampleSize = (int) maxRatio;

        srcBitmap = BitmapFactory.decodeStream(is, null, options);

    } else {

        srcBitmap = BitmapFactory.decodeStream(is);

    }

    is.close();


    /*

     * if the orientation is not 0 (or -1, which means we don't know), we

     * have to do a rotation.

     */

    if (orientation > 0) {

        Matrix matrix = new Matrix();

        matrix.postRotate(orientation);


        srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(),

                srcBitmap.getHeight(), matrix, true);

    }


    return srcBitmap;

}


查看完整回答
反對(duì) 回復(fù) 2019-10-05
?
郎朗坤

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

在這篇文章中,借助這篇文章幫助解決了我的問(wèn)題:


            Bitmap myBitmap = getBitmap(imgFile.getAbsolutePath());


            try {

                ExifInterface exif = new ExifInterface(imgFile.getAbsolutePath());

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

                Log.d("EXIF", "Exif: " + orientation);

                Matrix matrix = new Matrix();

                if (orientation == 6) {

                    matrix.postRotate(90);

                }

                else if (orientation == 3) {

                    matrix.postRotate(180);

                }

                else if (orientation == 8) {

                    matrix.postRotate(270);

                }

                myBitmap = Bitmap.createBitmap(myBitmap, 0, 0, myBitmap.getWidth(), myBitmap.getHeight(), matrix, true); // rotating bitmap

            }

            catch (Exception e) {


            }

            ImageView img = (ImageView) findViewById(R.id.imgTakingPic);

            img.setImageBitmap(myBitmap);

希望它可以節(jié)省別人的時(shí)間!


查看完整回答
反對(duì) 回復(fù) 2019-10-05
  • 3 回答
  • 0 關(guān)注
  • 778 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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