Get Bitmap size Trimmable Right by color - Android Graphics

Android examples for Graphics:Bitmap Color

Description

Get Bitmap size Trimmable Right 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 sizeTrimmableRight(Bitmap b) {
        for (int x = 1; x < b.getWidth(); x++) {
            for (int y = 0; y < b.getHeight() - 1; y++)
                if (colorDistance(b.getPixel(b.getWidth() - x, y),
                        b.getPixel(b.getWidth() - x, y + 1)) > FUZZ)
                    return (float) (x - 1) / b.getWidth();
            if (colorDistance(b.getPixel(b.getWidth() - x, 0),
                    b.getPixel(b.getWidth() - x - 1, 0)) > FUZZ)
                return (float) (x - 1) / b.getWidth();
        }/*  w  w w  .  j av  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