android.media.ExifInterface

Here are the examples of the java api class android.media.ExifInterface taken from open source projects.

1. ImageEditingManager#copyExif()

Project: react-native
File: ImageEditingManager.java
// Utils
private static void copyExif(Context context, Uri oldImage, File newFile) throws IOException {
    File oldFile = getFileFromUri(context, oldImage);
    if (oldFile == null) {
        FLog.w(ReactConstants.TAG, "Couldn't get real path for uri: " + oldImage);
        return;
    }
    ExifInterface oldExif = new ExifInterface(oldFile.getAbsolutePath());
    ExifInterface newExif = new ExifInterface(newFile.getAbsolutePath());
    for (String attribute : EXIF_ATTRIBUTES) {
        String value = oldExif.getAttribute(attribute);
        if (value != null) {
            newExif.setAttribute(attribute, value);
        }
    }
    newExif.saveAttributes();
}

2. ImageFileUtil#saveExifInterface()

Project: bither-android
File: ImageFileUtil.java
private static void saveExifInterface(File file, int orientation) throws IOException {
    ExifInterface exif = new ExifInterface(file.getAbsolutePath());
    switch(orientation) {
        case 90:
            exif.setAttribute(ExifInterface.TAG_ORIENTATION, Integer.toString(ExifInterface.ORIENTATION_ROTATE_90));
            break;
        case 180:
            exif.setAttribute(ExifInterface.TAG_ORIENTATION, Integer.toString(ExifInterface.ORIENTATION_ROTATE_180));
            break;
        case 270:
            exif.setAttribute(ExifInterface.TAG_ORIENTATION, Integer.toString(ExifInterface.ORIENTATION_ROTATE_270));
            break;
        default:
            exif.setAttribute(ExifInterface.TAG_ORIENTATION, Integer.toString(ExifInterface.ORIENTATION_NORMAL));
            break;
    }
    exif.saveAttributes();
}

3. ImageUtils#getExifOrientation()

