Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.graphics.Bitmap;

import android.graphics.Color;

public class Main {
    public static String getMainColor(Bitmap bitmap) {
        int[] colorSum = getRGBSum(bitmap);

        int tmpVal = 0;
        int maxKey = 0;
        for (int i = 0; i < colorSum.length; i++) {
            if (tmpVal < colorSum[i]) {
                tmpVal = colorSum[i];
                maxKey = i;
            }
        }

        String[] colorArray = { "RED", "GREEN", "BLUE" };
        return colorArray[maxKey];
    }

    public static int[] getRGBSum(Bitmap bitmap) {
        int height = bitmap.getHeight();
        int width = bitmap.getWidth();
        int[] pixels = new int[(width * height)];
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

        int[] sum = new int[3];
        for (int YY = 0; YY < width; ++YY) {
            for (int XX = 0; XX < height; ++XX) {
                int bitmapColor = pixels[(YY + XX * width)];
                sum[0] += Color.red(bitmapColor);
                sum[1] += Color.green(bitmapColor);
                sum[2] += Color.blue(bitmapColor);
            }
        }

        return sum;
    }
}