Search in sources :

Example 1 with ExifInterface

use of com.moez.QKSMS.exif.ExifInterface in project qksms by moezbhatti.

the class UriImage method getOrientation.

/**
 * Returns the number of degrees to rotate the picture, based on the orientation tag in
 * the exif data or the orientation column in the database. If there's no tag or column,
 * 0 degrees is returned.
 *
 * @param context Used to get the ContentResolver
 * @param uri Path to the image
 */
public static int getOrientation(Context context, Uri uri) {
    long dur = System.currentTimeMillis();
    if (ContentResolver.SCHEME_FILE.equals(uri.getScheme()) || sURLMatcher.match(uri) == MMS_PART_ID) {
        // file for the orientation because there is no column in the db for the orientation.
        try {
            InputStream inputStream = context.getContentResolver().openInputStream(uri);
            ExifInterface exif = new ExifInterface();
            try {
                exif.readExif(inputStream);
                Integer val = exif.getTagIntValue(ExifInterface.TAG_ORIENTATION);
                if (val == null) {
                    return 0;
                }
                int orientation = ExifInterface.getRotationForOrientationValue(val.shortValue());
                return orientation;
            } catch (IOException e) {
                Log.w(TAG, "Failed to read EXIF orientation", e);
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                    }
                }
            }
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Can't open uri: " + uri, e);
        } finally {
            dur = System.currentTimeMillis() - dur;
            if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
                Log.v(TAG, "UriImage.getOrientation (exif path) took: " + dur + " ms");
            }
        }
    } else {
        // Try to get the orientation from the ORIENTATION column in the database. This is much
        // faster than reading all the exif tags from the file.
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(uri, new String[] { Images.ImageColumns.ORIENTATION }, null, null, null);
            if (cursor.moveToNext()) {
                int ori = cursor.getInt(0);
                return ori;
            }
        } catch (SQLiteException e) {
        } catch (IllegalArgumentException e) {
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            dur = System.currentTimeMillis() - dur;
            if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
                Log.v(TAG, "UriImage.getOrientation (db column path) took: " + dur + " ms");
            }
        }
    }
    return 0;
}
Also used : InputStream(java.io.InputStream) ExifInterface(com.moez.QKSMS.exif.ExifInterface) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Cursor(android.database.Cursor) SQLiteException(android.database.sqlite.SQLiteException)

Aggregations

Cursor (android.database.Cursor)1 SQLiteException (android.database.sqlite.SQLiteException)1 ExifInterface (com.moez.QKSMS.exif.ExifInterface)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1