Search in sources :

Example 1 with ExifInterface

use of android.support.media.ExifInterface in project Tusky by Vavassor.

the class DownsizeImageTask method getOrientation.

private static int getOrientation(Uri uri, ContentResolver contentResolver) {
    InputStream inputStream;
    try {
        inputStream = contentResolver.openInputStream(uri);
    } catch (FileNotFoundException e) {
        Log.d(TAG, Log.getStackTraceString(e));
        return ExifInterface.ORIENTATION_UNDEFINED;
    }
    if (inputStream == null) {
        return ExifInterface.ORIENTATION_UNDEFINED;
    }
    ExifInterface exifInterface;
    try {
        exifInterface = new ExifInterface(inputStream);
    } catch (IOException e) {
        Log.d(TAG, Log.getStackTraceString(e));
        IOUtils.closeQuietly(inputStream);
        return ExifInterface.ORIENTATION_UNDEFINED;
    }
    int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    IOUtils.closeQuietly(inputStream);
    return orientation;
}
Also used : InputStream(java.io.InputStream) ExifInterface(android.support.media.ExifInterface) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 2 with ExifInterface

use of android.support.media.ExifInterface in project muzei by romannurik.

the class GalleryArtSource method ensureMetadataExists.

private void ensureMetadataExists(@NonNull Uri imageUri) {
    Cursor existingMetadata = getContentResolver().query(GalleryContract.MetadataCache.CONTENT_URI, new String[] { BaseColumns._ID }, GalleryContract.MetadataCache.COLUMN_NAME_URI + "=?", new String[] { imageUri.toString() }, null);
    if (existingMetadata == null) {
        return;
    }
    boolean metadataExists = existingMetadata.moveToFirst();
    existingMetadata.close();
    if (!metadataExists) {
        // No cached metadata or it's stale, need to pull it separately using Exif
        ContentValues values = new ContentValues();
        values.put(GalleryContract.MetadataCache.COLUMN_NAME_URI, imageUri.toString());
        try (InputStream in = getContentResolver().openInputStream(imageUri)) {
            if (in == null) {
                return;
            }
            ExifInterface exifInterface = new ExifInterface(in);
            String dateString = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
            if (!TextUtils.isEmpty(dateString)) {
                Date date = sExifDateFormat.parse(dateString);
                values.put(GalleryContract.MetadataCache.COLUMN_NAME_DATETIME, date.getTime());
            }
            float[] latlong = new float[2];
            if (exifInterface.getLatLong(latlong)) {
                // Reverse geocode
                List<Address> addresses = null;
                try {
                    addresses = mGeocoder.getFromLocation(latlong[0], latlong[1], 1);
                } catch (IllegalArgumentException e) {
                    Log.w(TAG, "Invalid latitude/longitude, skipping location metadata", e);
                }
                if (addresses != null && addresses.size() > 0) {
                    Address addr = addresses.get(0);
                    String locality = addr.getLocality();
                    String adminArea = addr.getAdminArea();
                    String countryCode = addr.getCountryCode();
                    StringBuilder sb = new StringBuilder();
                    if (!TextUtils.isEmpty(locality)) {
                        sb.append(locality);
                    }
                    if (!TextUtils.isEmpty(adminArea)) {
                        if (sb.length() > 0) {
                            sb.append(", ");
                        }
                        sb.append(adminArea);
                    }
                    if (!TextUtils.isEmpty(countryCode) && !sOmitCountryCodes.contains(countryCode)) {
                        if (sb.length() > 0) {
                            sb.append(", ");
                        }
                        sb.append(countryCode);
                    }
                    values.put(GalleryContract.MetadataCache.COLUMN_NAME_LOCATION, sb.toString());
                }
            }
            getContentResolver().insert(GalleryContract.MetadataCache.CONTENT_URI, values);
        } catch (ParseException | IOException | NumberFormatException | StackOverflowError e) {
            Log.w(TAG, "Couldn't read image metadata.", e);
        }
    }
}
Also used : ContentValues(android.content.ContentValues) Address(android.location.Address) InputStream(java.io.InputStream) ExifInterface(android.support.media.ExifInterface) IOException(java.io.IOException) Cursor(android.database.Cursor) Date(java.util.Date) ParseException(java.text.ParseException)

