Search in sources :

Example 36 with ExifInterface

use of android.media.ExifInterface in project weiui by kuaifan.

the class SubsamplingScaleImageView method getExifOrientation.

/**
 * Helper method for load tasks. Examines the EXIF info on the image file to determine the orientation.
 * This will only work for external files, not assets, resources or other URIs.
 */
@AnyThread
private int getExifOrientation(Context context, String sourceUri) {
    int exifOrientation = ORIENTATION_0;
    if (sourceUri.startsWith(ContentResolver.SCHEME_CONTENT)) {
        Cursor cursor = null;
        try {
            String[] columns = { MediaStore.Images.Media.ORIENTATION };
            cursor = context.getContentResolver().query(Uri.parse(sourceUri), columns, null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    int orientation = cursor.getInt(0);
                    if (VALID_ORIENTATIONS.contains(orientation) && orientation != ORIENTATION_USE_EXIF) {
                        exifOrientation = orientation;
                    } else {
                        Log.w(TAG, "Unsupported orientation: " + orientation);
                    }
                }
            }
        } catch (Exception e) {
            Log.w(TAG, "Could not get orientation of image from media store");
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (sourceUri.startsWith(ImageSource.FILE_SCHEME) && !sourceUri.startsWith(ImageSource.ASSET_SCHEME)) {
        try {
            ExifInterface exifInterface = new ExifInterface(sourceUri.substring(ImageSource.FILE_SCHEME.length() - 1));
            int orientationAttr = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            if (orientationAttr == ExifInterface.ORIENTATION_NORMAL || orientationAttr == ExifInterface.ORIENTATION_UNDEFINED) {
                exifOrientation = ORIENTATION_0;
            } else if (orientationAttr == ExifInterface.ORIENTATION_ROTATE_90) {
                exifOrientation = ORIENTATION_90;
            } else if (orientationAttr == ExifInterface.ORIENTATION_ROTATE_180) {
                exifOrientation = ORIENTATION_180;
            } else if (orientationAttr == ExifInterface.ORIENTATION_ROTATE_270) {
                exifOrientation = ORIENTATION_270;
            } else {
                Log.w(TAG, "Unsupported EXIF orientation: " + orientationAttr);
            }
        } catch (Exception e) {
            Log.w(TAG, "Could not get EXIF orientation of image");
        }
    }
    return exifOrientation;
}
Also used : ExifInterface(android.media.ExifInterface) Cursor(android.database.Cursor) Point(android.graphics.Point) Paint(android.graphics.Paint) AnyThread(android.support.annotation.AnyThread)

Example 37 with ExifInterface

use of android.media.ExifInterface in project weiui by kuaifan.

the class PictureFileUtils method readPictureDegree.

/**
 * 读取图片属性:旋转的角度
 *
 * @param path 图片绝对路径
 * @return degree旋转的角度
 */
public static int readPictureDegree(String path) {
    int degree = 0;
    try {
        ExifInterface exifInterface = new ExifInterface(path);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        switch(orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                degree = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                degree = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                degree = 270;
                break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return degree;
}
Also used : ExifInterface(android.media.ExifInterface) IOException(java.io.IOException) SuppressLint(android.annotation.SuppressLint) Paint(android.graphics.Paint)

Example 38 with ExifInterface

use of android.media.ExifInterface in project weiui by kuaifan.

the class ImageHeaderParser method copyExif.

public static void copyExif(ExifInterface originalExif, int width, int height, String imageOutputPath) {
    String[] attributes = new String[] { ExifInterface.TAG_APERTURE, ExifInterface.TAG_DATETIME, ExifInterface.TAG_DATETIME_DIGITIZED, ExifInterface.TAG_EXPOSURE_TIME, ExifInterface.TAG_FLASH, ExifInterface.TAG_FOCAL_LENGTH, ExifInterface.TAG_GPS_ALTITUDE, ExifInterface.TAG_GPS_ALTITUDE_REF, ExifInterface.TAG_GPS_DATESTAMP, ExifInterface.TAG_GPS_LATITUDE, ExifInterface.TAG_GPS_LATITUDE_REF, ExifInterface.TAG_GPS_LONGITUDE, ExifInterface.TAG_GPS_LONGITUDE_REF, ExifInterface.TAG_GPS_PROCESSING_METHOD, ExifInterface.TAG_GPS_TIMESTAMP, ExifInterface.TAG_ISO, ExifInterface.TAG_MAKE, ExifInterface.TAG_MODEL, ExifInterface.TAG_SUBSEC_TIME, ExifInterface.TAG_SUBSEC_TIME_DIG, ExifInterface.TAG_SUBSEC_TIME_ORIG, ExifInterface.TAG_WHITE_BALANCE };
    try {
        ExifInterface newExif = new ExifInterface(imageOutputPath);
        String value;
        for (String attribute : attributes) {
            value = originalExif.getAttribute(attribute);
            if (!TextUtils.isEmpty(value)) {
                newExif.setAttribute(attribute, value);
            }
        }
        newExif.setAttribute(ExifInterface.TAG_IMAGE_WIDTH, String.valueOf(width));
        newExif.setAttribute(ExifInterface.TAG_IMAGE_LENGTH, String.valueOf(height));
        newExif.setAttribute(ExifInterface.TAG_ORIENTATION, "0");
        newExif.saveAttributes();
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
    }
}
Also used : ExifInterface(android.media.ExifInterface) IOException(java.io.IOException)

Example 39 with ExifInterface

use of android.media.ExifInterface in project Rocket by mozilla-tw.

the class SubsamplingScaleImageView method getExifOrientation.

/**
 * Helper method for load tasks. Examines the EXIF info on the image file to determine the orientation.
 * This will only work for external files, not assets, resources or other URIs.
 */
@AnyThread
private int getExifOrientation(Context context, String sourceUri) {
    int exifOrientation = ORIENTATION_0;
    if (sourceUri.startsWith(ContentResolver.SCHEME_CONTENT)) {
        Cursor cursor = null;
        try {
            String[] columns = { MediaStore.Images.Media.ORIENTATION };
            cursor = context.getContentResolver().query(Uri.parse(sourceUri), columns, null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    int orientation = cursor.getInt(0);
                    if (VALID_ORIENTATIONS.contains(orientation) && orientation != ORIENTATION_USE_EXIF) {
                        exifOrientation = orientation;
                    } else {
                        Log.w(TAG, "Unsupported orientation: " + orientation);
                    }
                }
            }
        } catch (Exception e) {
            Log.w(TAG, "Could not get orientation of image from media store");
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (sourceUri.startsWith(ImageSource.FILE_SCHEME) && !sourceUri.startsWith(ImageSource.ASSET_SCHEME)) {
        try {
            ExifInterface exifInterface = new ExifInterface(sourceUri.substring(ImageSource.FILE_SCHEME.length() - 1));
            int orientationAttr = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            if (orientationAttr == ExifInterface.ORIENTATION_NORMAL || orientationAttr == ExifInterface.ORIENTATION_UNDEFINED) {
                exifOrientation = ORIENTATION_0;
            } else if (orientationAttr == ExifInterface.ORIENTATION_ROTATE_90) {
                exifOrientation = ORIENTATION_90;
            } else if (orientationAttr == ExifInterface.ORIENTATION_ROTATE_180) {
                exifOrientation = ORIENTATION_180;
            } else if (orientationAttr == ExifInterface.ORIENTATION_ROTATE_270) {
                exifOrientation = ORIENTATION_270;
            } else {
                Log.w(TAG, "Unsupported EXIF orientation: " + orientationAttr);
            }
        } catch (Exception e) {
            Log.w(TAG, "Could not get EXIF orientation of image");
        }
    }
    return exifOrientation;
}
Also used : ExifInterface(android.media.ExifInterface) Cursor(android.database.Cursor) Point(android.graphics.Point) Paint(android.graphics.Paint) AnyThread(android.support.annotation.AnyThread)

Example 40 with ExifInterface

use of android.media.ExifInterface in project WordPress-Utils-Android by wordpress-mobile.

the class ImageUtils method getExifOrientation.

private static int getExifOrientation(String path) {
    if (TextUtils.isEmpty(path)) {
        AppLog.w(AppLog.T.UTILS, "Can't read EXIF orientation. Passed path is empty.");
        return 0;
    }
    ExifInterface exif;
    try {
        exif = new ExifInterface(path);
    } catch (IOException e) {
        AppLog.e(AppLog.T.UTILS, "Can't read EXIF orientation.", e);
        return 0;
    }
    int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
    switch(exifOrientation) {
        case ExifInterface.ORIENTATION_NORMAL:
            return 0;
        case ExifInterface.ORIENTATION_ROTATE_90:
            return 90;
        case ExifInterface.ORIENTATION_ROTATE_180:
            return 180;
        case ExifInterface.ORIENTATION_ROTATE_270:
            return 270;
        default:
            return 0;
    }
}
Also used : ExifInterface(android.media.ExifInterface) IOException(java.io.IOException) Point(android.graphics.Point) Paint(android.graphics.Paint)

Aggregations

ExifInterface (android.media.ExifInterface)153 IOException (java.io.IOException)93 Paint (android.graphics.Paint)32 Bitmap (android.graphics.Bitmap)26 InputStream (java.io.InputStream)21 File (java.io.File)17 BitmapFactory (android.graphics.BitmapFactory)16 Matrix (android.graphics.Matrix)15 FileInputStream (java.io.FileInputStream)15 Point (android.graphics.Point)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ErrnoException (android.system.ErrnoException)10 BufferedInputStream (java.io.BufferedInputStream)10 FileDescriptor (java.io.FileDescriptor)10 FileNotFoundException (java.io.FileNotFoundException)10 SuppressLint (android.annotation.SuppressLint)9 FileOutputStream (java.io.FileOutputStream)8 Cursor (android.database.Cursor)7 AssetFileDescriptor (android.content.res.AssetFileDescriptor)6 Bundle (android.os.Bundle)5