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;

public class Main {

    public static Bitmap Bytes2Bimap(byte[] bytes, int maxSize) {
        try {
            if (bytes == null) {
                return null;
            }

            if (bytes.length != 0) {
                BitmapFactory.Options opt = new BitmapFactory.Options();
                opt.inJustDecodeBounds = true;
                BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opt);

                int scale = 1;

                if ((opt.outHeight > maxSize) || (opt.outWidth > maxSize)) {
                    scale = (int) Math.pow(2, (int) Math.round(
                            Math.log(maxSize / (double) Math.max(opt.outHeight, opt.outWidth)) / Math.log(0.5)));
                }

                BitmapFactory.Options newOpt = new BitmapFactory.Options();
                newOpt.inSampleSize = scale;

                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, newOpt);
            } else {
                return null;
            }
        } catch (Exception ex) {
            return null;
        }
    }
}