Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.math.BigDecimal;

import android.graphics.Bitmap;

import android.graphics.Matrix;

public class Main {
    public static Bitmap compressBitmap(Bitmap bitmap, int width, int height, boolean isAdjust) {
        if (bitmap.getWidth() > width || bitmap.getHeight() > height) {
            float scaleX = new BigDecimal(width).divide(new BigDecimal(bitmap.getWidth()), 4, BigDecimal.ROUND_DOWN)
                    .floatValue();
            float scaleY = new BigDecimal(height)
                    .divide(new BigDecimal(bitmap.getHeight()), 4, BigDecimal.ROUND_DOWN).floatValue();
            if (isAdjust) {
                scaleX = (scaleX < scaleY ? scaleX : scaleY);
                scaleY = scaleX;
            }
            Matrix matrix = new Matrix();
            matrix.postScale(scaleX, scaleY);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        }
        return bitmap;
    }
}