Android Open Source - SqrShare Sqr Bitmap Generator






From Project

Back to project page SqrShare.

License

The source code is released under:

<a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc/4.0/88x31.png" /></a><br />...

If you think the Android project SqrShare 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 com.frankegan.sqrshare;
/*  ww  w  .  ja v  a2s.c  o m*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.net.Uri;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author frankegan created on 1/3/15.
 */
public class SqrBitmapGenerator {
    public final static int MAX_RAW_IMG = 640;

    /**
     * Decodes an appropriately sized bitmap from the given Uri to be displayed by an ImageView.
     *
     * @param uri       The URI of the picture to be displayed.
     * @param reqWidth  The requested width of the output image.
     * @param reqHeight The requested height of the output image.
     * @return An appropriately sized bitmap.
     * @throws java.io.IOException if the provided URI could not be opened.
     */
    public static Bitmap decodeSampledBitmapFromUri(Context c, Uri uri, int reqWidth, int reqHeight) throws IOException {

        InputStream input = c.getContentResolver().openInputStream(uri);
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(input, null, options);
        input.close();

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

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        input = c.getContentResolver().openInputStream(uri);
        Bitmap result = BitmapFactory.decodeStream(input, null, options);
        input.close();
        return result;
    }

    /**
     * A method for adding a border to a bitmap so that the resulting image is square.
     *
     * @param source bitmap to be made square.
     * @return the original bitmap with borders added to either the top or bottom
     * such that it is now square.
     */
    public static Bitmap makeSquare(Bitmap source) {
        boolean landscape = source.getWidth() > source.getHeight();
        final int LENGTH = landscape ? source.getWidth() : source.getHeight();
        Rect rect = landscape ?
                new Rect(0, (LENGTH / 2) - (source.getHeight() / 2),
                        LENGTH, (LENGTH / 2) + (source.getHeight() / 2))
                :
                new Rect((LENGTH / 2) - (source.getWidth() / 2), 0,
                        (LENGTH / 2) + (source.getWidth() / 2), LENGTH);
        Bitmap square = Bitmap.createBitmap(LENGTH, LENGTH, source.getConfig());
        Canvas canvas = new Canvas(square);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(source, null, rect, null);
        return square;
    }

    /**
     * A method that calculates how much a bitmap should be scaled down by.
     *
     * @param options   {@link android.graphics.BitmapFactory.Options} with "inJustDecodeBounds"
     *                  set to true that has already been used to decoded a bitmap.
     * @param reqWidth  The requested width of the output image.
     * @param reqHeight The requested height of the output image.
     * @return the nearest power of two that can be used to scale a bitmap to the requested
     * height and width.
     */
    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;
    }

    /**
     * Generates a Square Bitmap from the given Uri.
     *
     * @param c        The context in which to open the InputStream needed to decode the Bitmap.
     * @param imageUri The Uri of the image to be turned into a sqr.
     * @return an optimally scaled Bitmap for displaying in an ImageView.
     */
    public static Bitmap generate(Context c, Uri imageUri) throws IOException {
        return makeSquare(decodeSampledBitmapFromUri(c, imageUri, MAX_RAW_IMG, MAX_RAW_IMG));
    }
}




Java Source Code List

com.frankegan.sqrshare.MainActivity.java
com.frankegan.sqrshare.PictureFragment.java
com.frankegan.sqrshare.PictureHolder.java
com.frankegan.sqrshare.SqrBitmapGenerator.java