get Borderless Bitmap - Android Graphics

Android examples for Graphics:Bitmap Effect

Description

get Borderless Bitmap

Demo Code


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

import android.util.Log;

public class Main {
    private static final String TAG = "BitmapHelper";

    public static Bitmap getBorderless(Bitmap bitmap) {

        final int MAX_DIFF = 10;

        int w = bitmap.getWidth();
        int h = bitmap.getHeight();

        int colour = bitmap.getPixel(0, 0);

        boolean end = false;
        int left = -1;
        for (int x = 0; x < w / 2; x++) {
            for (int y = 0; y < h; y++) {
                int pixel = bitmap.getPixel(x, y);
                if (pixel != colour) {
                    end = true;//  ww  w  .  ja  v a 2  s  .c om
                }
            }
            if (end)
                break;
            left = x + 1;
        }

        int top = -1;

        if (left < 0) {
            end = false;
            for (int y = 0; y < h / 2; y++) {
                for (int x = 0; x < w; x++) {
                    int pixel = bitmap.getPixel(x, y);
                    if (pixel != colour) {
                        if (Math.abs(pixel - colour) > MAX_DIFF)
                            end = true;
                    }
                }
                if (end)
                    break;
                top = y + 1;
            }
        }
        if (left < 0 && top < 0)
            return bitmap;
        if (left > 0 && top > 0)
            return bitmap;

        left = left > 0 ? left : 0;
        top = top > 0 ? top + 5 : 0;
        return Bitmap.createBitmap(bitmap, left, top, w - 2 * left, h - 2
                * top);
    }
}

Related Tutorials