Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.graphics.Bitmap;

import android.graphics.Color;

public class Main {
    public static Bitmap toThreshold(Bitmap bmpOriginal) {
        int imageWidth = bmpOriginal.getWidth();
        int imageHeight = bmpOriginal.getHeight();
        Bitmap bmpThreshold = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.RGB_565);
        int[] buffer = new int[imageWidth];

        int grayVal;
        for (int row = 0; row < imageHeight; row++) {
            for (int col = 0; col < imageWidth; col++) {
                grayVal = rgbToGray(bmpOriginal.getPixel(col, row));
                if (grayVal > 125)
                    buffer[col] = Color.rgb(255, 255, 255);
                else
                    buffer[col] = Color.rgb(0, 0, 0);
            }
            bmpThreshold.setPixels(buffer, 0, imageWidth, 0, row, imageWidth, 1);
        }
        return bmpThreshold;
    }

    /***********************************Helper methods*******************************************************/

    static int rgbToGray(int rgb) {
        int r = (rgb >> 16) & 0xff;
        int g = (rgb >> 8) & 0xff;
        int b = rgb & 0xff;
        int k = (int) (.56 * g + .33 * r + .11 * b);
        return k;
    }
}