Search in sources :

Example 26 with ExifInterface

use of android.media.ExifInterface in project apps-android-commons by commons-app.

the class GPSExtractor method getCoords.

/**
     * Extracts geolocation (either of image from EXIF data, or of user)
     * @param useGPS set to true if location permissions allowed (by API 23), false if disallowed
     * @return coordinates as string (needs to be passed as a String in API query)
     */
@Nullable
public String getCoords(boolean useGPS) {
    ExifInterface exif;
    String latitude = "";
    String longitude = "";
    String latitude_ref = "";
    String longitude_ref = "";
    String decimalCoords = "";
    try {
        exif = new ExifInterface(filePath);
    } catch (IOException e) {
        Timber.w(e);
        return null;
    } catch (IllegalArgumentException e) {
        Timber.w(e);
        return null;
    }
    //If image has no EXIF data and user has enabled GPS setting, get user's location
    if (exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) == null && useGPS) {
        registerLocationManager();
        imageCoordsExists = false;
        Timber.d("EXIF data has no location info");
        //Check what user's preference is for automatic location detection
        boolean gpsPrefEnabled = gpsPreferenceEnabled();
        //Check that currentLatitude and currentLongitude have been explicitly set by MyLocationListener and do not default to (0.0,0.0)
        if (gpsPrefEnabled && currentLatitude != null && currentLongitude != null) {
            Timber.d("Current location values: Lat = %f Long = %f", currentLatitude, currentLongitude);
            return String.valueOf(currentLatitude) + "|" + String.valueOf(currentLongitude);
        } else {
            // No coords found
            return null;
        }
    } else if (exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) == null) {
        return null;
    } else {
        //If image has EXIF data, extract image coords
        imageCoordsExists = true;
        Timber.d("EXIF data has location info");
        latitude = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
        latitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
        longitude = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
        longitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
        if (latitude != null && latitude_ref != null && longitude != null && longitude_ref != null) {
            Timber.d("Latitude: %s %s", latitude, latitude_ref);
            Timber.d("Longitude: %s %s", longitude, longitude_ref);
            decimalCoords = getDecimalCoords(latitude, latitude_ref, longitude, longitude_ref);
            return decimalCoords;
        } else {
            return null;
        }
    }
}
Also used : ExifInterface(android.media.ExifInterface) IOException(java.io.IOException) Nullable(android.support.annotation.Nullable)

Example 27 with ExifInterface

use of android.media.ExifInterface in project android_frameworks_base by DirtyUnicorns.

the class ExifInterfaceTest method testSaveAttributes_withFileName.

private void testSaveAttributes_withFileName(File imageFile, ExpectedValue expectedValue) throws IOException {
    String verboseTag = imageFile.getName();
    ExifInterface exifInterface = new ExifInterface(imageFile.getAbsolutePath());
    exifInterface.saveAttributes();
    exifInterface = new ExifInterface(imageFile.getAbsolutePath());
    compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
    // Test for modifying one attribute.
    String backupValue = exifInterface.getAttribute(ExifInterface.TAG_MAKE);
    exifInterface.setAttribute(ExifInterface.TAG_MAKE, "abc");
    exifInterface.saveAttributes();
    exifInterface = new ExifInterface(imageFile.getAbsolutePath());
    assertEquals("abc", exifInterface.getAttribute(ExifInterface.TAG_MAKE));
    // Restore the backup value.
    exifInterface.setAttribute(ExifInterface.TAG_MAKE, backupValue);
    exifInterface.saveAttributes();
    exifInterface = new ExifInterface(imageFile.getAbsolutePath());
    compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
}
Also used : ExifInterface(android.media.ExifInterface)

Example 28 with ExifInterface

use of android.media.ExifInterface in project android_frameworks_base by DirtyUnicorns.

the class ExifInterfaceTest method testSaveAttributes_withFileDescriptor.

