Image Condense - Android Graphics

Android examples for Graphics:Image Compress

Description

Image Condense

Demo Code


//package com.java2s;
import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

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

import android.util.Log;

public class Main {

    public static boolean imgCondense(String imagePath) {
        // String path = srcFile.getAbsolutePath();
        // File objFile = new File(path);

        Bitmap bmp = null;/*from w w w. j a v a 2s  .  co m*/
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inPreferredConfig = Bitmap.Config.ARGB_4444;
        int inSampleSize;
        opts.inJustDecodeBounds = true;
        bmp = BitmapFactory.decodeFile(imagePath, opts);
        Log.d("opts.outWidth: ", Integer.toString(opts.outWidth));
        if (opts.outWidth > 1000) {
            inSampleSize = computeSampleSize(opts, -1, 450 * 450 * 4);
            if (inSampleSize < 0) {
                inSampleSize = 1;
            }
        } else {
            inSampleSize = 1;
        }
        Log.d("inSampleSize: ", Integer.toString(inSampleSize));
        opts.inSampleSize = inSampleSize;
        opts.inJustDecodeBounds = false;
        bmp = BitmapFactory.decodeFile(imagePath, opts);
        Log.d("bmp width: ", Integer.toString(bmp.getWidth()));
        Log.d("bmp height: ", Integer.toString(bmp.getHeight()));
        File file = new File(imagePath);
        try {
            FileOutputStream out = new FileOutputStream(file);
            if (bmp.compress(Bitmap.CompressFormat.PNG, 100, out)) {
                out.flush();
                out.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

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

        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }

        return roundedSize;
    }

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

        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
                .sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 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 == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }
}

Related Tutorials