Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Sorin Otescu <sorin.otescu@gmail.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.media.ExifInterface;

import android.util.Log;

public class Main {
    public static Bitmap createCorrectlyRotatedBitmapIfNeeded(Bitmap bitmap, String jpegPath, float scale) {
        // Rotate the image if necessary; all images are shot in LANDSCAPE mode
        try {
            ExifInterface exif = new ExifInterface(jpegPath);

            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
            Log.d("CashLensUtils", "image has orientation " + Integer.toString(orientation) + ", width "
                    + Integer.toString(bitmap.getWidth()) + ", height " + Integer.toString(bitmap.getHeight()));

            Matrix matrix = new Matrix();

            if (scale != 1.0f)
                matrix.preScale(scale, scale);

            // From http://sylvana.net/jpegcrop/exif_orientation.html
            // For convenience, here is what the letter F would look like if it were tagged correctly 
            // and displayed by a program that ignores the orientation tag (thus showing the stored image):
            //   (1)       2      (3)      4         5          (6)          7         (8)
            //
            //   888888  888888      88  88      8888888888  88                  88  8888888888
            //   88          88      88  88      88  88      88  88          88  88      88  88
            //   8888      8888    8888  8888    88          8888888888  8888888888          88
            //   88          88      88  88
            //   88          88  888888  888888

            if (orientation == 3)
                matrix.postRotate(180);
            else if (orientation == 6)
                matrix.postRotate(90);
            else if (orientation == 8)
                matrix.postRotate(-90);

            if (orientation != 1 || scale != 1.0f) {
                // Create a new image with the correct (maybe rotated) width/height
                Bitmap newImage = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
                        true);

                Log.d("CashLensUtils", "created a new image with width " + Integer.toString(newImage.getWidth())
                        + ", height " + Integer.toString(newImage.getHeight()));

                // Replace original image
                return newImage;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bitmap;
    }
}