private void testSaveAttributes_withFileDescriptor(File imageFile, ExpectedValue expectedValue) throws IOException {
    String verboseTag = imageFile.getName();
    FileDescriptor fd = null;
    try {
        fd = Os.open(imageFile.getAbsolutePath(), OsConstants.O_RDWR, 0600);
        ExifInterface exifInterface = new ExifInterface(fd);
        exifInterface.saveAttributes();
        Os.lseek(fd, 0, OsConstants.SEEK_SET);
        exifInterface = new ExifInterface(fd);
        compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
        // Test for modifying one attribute.
        String backupValue = exifInterface.getAttribute(ExifInterface.TAG_MAKE);
        exifInterface.setAttribute(ExifInterface.TAG_MAKE, "abc");
        exifInterface.saveAttributes();
        Os.lseek(fd, 0, OsConstants.SEEK_SET);
        exifInterface = new ExifInterface(fd);
        assertEquals("abc", exifInterface.getAttribute(ExifInterface.TAG_MAKE));
        // Restore the backup value.
        exifInterface.setAttribute(ExifInterface.TAG_MAKE, backupValue);
        exifInterface.saveAttributes();
        Os.lseek(fd, 0, OsConstants.SEEK_SET);
        exifInterface = new ExifInterface(fd);
        compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    } finally {
        IoUtils.closeQuietly(fd);
    }
}
Also used : ErrnoException(android.system.ErrnoException) ExifInterface(android.media.ExifInterface) FileDescriptor(java.io.FileDescriptor)

Example 29 with ExifInterface

use of android.media.ExifInterface in project android_frameworks_base by DirtyUnicorns.

the class DocumentsContract method openImageThumbnail.

/**
     * Open the given image for thumbnail purposes, using any embedded EXIF
     * thumbnail if available, and providing orientation hints from the parent
     * image.
     *
     * @hide
     */
public static AssetFileDescriptor openImageThumbnail(File file) throws FileNotFoundException {
    final ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    Bundle extras = null;
    try {
        final ExifInterface exif = new ExifInterface(file.getAbsolutePath());
        switch(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1)) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                extras = new Bundle(1);
                extras.putInt(EXTRA_ORIENTATION, 90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                extras = new Bundle(1);
                extras.putInt(EXTRA_ORIENTATION, 180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                extras = new Bundle(1);
                extras.putInt(EXTRA_ORIENTATION, 270);
                break;
        }
        final long[] thumb = exif.getThumbnailRange();
        if (thumb != null) {
            return new AssetFileDescriptor(pfd, thumb[0], thumb[1], extras);
        }
    } catch (IOException e) {
    }
    return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH, extras);
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) Bundle(android.os.Bundle) ExifInterface(android.media.ExifInterface) ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException)

Example 30 with ExifInterface

use of android.media.ExifInterface in project android_frameworks_base by AOSPA.

the class DocumentsContract method openImageThumbnail.

/**
     * Open the given image for thumbnail purposes, using any embedded EXIF
     * thumbnail if available, and providing orientation hints from the parent
     * image.
     *
     * @hide
     */
public static AssetFileDescriptor openImageThumbnail(File file) throws FileNotFoundException {
    final ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    Bundle extras = null;
    try {
        final ExifInterface exif = new ExifInterface(file.getAbsolutePath());
        switch(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1)) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                extras = new Bundle(1);
                extras.putInt(EXTRA_ORIENTATION, 90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                extras = new Bundle(1);
                extras.putInt(EXTRA_ORIENTATION, 180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                extras = new Bundle(1);
                extras.putInt(EXTRA_ORIENTATION, 270);
                break;
        }
        final long[] thumb = exif.getThumbnailRange();
        if (thumb != null) {
            return new AssetFileDescriptor(pfd, thumb[0], thumb[1], extras);
        }
    } catch (IOException e) {
    }
    return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH, extras);
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) Bundle(android.os.Bundle) ExifInterface(android.media.ExifInterface) ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException)

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