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 boolean scaleImage(String origin_path, String result_path, int target_size) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(origin_path, options);
        int imageHeight = options.outHeight;
        int imageWidth = options.outWidth;
        int scale = (int) (Math.sqrt(imageWidth * imageHeight * 4 / target_size)) + 1;
        Bitmap bmp = null;
        options.inJustDecodeBounds = false;
        options.inSampleSize = scale;
        bmp = BitmapFactory.decodeFile(origin_path, options);

        FileOutputStream out = null;
        try {
            out = new FileOutputStream(result_path);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                bmp.recycle();
                if (out != null) {
                    out.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}