Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.FileOutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {
    public static void resizeSavedBitmap(String path, int maxSide, String dest) {
        System.out.println("Resizing");
        Bitmap resizedBitmap = resizeBitmap(path, maxSide);
        saveBitmap(resizedBitmap, dest);
    }

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

        boolean landscape = true;
        if (photoW < photoH) {
            // portrait
            landscape = false;
        }

        int scaleFactor = 1;
        if (landscape) {
            scaleFactor = photoW / maxSide;
        } else {
            scaleFactor = photoH / maxSide;
        }

        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;

        return BitmapFactory.decodeFile(photoPath, bmOptions);
    }

    public static void saveBitmap(Bitmap bitmap, String path) {
        try {
            FileOutputStream out = new FileOutputStream(path);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}