Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.IOException;
import android.content.Context;
import android.database.Cursor;

import android.media.ExifInterface;
import android.net.Uri;

import android.provider.MediaStore.Images;
import android.util.Log;

public class Main {
    private final static String TAG = "CameraUtils";

    /**
     * Calculates the amount of rotation needed for the image to look upright.
     * 
     * @param context
     *            the application's context
     * @param uri
     *            the Uri pointing to the file
     * @return the needed rotation as a <code>float</code>
     */
    private static float rotationForImage(Context context, Uri uri) {
        if ("content".equals(uri.getScheme())) {
            String[] projection = { Images.ImageColumns.ORIENTATION };
            Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
            if (c.moveToFirst()) {
                return c.getInt(0);
            }
        } else if ("file".equals(uri.getScheme())) {
            try {
                ExifInterface exif = new ExifInterface(uri.getPath());
                int rotation = (int) exifOrientationToDegrees(
                        exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
                return rotation;
            } catch (IOException e) {
                Log.e(TAG, "Error checking exif", e);
            }
        }
        return 0f;
    }

    /**
     * Converts the given Exif Orientation to a degree for rotation.
     * 
     * @param exifOrientation
     *            the ExifInterface code for the orientation
     * @return the rotation as a <code>float</code>
     */
    private static float exifOrientationToDegrees(int exifOrientation) {
        if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
            return 90;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
            return 180;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
            return 270;
        }
        return 0;
    }
}