Project: WordPress-Android
File: ImageUtils.java
public static int getExifOrientation(String path) {
    ExifInterface exif;
    try {
        exif = new ExifInterface(path);
    } catch (IOException e) {
        AppLog.e(AppLog.T.UTILS, 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;
    }
}

4. ImageUtils#getExifDateTime()

Project: mobile-android
File: ImageUtils.java
/**
     * Returns number of milliseconds since Jan. 1, 1970, midnight. Returns -1
     * if the date time information if not available.
     * 
     * @param attributeName
     * @throws IOException
     */
public static long getExifDateTime(String fileName) throws IOException {
    ExifInterface exif = new ExifInterface(fileName);
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
    long result = getExifDateTime(exif, TAG_DATETIME_ORIGINAL, formatter);
    CommonUtils.debug(TAG, "getExifDateTime: getting %1$s", TAG_DATETIME_ORIGINAL);
    if (result == -1) {
        CommonUtils.debug(TAG, "getExifDateTime: getting %1$s", TAG_DATETIME_DIGITIZED);
        result = getExifDateTime(exif, TAG_DATETIME_DIGITIZED, formatter);
    }
    if (result == -1) {
        CommonUtils.debug(TAG, "getExifDateTime: getting %1$s", TAG_DATETIME);
        result = getExifDateTime(exif, TAG_DATETIME, formatter);
    }
    return result;
}

5. Media#getThumbnail()

Project: LeafPic
File: Media.java
public byte[] getThumbnail() {
    ExifInterface exif;
    try {
        exif = new ExifInterface(getPath());
    } catch (IOException e) {
        return null;
    }
    byte[] imageData = exif.getThumbnail();
    if (imageData != null)
        return imageData;
    return null;
// NOTE: ExifInterface is faster than metadata-extractor to get the thumbnail data
/*try {
            Metadata metadata = ImageMetadataReader.readMetadata(new File(getPath()));
            ExifThumbnailDirectory thumbnailDirectory = metadata.getFirstDirectoryOfType(ExifThumbnailDirectory.class);
            if (thumbnailDirectory.hasThumbnailData())
                return thumbnailDirectory.getThumbnailData();
        } catch (Exception e) { return null; }*/
}

6. ExifUtils#saveAttributes()

Project: ImageViewZoom
File: ExifUtils.java
/**
	 * Store the exif attributes in the passed image file using the TAGS stored in the passed bundle
	 * 
	 * @param filepath
	 * @param bundle
	 * @return true if success
	 */
public static boolean saveAttributes(final String filepath, Bundle bundle) {
    ExifInterface exif;
    try {
        exif = new ExifInterface(filepath);
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    for (String tag : EXIF_TAGS) {
        if (bundle.containsKey(tag)) {
            exif.setAttribute(tag, bundle.getString(tag));
        }
    }
    try {
        exif.saveAttributes();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

7. ExifUtils#loadAttributes()

Project: ImageViewZoom
File: ExifUtils.java
/**
	 * Load the exif tags into the passed Bundle
	 * 
	 * @param filepath
	 * @param out
	 * @return true if exif tags are loaded correctly
	 */
public static boolean loadAttributes(final String filepath, Bundle out) {
    ExifInterface e;
    try {
        e = new ExifInterface(filepath);
    } catch (IOException e1) {
        e1.printStackTrace();
        return false;
    }
    for (String tag : EXIF_TAGS) {
        out.putString(tag, e.getAttribute(tag));
    }
    return true;
}

8. BitmapProcessing#modifyOrientation()

Project: Effects-Pro
File: BitmapProcessing.java
public static Bitmap modifyOrientation(Bitmap bitmap, String image_url) throws IOException {
    ExifInterface ei = new ExifInterface(image_url);
    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    switch(orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            return rotate(bitmap, 90);
        case ExifInterface.ORIENTATION_ROTATE_180:
            return rotate(bitmap, 180);
        case ExifInterface.ORIENTATION_ROTATE_270:
            return rotate(bitmap, 270);
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            return flip(bitmap, true, false);
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            return flip(bitmap, false, true);
        default:
            return bitmap;
    }
}

9. ThreePartImageUtils#newThreePartImageMessage()

Project: Atlas-Android
File: ThreePartImageUtils.java
/**
     * Creates a new ThreePartImage Message.  The full image is attached untouched, while the
     * preview is created from the full image by loading, resizing, and compressing.
     *
     * @param client
     * @param file   Image file
     * @return
     */
private static Message newThreePartImageMessage(Context context, LayerClient client, int exifOrientation, int orientation, File file) throws IOException {
    if (client == null)
        throw new IllegalArgumentException("Null LayerClient");
    if (file == null)
        throw new IllegalArgumentException("Null image file");
    if (!file.exists())
        throw new IllegalArgumentException("No image file");
    if (!file.canRead())
        throw new IllegalArgumentException("Cannot read image file");
    BitmapFactory.Options justBounds = new BitmapFactory.Options();
    justBounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file.getAbsolutePath(), justBounds);
    int fullWidth = justBounds.outWidth;
    int fullHeight = justBounds.outHeight;
    MessagePart full = client.newMessagePart("image/jpeg", new FileInputStream(file), file.length());
    boolean isSwap = orientation == ORIENTATION_270 || orientation == ORIENTATION_90;
    String intoString = "{\"orientation\":" + orientation + ", \"width\":" + (!isSwap ? fullWidth : fullHeight) + ", \"height\":" + (!isSwap ? fullHeight : fullWidth) + "}";
    MessagePart info = client.newMessagePart(MIME_TYPE_INFO, intoString.getBytes());
    if (Log.isLoggable(Log.VERBOSE)) {
        Log.v("Creating image info: " + intoString);
    }
    MessagePart preview;
    if (Log.isLoggable(Log.VERBOSE)) {
        Log.v("Creating Preview from '" + file.getAbsolutePath() + "'");
    }
    // Determine preview size
    int[] previewDim = Util.scaleDownInside(fullWidth, fullHeight, PREVIEW_MAX_WIDTH, PREVIEW_MAX_HEIGHT);
    if (Log.isLoggable(Log.VERBOSE)) {
        Log.v("Preview size: " + previewDim[0] + "x" + previewDim[1]);
    }
    // Determine sample size for preview
    int sampleSize = 1;
    int sampleWidth = fullWidth;
    int sampleHeight = fullHeight;
    while (sampleWidth > previewDim[0] && sampleHeight > previewDim[1]) {
        sampleWidth >>= 1;
        sampleHeight >>= 1;
        sampleSize <<= 1;
    }
    // Back off 1 for scale-down instead of scale-up
    if (sampleSize != 1)
        sampleSize >>= 1;
    BitmapFactory.Options previewOptions = new BitmapFactory.Options();
    previewOptions.inSampleSize = sampleSize;
    if (Log.isLoggable(Log.VERBOSE)) {
        Log.v("Preview sampled size: " + (sampleWidth << 1) + "x" + (sampleHeight << 1));
    }
    // Create preview
    Bitmap sampledBitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), previewOptions);
    Bitmap previewBitmap = Bitmap.createScaledBitmap(sampledBitmap, previewDim[0], previewDim[1], true);
    File temp = new File(context.getCacheDir(), ThreePartImageUtils.class.getSimpleName() + "." + System.nanoTime() + ".jpg");
    FileOutputStream previewStream = new FileOutputStream(temp);
    if (Log.isLoggable(Log.VERBOSE)) {
        Log.v("Compressing preview to '" + temp.getAbsolutePath() + "'");
    }
    previewBitmap.compress(Bitmap.CompressFormat.JPEG, PREVIEW_COMPRESSION_QUALITY, previewStream);
    sampledBitmap.recycle();
    previewBitmap.recycle();
    previewStream.close();
    // Preserve exif orientation
    ExifInterface preserver = new ExifInterface(temp.getAbsolutePath());
    preserver.setAttribute(ExifInterface.TAG_ORIENTATION, Integer.toString(exifOrientation));
    preserver.saveAttributes();
    if (Log.isLoggable(Log.VERBOSE)) {
        Log.v("Exif orientation preserved in preview");
    }
    preview = client.newMessagePart(MIME_TYPE_PREVIEW, new FileInputStream(temp), temp.length());
    if (Log.isLoggable(Log.VERBOSE)) {
        Log.v(String.format(Locale.US, "Full image bytes: %d, preview bytes: %d, info bytes: %d", full.getSize(), preview.getSize(), info.getSize()));
    }
    MessagePart[] parts = new MessagePart[3];
    parts[PART_INDEX_FULL] = full;
    parts[PART_INDEX_PREVIEW] = preview;
    parts[PART_INDEX_INFO] = info;
    return client.newMessage(parts);
}

10. FileRequestHandler#getFileExifRotation()

Project: picasso
File: FileRequestHandler.java
static int getFileExifRotation(Uri uri) throws IOException {
    ExifInterface exifInterface = new ExifInterface(uri.getPath());
    return exifInterface.getAttributeInt(TAG_ORIENTATION, ORIENTATION_NORMAL);
}

11. GetPhotoUtil#getOrientation()

Project: NonViewUtils
File: GetPhotoUtil.java
/**
     * ???????
     * 
     * @param photoUri    
     * @return
     */
public static int getOrientation(final Uri photoUri) {
    ExifInterface exifInterface = null;
    try {
        exifInterface = new ExifInterface(photoUri.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
}

12. ImageUtils#rotateUsingExif()

Project: Snapprefs
File: ImageUtils.java
/**
     * Reads the orientation flag from the Exif-data and rotates and translates the bitmap according to this.
     *
     * @param bitmap The bitmap to be rotated
     * @param path   The path to the image
     * @return The transformed bitmap
     * @throws IOException File not found
     */
public static Bitmap rotateUsingExif(Bitmap bitmap, String path) throws IOException {
    Matrix matrix = new Matrix();
    ExifInterface exifInterface = new ExifInterface(path);
    int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
    XposedUtils.log("Exif rotation tag: " + orientation);
    // See http://sylvana.net/jpegcrop/exif_orientation.html for more information about the values of the Exif Orientation Tag
    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;
        default:
            return bitmap;
    }
    return transformBitmap(bitmap, matrix);
}

13. ImageFileSystemFetcher#getOrientationInDegreesForFileName()

Project: mobile-android
File: ImageFileSystemFetcher.java
/**
     * Get the orientation in degrees from file EXIF information. Idea and code
     * from http://stackoverflow.com/a/11081918/527759
     * 
     * @param fileName
     * @return
     * @throws IOException
     */
public static int getOrientationInDegreesForFileName(String fileName) throws IOException {
    ExifInterface exif = new ExifInterface(fileName);
    int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    int rotationInDegrees = exifToDegrees(rotation);
    return rotationInDegrees;
}

14. StringUtils#getExifOrientation()

Project: LeafPic
File: StringUtils.java
public static int getExifOrientation(String filepath) {
    int degree = 0;
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(filepath);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    if (exif != null) {
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
        if (orientation != -1) {
            // We only recognise a subset of orientation tag values.
            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;
            }
        }
    }
    return degree;
}

15. ExifUtils#getExifOrientation()

Project: ImageViewZoom
File: ExifUtils.java
/**
	 * Return the rotation of the passed image file
	 * 
	 * @param filepath
	 *           image absolute file path
	 * @return image orientation
	 */
public static int getExifOrientation(final String filepath) {
    if (null == filepath)
        return 0;
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(filepath);
    } catch (IOException e) {
        return 0;
    }
    return getExifOrientation(exif);
}