Example 3 with ExifInterface

use of android.support.media.ExifInterface in project collect by opendatakit.

the class ImageConverter method rotateImageIfNeeded.

/**
 * Sometimes an image might be taken up sideways.
 * https://github.com/opendatakit/collect/issues/36
 */
private static void rotateImageIfNeeded(String imagePath) {
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(imagePath);
    } catch (IOException e) {
        Timber.w(e);
    }
    if (exif != null) {
        Bitmap image = FileUtils.getBitmap(imagePath, new BitmapFactory.Options());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        switch(orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotateBitmap(image, 90, imagePath);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotateBitmap(image, 180, imagePath);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotateBitmap(image, 270, imagePath);
                break;
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) ExifInterface(android.support.media.ExifInterface) IOException(java.io.IOException) BitmapFactory(android.graphics.BitmapFactory)

Example 4 with ExifInterface

use of android.support.media.ExifInterface in project StreetComplete by westnordost.

the class AttachPhotoUtils method getRotationMatrix.

private static Matrix getRotationMatrix(String imagePath) {
    int orientation = 0;
    try {
        ExifInterface exifInterface = new ExifInterface(imagePath);
        orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
    } catch (IOException e) {
    }
    Matrix matrix = new Matrix();
    switch(orientation) {
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            matrix.setScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.setRotate(180);
            break;
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            matrix.setRotate(180);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_TRANSPOSE:
            matrix.setRotate(90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.setRotate(90);
            break;
        case ExifInterface.ORIENTATION_TRANSVERSE:
            matrix.setRotate(-90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.setRotate(-90);
            break;
    }
    return matrix;
}
Also used : Matrix(android.graphics.Matrix) ExifInterface(android.support.media.ExifInterface) IOException(java.io.IOException)

Example 5 with ExifInterface

use of android.support.media.ExifInterface in project Tusky by tuskyapp.

the class DownsizeImageTask method getOrientation.

private static int getOrientation(Uri uri, ContentResolver contentResolver) {
    InputStream inputStream;
    try {
        inputStream = contentResolver.openInputStream(uri);
    } catch (FileNotFoundException e) {
        Log.d(TAG, Log.getStackTraceString(e));
        return ExifInterface.ORIENTATION_UNDEFINED;
    }
    if (inputStream == null) {
        return ExifInterface.ORIENTATION_UNDEFINED;
    }
    ExifInterface exifInterface;
    try {
        exifInterface = new ExifInterface(inputStream);
    } catch (IOException e) {
        Log.d(TAG, Log.getStackTraceString(e));
        IOUtils.closeQuietly(inputStream);
        return ExifInterface.ORIENTATION_UNDEFINED;
    }
    int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    IOUtils.closeQuietly(inputStream);
    return orientation;
}
Also used : InputStream(java.io.InputStream) ExifInterface(android.support.media.ExifInterface) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Aggregations

ExifInterface (android.support.media.ExifInterface)26 IOException (java.io.IOException)21 InputStream (java.io.InputStream)12 Bitmap (android.graphics.Bitmap)4 Date (java.util.Date)4 BitmapFactory (android.graphics.BitmapFactory)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 ParseException (java.text.ParseException)3 SimpleDateFormat (java.text.SimpleDateFormat)3 ContentResolver (android.content.ContentResolver)2 AssetFileDescriptor (android.content.res.AssetFileDescriptor)2 Resources (android.content.res.Resources)2 Cursor (android.database.Cursor)2 Matrix (android.graphics.Matrix)2 Point (android.graphics.Point)2 Uri (android.net.Uri)2 Bundle (android.os.Bundle)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Locale (java.util.Locale)2