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;

public class Main {
    private static int sum = 0;

    public static String getHash(Bitmap bitmap) {
        Bitmap temp = Bitmap.createScaledBitmap(bitmap, 8, 8, false);
        int[] grayValues = reduceColor(temp);
        int average = sum / grayValues.length;
        String reslut = computeBits(grayValues, average);
        return reslut;
    }

    private static int[] reduceColor(Bitmap bitmap) {
        sum = 0;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] grayValues = new int[width * height];
        int[] pix = new int[width * height];
        bitmap.getPixels(pix, 0, width, 0, 0, width, height);
        for (int i = 0; i < width; i++)
            for (int j = 0; j < height; j++) {
                int x = j * width + i;
                int r = (pix[x] >> 16) & 0xff;
                int g = (pix[x] >> 8) & 0xff;
                int b = pix[x] & 0xff;
                int grayValue = (r * 30 + g * 59 + b * 11) / 100;
                sum += grayValue;
                grayValues[x] = grayValue;
            }
        return grayValues;
    }

    private static String computeBits(int[] grayValues, int average) {
        char[] result = new char[grayValues.length];
        for (int i = 0; i < grayValues.length; i++) {
            if (grayValues[i] < average)
                result[i] = '0';
            else
                result[i] = '1';
        }
        return new String(result);
    }
}