16. ImageHelper#getExifOrientation()

Project: fanfouapp-opensource
File: ImageHelper.java
public static int getExifOrientation(final String filepath) {
    int degree = 0;
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(filepath);
    } catch (final IOException ex) {
        Log.e(ImageHelper.TAG, "cannot read exif", ex);
    }
    if (exif != null) {
        final int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
        if (orientation != -1) {
            // We only recognize a subset of orientation tag values.
            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;
            }
        }
    }
    return degree;
}

17. ImageUtils#getOrientation()

Project: android-utils
File: ImageUtils.java
/**
	 * Gets the orientation of the image pointed to by the parameter uri
	 * 
	 * @return Image orientation value corresponding to <code>ExifInterface.ORIENTATION_*</code> <br/>
	 *         Returns -1 if the row for the {@link android.net.Uri} is not found.
	 **/
public static int getOrientation(Context context, Uri uri) {
    int invalidOrientation = -1;
    if (uri == null) {
        throw new NullPointerException(ERROR_URI_NULL);
    }
    if (!MediaUtils.isMediaContentUri(uri)) {
        return invalidOrientation;
    }
    String filePath = Utils.getPathForMediaUri(context, uri);
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(filePath);
    } catch (IOException e) {
        e.printStackTrace();
    }
    int orientation = invalidOrientation;
    if (exif != null) {
        orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, invalidOrientation);
    }
    return orientation;
}

