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 android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.LightingColorFilter;

import android.graphics.Paint;

import android.graphics.Rect;

import android.util.TypedValue;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class Main {
    /**
     * ---------------------------------------------------------------------------
     * USE THIS METHOD AND NOT THE OLDER VERSION CALLED: "overlayColorOnGrayScale"
     * ---------------------------------------------------------------------------
     * Method to overlay color on a gray scale Bitmap.
     * This method creates automatically a gray scale bitmap from {@code source}.
     *
     * @param source The original colored Bitmap.
     * @param color  Color to overlay.
     * @return A colored gray scale Bitmap.
     */
    public static Bitmap overlayColorOnGrayScale(Bitmap source, int color) {
        Bitmap newBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        Bitmap mutableBitmap = newBitmap.copy(Bitmap.Config.ARGB_8888, true);

        Canvas canvas = new Canvas(mutableBitmap);
        canvas.drawBitmap(source, 0, 0, getGrayScalePaint());

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        ColorFilter filter = new LightingColorFilter(color, 1);
        paint.setColorFilter(filter);
        canvas.drawBitmap(mutableBitmap, 0, 0, paint);

        return mutableBitmap;
    }

    /**
     * Method to overlay a color on a gray scale Bitmap, passed as a resource id {@code id}.
     * If you want to call this method from a Fragment/Activity call it in this way:
     * overlayColorOnGrayScale(getResources(), R.drawable.your_image, Color.RED)
     *
     * @param res   A reference to Resources.
     * @param id    The id of a drawable image.
     * @param color Color to overlay.
     * @return A colored gray scale Bitmap.
     * @throws IOException
     */
    public static Bitmap overlayColorOnGrayScale(Resources res, int id, int color) throws IOException {
        Bitmap mutableBitmap = getMutableBitmap(res, id);

        Canvas canvas = new Canvas(mutableBitmap);
        canvas.drawBitmap(mutableBitmap, 0, 0, getGrayScalePaint());

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        ColorFilter filter = new LightingColorFilter(color, 1);
        paint.setColorFilter(filter);
        canvas.drawBitmap(mutableBitmap, 0, 0, paint);

        return mutableBitmap;
    }

    private static Paint getGrayScalePaint() {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(cm);
        paint.setColorFilter(colorMatrixColorFilter);
        return paint;
    }

    /**
     * Method to get a mutable bitmap.
     *
     * @param immutable Bitmap to convert.
     * @return A mutable Bitmap.
     * @throws IOException
     */
    private static Bitmap getMutableBitmap(Bitmap immutable) throws IOException {
        byte[] byteArray = toByteArrayNew(immutable);
        Bitmap result = getMutableBitmap(byteArray, 0, byteArray.length);
        byteArray = null;
        return result;
    }

    private static Bitmap getMutableBitmap(byte[] data, int offset, int length) {
        return BitmapFactory.decodeByteArray(data, offset, length, getMutableOption());
    }

    private static Bitmap getMutableBitmap(String filePath) {
        return BitmapFactory.decodeFile(filePath, getMutableOption());
    }

    private static Bitmap getMutableBitmap(FileDescriptor fd, Rect outPadding) {
        return BitmapFactory.decodeFileDescriptor(fd, outPadding, getMutableOption());
    }

    private static Bitmap getMutableBitmap(Resources res, int id) {
        return BitmapFactory.decodeResource(res, id, getMutableOption());
    }

    private static Bitmap getMutableBitmap(Resources res, TypedValue value, InputStream is, Rect pad) {
        return BitmapFactory.decodeResourceStream(res, value, is, pad, getMutableOption());
    }

    private static Bitmap getMutableBitmap(InputStream is, Rect outPadding) {
        return BitmapFactory.decodeStream(is, outPadding, getMutableOption());
    }

    public static byte[] toByteArrayNew(Bitmap source) {
        //int size = source.getRowBytes() * source.getHeight();
        int size = source.getByteCount();
        ByteBuffer byteBuffer = ByteBuffer.allocate(size);
        source.copyPixelsToBuffer(byteBuffer);
        byteBuffer.rewind();
        byte[] b = byteBuffer.array();
        return b;
    }

    private static BitmapFactory.Options getMutableOption() {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inMutable = true;
        return opt;
    }
}