正確獲取手機本地圖片的寬度和高度


獲取手機本地圖片的寬度和高度,我們通常使用以下方法:

 final BitmapFactory.Options options = new BitmapFactory.Options();
 options.inJustDecodeBounds = true;
 BitmapFactory.decodeFile(filePath, options);
 int width= options.outWidth;
 int height=options.outHeight;

但是在一些手機機型上如三星手機,拍照后圖片可能會進行旋轉,如旋轉90度,如果采用上面的代碼獲取的寬高值可能是反的,這時就需要獲取圖片的旋轉角度,然后根據旋轉角度來獲取圖片的寬高,直接上代碼:

    //獲取手機本地圖片的寬度和高度
    private Pair<Integer,Integer> getImageWidthHeight(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        int width= options.outWidth;
        int height=options.outHeight;
        int orientation=ExifInterface.ORIENTATION_NORMAL; try { ExifInterface exifInterface = new ExifInterface(filePath); orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); } catch (IOException e) { e.printStackTrace(); } switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: case ExifInterface.ORIENTATION_ROTATE_270: width=options.outHeight; height=options.outWidth; break; } return new Pair<>(width,height); }

EXIF:可交換圖像文件格式(英語:Exchangeable image file format,官方簡稱Exif),是專門為數碼相機的照片設定的,可以記錄數碼照片的屬性信息和拍攝數據,只有JPEG格式的圖片才會攜帶exif數據,像PNG,WebP這類的圖片就不會有這些數據。

在Android下,可以通過ExifInterface類來獲取圖片的信息如曝光時間,圖片寬度,圖片高度,設備品牌,旋轉角度等信息。使用的demo圖片如下,圖片的寬高為3024*4032,可自行測試

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM