Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;

public class Main {
    public static Bitmap resize(Bitmap src, int w2, int h2) {
        int w1 = src.getWidth();
        int h1 = src.getHeight();
        int[] pxSource = new int[w1 * h1];
        int[] pxResult = new int[w2 * h2];

        src.getPixels(pxSource, 0, w1, 0, 0, w1, h1);
        double x_ratio = w1 / (double) w2;
        double y_ratio = h1 / (double) h2;
        double px, py;
        for (int i = 0; i < h2; i++) {
            for (int j = 0; j < w2; j++) {
                px = Math.floor(j * x_ratio);
                py = Math.floor(i * y_ratio);
                pxResult[(i * w2) + j] = pxSource[(int) ((py * w1) + px)];
            }
        }

        return Bitmap.createBitmap(pxResult, w2, h2, Config.ARGB_8888);
    }
}