Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.view.Surface;

public class Main {
    public static Bitmap getRotatedBitmap(Bitmap imageBitmap, int screenRotation) {
        int rotationAdjustment = getRotationAdjustment(screenRotation);
        Matrix matrix = new Matrix();
        matrix.postRotate(rotationAdjustment);
        return Bitmap.createBitmap(imageBitmap, 0, 0, imageBitmap.getWidth(), imageBitmap.getHeight(), matrix,
                true);
    }

    public static int getRotationAdjustment(int screenRotation) {
        int degrees = 0;
        switch (screenRotation) {
        case Surface.ROTATION_0:
            degrees = 0;
            break;
        case Surface.ROTATION_90:
            degrees = 90;
            break;
        case Surface.ROTATION_180:
            degrees = 180;
            break;
        case Surface.ROTATION_270:
            degrees = 270;
            break;
        }

        int cameraOrientation = getCameraOrientation();
        return (cameraOrientation - degrees + 360) % 360;
    }

    public static int getCameraOrientation() {
        Camera.CameraInfo cameraInfo = getCameraInfo();
        return cameraInfo.orientation;
    }

    private static Camera.CameraInfo getCameraInfo() {
        Camera.CameraInfo info = new Camera.CameraInfo();
        for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
            Camera.getCameraInfo(i, info);
            if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                break;
            }
        }
        return info;
    }
}