Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.*;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.ImageColumns;

import android.util.Log;
import java.io.*;

public class Main {
    private static final int UNCONSTRAINED = -1;
    public static int MAX_SIZE = 1024 * 1024;
    public static final Bitmap EMPTY_BITMAP = Bitmap.createBitmap(1, 1, Config.ARGB_8888);

    public static Bitmap load(ContentResolver contentResolver, Uri uri) {
        return load(contentResolver, uri, UNCONSTRAINED, MAX_SIZE);
    }

    public static Bitmap load(ContentResolver contentResolver, Uri uri, int minSideLen, int maxSize) {

        Bitmap result = EMPTY_BITMAP;
        try {
            Options opts = new Options();
            opts.inJustDecodeBounds = true;

            BitmapFactory.decodeStream(contentResolver.openInputStream(uri), null, opts);

            result = load(contentResolver.openInputStream(uri), computeSampleSize(opts, minSideLen, maxSize));

            if (isMediaUri(uri) && result != null && result != EMPTY_BITMAP) {
                result = rotateMediaImage(contentResolver, uri, result);
            }

        } catch (OutOfMemoryError e) {
            Log.e("LoadImage", e.getMessage(), e);
        } catch (IOException e) {
            Log.e("LoadImage", e.getMessage(), e);
        }
        if (result == null) {
            result = EMPTY_BITMAP;
        }
        return result;
    }

    public static Bitmap load(File file, int minSideLen, int maxSize) throws IOException {

        FileInputStream is = null;
        try {
            Options opts = new Options();
            opts.inJustDecodeBounds = true;

            is = new FileInputStream(file);
            BitmapFactory.decodeStream(is, null, opts);
            is.close();

            is = new FileInputStream(file);
            return load(is, computeSampleSize(opts, minSideLen, maxSize));
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }

    public static Bitmap load(byte[] data, int inSampleSize) {
        try {
            Options opts = new Options();
            opts.inSampleSize = inSampleSize;
            opts.inDither = false;
            opts.inPreferredConfig = Config.ARGB_8888;

            Bitmap result = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
            return result == null ? EMPTY_BITMAP : result;
        } catch (OutOfMemoryError e) {
            Log.e("LoadImage", e.getMessage(), e);
        }
        return EMPTY_BITMAP;
    }

    public static Bitmap load(InputStream inputStream, int inSampleSize) {
        try {
            Options opts = new Options();
            opts.inSampleSize = inSampleSize;
            opts.inDither = false;
            opts.inPreferredConfig = Config.ARGB_8888;

            Bitmap result = BitmapFactory.decodeStream(inputStream, null, opts);
            return result == null ? EMPTY_BITMAP : result;
        } catch (OutOfMemoryError e) {
            Log.e("LoadImage", e.getMessage(), e);
        }
        return EMPTY_BITMAP;
    }

    public static int computeSampleSize(Options options, int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);

        int roundedSize = nextPowerOfTwo(initialSize);
        return roundedSize > initialSize ? roundedSize / 2 : roundedSize;
    }

    private static boolean isMediaUri(Uri uri) {
        return uri != null && ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())
                && MediaStore.AUTHORITY.equals(uri.getAuthority());
    }

    private static Bitmap rotateMediaImage(ContentResolver contentResolver, Uri uri, Bitmap image)
            throws FileNotFoundException {

        Cursor c = contentResolver.query(uri, null, null, null, null);
        try {
            if (c != null) {
                if (c.moveToFirst()) {

                    int index = c.getColumnIndex(ImageColumns.ORIENTATION);
                    int degrees = index < 0 ? 0 : c.getInt(index);

                    if (degrees != 0) {
                        return rotate(image, degrees);
                    }
                    return image;
                }
            }
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return image;
    }

    private static int computeInitialSampleSize(Options options, int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;

        int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == UNCONSTRAINED) ? 128
                : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));

        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }

        if ((maxNumOfPixels == UNCONSTRAINED) && (minSideLength == UNCONSTRAINED)) {
            return 1;
        } else if (minSideLength == UNCONSTRAINED) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }

    public static int nextPowerOfTwo(int value) {
        if (value == 0) {
            return 1;
        } else {
            --value;
            value |= value >> 1;
            value |= value >> 2;
            value |= value >> 4;
            value |= value >> 8;
            value |= value >> 16;
            return value + 1;
        }
    }

    public static Bitmap rotate(Bitmap b, int degrees) {
        if (degrees != 0 && b != null) {
            Matrix m = new Matrix();
            m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
            try {
                Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
                if (b != b2) {
                    b.recycle();
                    b = b2;
                }
            } catch (OutOfMemoryError ex) {
                // We have no memory to rotate. Return the original bitmap.
            }
        }
        return b;
    }
}