Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

import android.graphics.BitmapFactory;

import android.util.Base64;

public class Main {
    public static String bitmapToString(String filePath) {

        Bitmap bm = getSmallBitmap(filePath);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(CompressFormat.JPEG, 40, baos);
        byte[] b = baos.toByteArray();
        return Base64.encodeToString(b, Base64.DEFAULT);
    }

    public static Bitmap getSmallBitmap(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        // Calculate inSampleSize
        //      options.inSampleSize = calculateInSampleSize(options, 300, 300);
        options.inSampleSize = calculateInSampleSize(options, 300, 300, 300);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(filePath, options);
    }

    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

    /**
     * Calculate an inSampleSize for use in a {@link BitmapFactory.Options} object when decoding
     * bitmaps using the decode* methods from {@link BitmapFactory}. This implementation calculates
     * the closest inSampleSize that is a power of 2 and will result in the final decoded bitmap
     * having a width and height equal to or larger than the requested width and height.
     *
     * @param options   An options object with out params already populated (run through a decode
     *                  method with inJustDecodeBounds==true
     * @param reqWidth  The requested width of the resulting bitmap
     * @param reqHeight The requested height of the resulting bitmap
     * @param maxSize   The requested max size of the resulting bitmap
     * @return The value to be used for inSampleSize
     */
    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight,
            int maxSize) {
        // BEGIN_INCLUDE (calculate_sample_size)
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (maxSize > 0) {// only the biggest length(height or width) should not bigger than maxSize
            if (height > width) {
                int reqHeight1 = maxSize;
                if (height > reqHeight) {
                    final int halfHeight = height / 2;

                    // Calculate the largest inSampleSize value that is a power
                    // of 2 and keeps height larger than the requested height.
                    while ((halfHeight / inSampleSize) > reqHeight1) {
                        inSampleSize *= 2;
                    }
                }
            } else {
                int reqWidth1 = maxSize;
                if (width > reqWidth) {
                    final int halfWidth = width / 2;

                    // Calculate the largest inSampleSize value that is a power
                    // of 2 and keeps width larger than the requested width.
                    while ((halfWidth / inSampleSize) > reqWidth1) {
                        inSampleSize *= 2;
                    }
                }
            }
        } else {// both width and height should not bigger than reqWidth and reqHeight
            if (height > reqHeight || width > reqWidth) {
                final int halfHeight = height / 2;
                final int halfWidth = width / 2;

                // Calculate the largest inSampleSize value that is a power of 2 and keeps both
                // height and width larger than the requested height and width.
                while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
                    inSampleSize *= 2;
                }

                // This offers some additional logic in case the image has a strange
                // aspect ratio. For example, a panorama may have a much larger
                // width than height. In these cases the total pixels might still
                // end up being too large to fit comfortably in memory, so we should
                // be more aggressive with sample down the image (=larger inSampleSize).
                /*long totalPixels = width * height / inSampleSize;
                    
                // Anything more than 2x the requested pixels we'll sample down further
                final long totalReqPixelsCap = reqWidth * reqHeight * 2;
                        
                while (totalPixels > totalReqPixelsCap) {
                inSampleSize *= 2;
                totalPixels /= 2;
                }*/
            }
        }
        return inSampleSize;
        // END_INCLUDE (calculate_sample_size)
    }
}