FileDocCategorySizeDatePackage
ImageUtils.javaAPI DocAndroid 5.1 API4414Thu Mar 12 22:22:10 GMT 2015com.android.internal.util

ImageUtils

public class ImageUtils extends Object
Utility class for image analysis and processing.
hide

Fields Summary
private static final int
TOLERANCE
private static final int
ALPHA_TOLERANCE
private static final int
COMPACT_BITMAP_SIZE
private int[]
mTempBuffer
private android.graphics.Bitmap
mTempCompactBitmap
private android.graphics.Canvas
mTempCompactBitmapCanvas
private android.graphics.Paint
mTempCompactBitmapPaint
private final android.graphics.Matrix
mTempMatrix
Constructors Summary
Methods Summary
private voidensureBufferSize(int size)
Makes sure that {@code mTempBuffer} has at least length {@code size}.

        if (mTempBuffer == null || mTempBuffer.length < size) {
            mTempBuffer = new int[size];
        }
    
public booleanisGrayscale(android.graphics.Bitmap bitmap)
Checks whether a bitmap is grayscale. Grayscale here means "very close to a perfect gray". Instead of scanning every pixel in the bitmap, we first resize the bitmap to no more than COMPACT_BITMAP_SIZE^2 pixels using filtering. The hope is that any non-gray color elements will survive the squeezing process, contaminating the result with color.


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

        // shrink to a more manageable (yet hopefully no more or less colorful) size
        if (height > COMPACT_BITMAP_SIZE || width > COMPACT_BITMAP_SIZE) {
            if (mTempCompactBitmap == null) {
                mTempCompactBitmap = Bitmap.createBitmap(
                        COMPACT_BITMAP_SIZE, COMPACT_BITMAP_SIZE, Bitmap.Config.ARGB_8888
                );
                mTempCompactBitmapCanvas = new Canvas(mTempCompactBitmap);
                mTempCompactBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
                mTempCompactBitmapPaint.setFilterBitmap(true);
            }
            mTempMatrix.reset();
            mTempMatrix.setScale(
                    (float) COMPACT_BITMAP_SIZE / width,
                    (float) COMPACT_BITMAP_SIZE / height,
                    0, 0);
            mTempCompactBitmapCanvas.drawColor(0, PorterDuff.Mode.SRC); // select all, erase
            mTempCompactBitmapCanvas.drawBitmap(bitmap, mTempMatrix, mTempCompactBitmapPaint);
            bitmap = mTempCompactBitmap;
            width = height = COMPACT_BITMAP_SIZE;
        }

        final int size = height*width;
        ensureBufferSize(size);
        bitmap.getPixels(mTempBuffer, 0, width, 0, 0, width, height);
        for (int i = 0; i < size; i++) {
            if (!isGrayscale(mTempBuffer[i])) {
                return false;
            }
        }
        return true;
    
public static booleanisGrayscale(int color)
Classifies a color as grayscale or not. Grayscale here means "very close to a perfect gray"; if all three channels are approximately equal, this will return true. Note that really transparent colors are always grayscale.

        int alpha = 0xFF & (color >> 24);
        if (alpha < ALPHA_TOLERANCE) {
            return true;
        }

        int r = 0xFF & (color >> 16);
        int g = 0xFF & (color >> 8);
        int b = 0xFF & color;

        return Math.abs(r - g) < TOLERANCE
                && Math.abs(r - b) < TOLERANCE
                && Math.abs(g - b) < TOLERANCE;