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.BitmapFactory;
import android.util.Log;

public class Main {
    public static final String TAG = "BitmapUtil";
    public static final int MAX_WIDTH = 800;
    public static final int MAX_HEIGHT = 800;

    public static Bitmap getUploadBitmap(String imagePath) {
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imagePath, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;

        /* Figure out which way needs to be reduced less */
        int scaleFactor;
        if (photoW < MAX_WIDTH && photoH < MAX_HEIGHT) {
            scaleFactor = 1;
        } else {
            scaleFactor = Math.max(photoW / MAX_WIDTH, photoH / MAX_HEIGHT);
        }
        Log.d(TAG, "scaleFactor:" + scaleFactor);
        /* Set bitmap options to scale the image decode target */
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        /* Decode the JPEG file into a Bitmap */
        return BitmapFactory.decodeFile(imagePath, bmOptions);

    }
}