Android Bitmap Color Get getColorHistogram(Bitmap bitmap)

Here you can find the source of getColorHistogram(Bitmap bitmap)

Description

get Color Histogram

License

Apache License

Declaration

public static int[] getColorHistogram(Bitmap bitmap) 

Method Source Code

//package com.java2s;
/*/*from   w  w w.  j  ava  2 s  .  c  o m*/
 * Copyright (C) 2013 www.418log.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.graphics.Bitmap;

public class Main {

    public static int[] getColorHistogram(Bitmap bitmap) {

        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        //?????
        int[] areaColor = new int[64];

        //?????????
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int pixels = bitmap.getPixel(i, j);
                int alpha = (pixels >> 24) & 0xFF;
                int red = (pixels >> 16) & 0xFF;
                int green = (pixels >> 8) & 0xFF;
                int blue = (pixels) & 0xFF;
                int redArea = 0;
                int greenArea = 0;
                int blueArea = 0;
                //0-63   64-127   128-191   192-255
                if (red >= 192) {
                    redArea = 3;
                } else if (red >= 128) {
                    redArea = 2;
                } else if (red >= 64) {
                    redArea = 1;
                } else if (red >= 0) {
                    redArea = 0;
                }

                if (green >= 192) {
                    greenArea = 3;
                } else if (green >= 128) {
                    greenArea = 2;
                } else if (green >= 64) {
                    greenArea = 1;
                } else if (green >= 0) {
                    greenArea = 0;
                }

                if (blue >= 192) {
                    blueArea = 3;
                } else if (blue >= 128) {
                    blueArea = 2;
                } else if (blue >= 64) {
                    blueArea = 1;
                } else if (blue >= 0) {
                    blueArea = 0;
                }
                int index = redArea * 16 + greenArea * 4 + blueArea;
                //??
                areaColor[index] += 1;
            }
        }
        return areaColor;
    }
}

Related

  1. countSE(Bitmap bitmap, int[][] se)
  2. getContactCount(Bitmap bitmap)
  3. getPixelsCount(Bitmap bitmap)
  4. kernelMatch(int x, int y, int w, int h, Bitmap bitmap, int[][] se)
  5. isBitmapWhiteAtTopOrBottom(Bitmap largeBitmap)