Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {

    public static Bitmap byteToBitmap(byte[] bitmapData) {
        if (bitmapData == null)
            return null;
        Options options = new Options();
        options.inJustDecodeBounds = true;
        int srcWidth = options.outWidth;
        options.inJustDecodeBounds = false;
        int be = 0;
        be = (int) Math.round(((double) srcWidth) / ((double) 80));
        options = new Options();
        options.inSampleSize = be;
        try {
            return BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length, options);
        } catch (OutOfMemoryError e) {
            return null;
        }
    }

    public static Bitmap byteToBitmap(byte[] bitmapData, int size) {
        if (bitmapData == null)
            return null;
        Options options = new Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length, options);
        int w = options.outWidth;
        int h = options.outHeight;
        int sc = 0;
        if (w > size || h > size)
            if (w > h) {
                sc = Math.round((float) w / (float) size);
            } else {
                sc = Math.round((float) h / (float) size);
            }
        options.inJustDecodeBounds = false;
        options.inSampleSize = sc;
        try {
            return BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length, options);
        } catch (OutOfMemoryError e) {
            return null;
        }
    }
}