Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {
    public static Bitmap imageResourceToBitmap(Context c, int res, int maxDim) {
        Bitmap bmp = null;
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        // compute the smallest size bitmap we need to read
        BitmapFactory.decodeResource(c.getResources(), res, opts);
        int w = opts.outWidth;
        int h = opts.outHeight;
        int s = 1;
        while (true) {
            if ((w / 2 < maxDim) || (h / 2 < maxDim)) {
                break;
            }
            w /= 2;
            h /= 2;
            s++;
        }
        // scale and read the data
        opts.inJustDecodeBounds = false;
        opts.inPurgeable = true;
        opts.inSampleSize = s;
        bmp = BitmapFactory.decodeResource(c.getResources(), res, opts);
        return bmp;
    }
}