The right to obtain local phone picture width and height

Get local phone picture width and height, we usually use the following method:

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

But in some models of mobile phones such as Samsung, after taking the picture may be rotated, such as rotated 90 degrees, if the width and height values ​​above code may be obtained opposite, then you need to obtain the rotation angle of the picture, then acquiring the rotation angle of the image width and height, directly on the code:

    //获取手机本地图片的宽度和高度
    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 (English: Exchangeable image file format, the official abbreviation Exif), the set is designed for digital camera photos, digital photos can be recorded attribute information and the shooting data, only pictures in JPEG format will carry exif data, like PNG, WebP images would not have such data.

In Android, the information may be acquired as image exposure time, image width, image height, brand equipment, rotation angle information through ExifInterface class. Used as demo images, image width and height is 3024 * 4032, self-test

 

Guess you like

Origin www.cnblogs.com/rainboy2010/p/12557015.html