Example usage for android.media ExifInterface ExifInterface

List of usage examples for android.media ExifInterface ExifInterface

Introduction

In this page you can find the example usage for android.media ExifInterface ExifInterface.

Prototype

public ExifInterface(InputStream inputStream) throws IOException 

Source Link

Document

Reads Exif tags from the specified image input stream.

Usage

From source file:Main.java

static public Bitmap getOrientedBitmapFromBitmapAndPath(Bitmap bitmap, String filePath) {
    Log.d(TAG, "[AirImagePickerUtils] Entering getOrientedBitmapFromBitmapAndPath");
    try {/* ww  w . j  ava 2s .  c om*/
        // Get orientation from EXIF
        ExifInterface exif = new ExifInterface(filePath);
        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

        // Compute rotation matrix
        Matrix rotation = new Matrix();
        switch (exifOrientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotation.preRotate(90);
            break;

        case ExifInterface.ORIENTATION_ROTATE_180:
            rotation.preRotate(180);
            break;

        case ExifInterface.ORIENTATION_ROTATE_270:
            rotation.preRotate(270);
            break;
        }

        // Return new bitmap
        Log.d(TAG, "[AirImagePickerUtils] Exiting getOrientedBitmapFromBitmapAndPath");
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), rotation, true);
    } catch (Exception exception) {
        Log.d(TAG, "Couldn't fix bitmap orientation: " + exception.getMessage());
        Log.d(TAG, "[AirImagePickerUtils] Exiting getOrientedBitmapFromBitmapAndPath");
        return bitmap;
    }
}

From source file:Main.java

/**
 * This method resets bitmap orientation to 0 if not.
 * /*from  ww  w .  j av  a  2s  .co  m*/
 * @param path
 * @param bitmap
 * @return bitmap with 0 degree orientation
 */
