get Color On Bitmap by Rectangle - Android Graphics

Android examples for Graphics:Bitmap Color

Description

get Color On Bitmap by Rectangle

Demo Code


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

public class Main {
    public static int getColorOnBitmap(Bitmap src, Rect serchRect,
            int defaultColor) {
        int width = serchRect.width();
        int height = serchRect.height();
        if (width == 0 || height == 0)
            return defaultColor;

        if (width == 1 && height == 1) {
            return src.getPixel(serchRect.left, serchRect.top);
        }// w w  w  .  ja v a2 s  . co m
        if (width == 1 && height > 1) {
            int colorMerge = 0xffffffff;
            for (int start = 0; start < height; ++start) {
                colorMerge = colorMerge
                        & src.getPixel(serchRect.left, serchRect.top
                                + start);
            }
            return colorMerge;
        }
        if (width > 1 && height == 1) {
            int colorMerge = 0xffffffff;
            for (int start = 0; start < width; ++start) {
                colorMerge = colorMerge
                        & src.getPixel(serchRect.left + start,
                                serchRect.top);
            }
            return colorMerge;
        }
        if (width > 1 && height > 1) {
            int colorMerge = 0xffffffff;
            for (int x = 0; x < width; ++x) {
                for (int y = 0; y < height; ++y) {
                    colorMerge = colorMerge
                            & src.getPixel(serchRect.left + x,
                                    serchRect.top + y);
                }
            }
            return colorMerge;
        }

        return defaultColor;
    }
}

Related Tutorials