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.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;

public class Main {
    public static Bitmap subtract(Bitmap bitmap1, Bitmap bitmap2) {
        int width = bitmap1.getWidth();
        int height = bitmap1.getHeight();
        Bitmap grayBitmap1 = toGrayScale(bitmap1);
        Bitmap grayBitmap2 = toGrayScale(bitmap2);
        Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        for (int x = 0; x < width; x++)
            for (int y = 0; y < height; y++) {
                int value1 = Color.blue(grayBitmap1.getPixel(x, y));
                int value2 = Color.blue(grayBitmap2.getPixel(x, y));
                int resultValue = Math.abs(value2 - value1);
                //                resultValue = (resultValue>255/3)?resultValue:0;
                result.setPixel(x, y, Color.rgb(resultValue, resultValue, resultValue));
            }
        //        grayBitmap1.recycle();
        //        grayBitmap2.recycle();
        return result;
    }

    public static Bitmap toGrayScale(Bitmap bmpOriginal) {
        int width, height;
        height = bmpOriginal.getHeight();
        width = bmpOriginal.getWidth();

        Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        Canvas c = new Canvas(bmpGrayscale);
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        paint.setColorFilter(f);
        c.drawBitmap(bmpOriginal, 0, 0, paint);
        return bmpGrayscale;
    }
}