public static Bitmap resetBitmapOrientation(String path, Bitmap bitmap) {
    // TODO Auto-generated method stub
    int rotate = 0;
    try {
        ExifInterface ei = new ExifInterface(path);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        default:
            // do nothing...
            break;
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (rotate == 0) {
        return bitmap;
    } else {
        Matrix m = new Matrix();
        m.postRotate(rotate);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
    }
}

From source file:Main.java

/**
 * Adjust the photo orientation//from  w w w . j  a  v a2 s  .c om
 * @param pathToFile
 * @return
 */
public static Bitmap adjustPhotoOrientation(String pathToFile) {
    try {
        Bitmap bitmap = BitmapFactory.decodeFile(pathToFile);
        ExifInterface exif = new ExifInterface(pathToFile);
        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        int rotate = 0;

        switch (exifOrientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;

        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;

        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        }
        if (rotate != 0) {
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();

            // Setting pre rotate
            Matrix mtx = new Matrix();
            mtx.preRotate(rotate);

            // Rotating Bitmap & convert to ARGB_8888, required by tess
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
            bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
            return bitmap;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Bitmap decodeFileAndResize(File f, int requiredSize) {
    try {//from ww  w  .  j  av a 2s .  com

        int rotation = 0;
        try {
            ExifInterface exif = new ExifInterface(f.getAbsolutePath());
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
            rotation = getRotation(orientation);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = requiredSize;
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }
        // decode with inSampleSize
        return createScaledBitmap(f, rotation, scale);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Returns a matrix with rotation set based on Exif orientation tag.
 * If the orientation is undefined or 0 null is returned.
 *
 * @param pathToOriginal Path to original image file that may have exif data.
 * @return  A rotation in degrees based on exif orientation
 *///w ww  .  j a  va  2 s .c o  m
public static int getOrientation(String pathToOriginal) {
    int degreesToRotate = 0;
    try {
        ExifInterface exif = new ExifInterface(pathToOriginal);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);
        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            degreesToRotate = 90;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            degreesToRotate = 180;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            degreesToRotate = 270;
        }
    } catch (Exception e) {
        if (Log.isLoggable(TAG, Log.ERROR)) {
            Log.e(TAG, "Unable to get orientation for image with path=" + pathToOriginal, e);
        }
    }
    return degreesToRotate;
}

From source file:Main.java

private static String getPicOrientation(String s) {
    try {// ww w . j ava  2  s.  c  om
        return (new ExifInterface(s)).getAttribute("Orientation");
    } catch (IOException e) {
        return "1";
    }

}

From source file:Main.java

public static Bitmap rotateBitmapFromExif(String filePath, Bitmap bitmap) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from  www .ja  va 2s  .  c  o m*/
    BitmapFactory.decodeFile(filePath, options);

    String orientString;
    try {
        ExifInterface exif = new ExifInterface(filePath);
        orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
    } catch (IOException e) {
        e.printStackTrace();
        return bitmap;
    }

    int orientation = (orientString != null) ? Integer.parseInt(orientString)
            : ExifInterface.ORIENTATION_NORMAL;
    int rotationAngle = 0, outHeight = 0, outWidth = 0;
    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotationAngle = 90;
        outHeight = bitmap.getWidth();
        outWidth = bitmap.getHeight();
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotationAngle = 180;
        outHeight = bitmap.getHeight();
        outWidth = bitmap.getWidth();
        break;
    case ExifInterface.ORIENTATION_ROTATE_270:
        rotationAngle = 270;
        outHeight = bitmap.getWidth();
        outWidth = bitmap.getHeight();
        break;
    }

    if (rotationAngle == 0) {
        return bitmap;
    }

    Matrix matrix = new Matrix();
    matrix.setRotate(rotationAngle, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
    Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, outWidth, outHeight, matrix, true);
    if (bitmap != rotateBitmap) {
        bitmap.recycle();
    }

    return rotateBitmap;
}

From source file:Main.java

/**
 * Checks if the orientation of the image is correct, if it's not it will rotate the image
 *
 * @param pBitmap/*from ww w  . j a v a 2 s  . co  m*/
 *        The bitmap to check the orientation for
 * @param pPath
 *        The path to the image, used to load the exif interface
 * @return Bitmap in the correct orientation
 */
public static Bitmap imageOreintationValidator(Bitmap pBitmap, String pPath) {
    ExifInterface ei;
    try {
        ei = new ExifInterface(pPath);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            pBitmap = rotateImage(pBitmap, 90);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            pBitmap = rotateImage(pBitmap, 180);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            pBitmap = rotateImage(pBitmap, 270);
            break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return pBitmap;
}

From source file:Main.java

private static int getOrientationFromExif(Uri imageUri, Context context) {
    int orientation = -1;
    Log.i("Photo Editor", "imageUri = " + imageUri);
    // File imageFile = new File(getRealPathFromUri(imageUri, context));
    File imageFile = new File(imageUri.getPath());
    try {//w w w.j av a  2 s  .  c  o m
        ExifInterface exif;
        Log.i("Photo Editor", "imageFile.getAbsolutePath() = " + imageFile.getAbsolutePath());
        exif = new ExifInterface(imageFile.getAbsolutePath());
        orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return orientation;
}

From source file:Main.java

/**
 * Get the orientation rotate degree of an image file.
 * /*from ww  w  .ja  v a 2s  .co m*/
 * @param soureFilePath
 *            The file path of the image.
 * @return The orientation rotate degree of an image file(90 or 180 or 270). <br>
 *         0, if the image file has no orientation rotate degree or the
 *         source file is not a valid image file.
 */
public static int getOrientationRotateDegree(String soureFilePath) {
    if (null == soureFilePath) {
        return 0;
    }

    try {
        ExifInterface exif = new ExifInterface(soureFilePath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90: {
            return 90;
        }
        case ExifInterface.ORIENTATION_ROTATE_180: {
            return 180;
        }
        case ExifInterface.ORIENTATION_ROTATE_270: {
            return 270;
        }
        default: {
            return 0;
        }
        }

    } catch (IOException e) {
        e.printStackTrace();
        return 0;
    }
}