Android Open Source - ImageFilter Bitmap Utils






From Project

Back to project page ImageFilter.

License

The source code is released under:

GNU General Public License

If you think the Android project ImageFilter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ca.tannerrutgers.ImageFilter.utils;
/*  w  w w.ja  v a  2  s  .c o m*/
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
 * Created by Tanner on 22/01/14.
 */
public class BitmapUtils {

    /**
     * Retrieve Bitmap located at given filepath scaled to reqWidth x requiredHeight
     * @param filepath Filepath of Bitmap
     * @param reqWidth Required width of returned Bitmap
     * @param reqHeight Required height of returned Bitmap
     */
    public static Bitmap decodeSampledBitmapFromFilepath(String filepath, int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filepath, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filepath, options);
    }

    /**
     * Calculates sample size to use in the passed options so that the Bitmap
     * represeneted by options can be scaled to reqWidth and reqHeight
     * @param options Options of corresponding Bitmap
     * @param reqWidth Required width of Bitmap
     * @param reqHeight Required height of Bitmap
     */
    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        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;
            }
        }

        return inSampleSize;
    }

    /**
     * Returns pixel map of given Bitmap
     * @param bitmap Bitmap to retrieve pixels from
     * @return int[] representing pixels of Bitmap
     */
    public static int[] getPixels(Bitmap bitmap) {

        if (bitmap == null) {
            return new int[0];
        }

        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        int[] pixels = new int[width*height];
        bitmap.getPixels(pixels,0,width,0,0,width,height);

        return pixels;
    }
}




Java Source Code List

ca.tannerrutgers.ImageFilter.activities.MainActivity.java
ca.tannerrutgers.ImageFilter.activities.SettingsActivity.java
ca.tannerrutgers.ImageFilter.dialogs.FilterSelectionDialog.java
ca.tannerrutgers.ImageFilter.dialogs.MaskSizePreference.java
ca.tannerrutgers.ImageFilter.models.ImageFilter.java
ca.tannerrutgers.ImageFilter.models.MeanFilter.java
ca.tannerrutgers.ImageFilter.models.MedianFilter.java
ca.tannerrutgers.ImageFilter.utils.BitmapUtils.java