Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Matrix;

import android.support.annotation.NonNull;
import android.util.Log;

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

    /**
     * This is an expensive operation that copies the image in place with the pixels rotated. If
     * possible rather use getOrientationMatrix, and put that as the imageMatrix on an ImageView.
     *
     * @param imageToOrient   Image Bitmap to orient.
     * @param degreesToRotate number of degrees to rotate the image by. If zero the original image is
     *                        returned unmodified.
     * @return The oriented bitmap. May be the imageToOrient without modification, or a new Bitmap.
     */
    public static Bitmap rotateImage(@NonNull Bitmap imageToOrient, int degreesToRotate) {
        Bitmap result = imageToOrient;
        try {
            if (degreesToRotate != 0) {
                Matrix matrix = new Matrix();
                matrix.setRotate(degreesToRotate);
                result = Bitmap.createBitmap(imageToOrient, 0, 0, imageToOrient.getWidth(),
                        imageToOrient.getHeight(), matrix, true /*filter*/);
            }
        } catch (Exception e) {
            if (Log.isLoggable(TAG, Log.ERROR)) {
                Log.e(TAG, "Exception when trying to orient image", e);
            }
        }
        return result;
    }
}