Get Bitmap size Trimmable Left by color - Android Graphics

Android examples for Graphics:Bitmap Color

Description

Get Bitmap size Trimmable Left by color

Demo Code


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

public class Main {
    private static final double FUZZ = 0.1;

    public static final float sizeTrimmableLeft(Bitmap b) {
        for (int x = 0; x < b.getWidth() - 1; x++) {
            for (int y = 0; y < b.getHeight() - 1; y++)
                if (colorDistance(b.getPixel(x, y), b.getPixel(x, y + 1)) > FUZZ)
                    return (float) x / b.getWidth();
            if (colorDistance(b.getPixel(x, 0), b.getPixel(x + 1, 0)) > FUZZ)
                return (float) x / b.getWidth();
        }//from w  w w .j  a v  a  2 s. c  o  m
        return 0;
    }

    private static final double colorDistance(int color1, int color2) {
        int redDistance = Color.red(color1) - Color.red(color2);
        int greenDistance = Color.green(color1) - Color.green(color2);
        int blueDistance = Color.blue(color1) - Color.blue(color2);
        double distance = Math.sqrt(redDistance * redDistance
                + greenDistance * greenDistance + blueDistance
                * blueDistance);
        return distance / 256.;
    }
}

Related Tutorials