18. MediaUtils#getExifOrientation()

Project: android-open-project-demo
File: MediaUtils.java
public static int getExifOrientation(String filepath) {
    int degree = 0;
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(filepath);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    if (exif != null) {
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
        if (orientation != -1) {
            // We only recognise a subset of orientation tag values.
            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;
            }
        }
    }
    return degree;
}

19. BitmapUtils#getExifOrientation()

Project: Android-Next
File: BitmapUtils.java
public static int getExifOrientation(String fileName) {
    if (StringUtils.isEmpty(fileName)) {
        return 0;
    }
    int degree = 0;
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(fileName);
    } catch (IOException ex) {
        Log.e(TAG, "cannot read exif", ex);
    }
    if (exif != null) {
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
        if (orientation != -1) {
            // We only recognize a subset of orientation tag values.
            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;
            }
        }
    }
    return degree;
}

20. ImageManager#getExifOrientation()

Project: android-cropimage
File: ImageManager.java
public static int getExifOrientation(String filepath) {
    int degree = 0;
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(filepath);
    } catch (IOException ex) {
        Log.e(TAG, "cannot read exif", ex);
    }
    if (exif != null) {
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
        if (orientation != -1) {
            // We only recognize a subset of orientation tag values.
            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;
            }
        }
    }
    return degree;
}

21. FileSource#loadMetadata()

Project: actor-platform
File: FileSource.java
@Override
protected ImageMetadata loadMetadata() throws ImageLoadException {
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    o.inTempStorage = WorkCache.BITMAP_TMP.get();
    BitmapFactory.decodeFile(fileName, o);
    if (o.outWidth == 0 || o.outHeight == 0) {
        throw new ImageLoadException("BitmapFactory.decodeFile: unable to load file");
    }
    int w = o.outWidth;
    int h = o.outHeight;
    ExifInterface exif = null;
    int orientationTag = 0;
    try {
        exif = new ExifInterface(fileName);
        String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        if (exifOrientation != null) {
            if (exifOrientation.equals("5") || exifOrientation.equals("6") || exifOrientation.equals("7") || exifOrientation.equals("8")) {
                w = o.outHeight;
                h = o.outWidth;
            }
            orientationTag = Integer.parseInt(exifOrientation);
        }
    } catch (IOException e) {
    }
    ImageFormat format = ImageFormat.UNKNOWN;
    if ("image/jpeg".equals(o.outMimeType) || "image/jpg".equals(o.outMimeType)) {
        format = ImageFormat.JPEG;
    } else if ("image/gif".equals(o.outMimeType)) {
        format = ImageFormat.GIF;
    } else if ("image/bmp".equals(o.outMimeType)) {
        format = ImageFormat.BMP;
    } else if ("image/webp".equals(o.outMimeType)) {
        format = ImageFormat.WEBP;
    }
    return new ImageMetadata(w, h, orientationTag, format);
}