Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;

import android.os.Build;

public class Main {
    /**
     * Fills the bitmap's pixels of the specified Color to transparency.
     * 
     * @param aBitmap bitmap to process
     * @param aColor color to fill
     * @return bmp
     */
    @SuppressLint("NewApi")
    public static Bitmap eraseBG(Bitmap aBitmap, int aColor) {
        int width = aBitmap.getWidth();
        int height = aBitmap.getHeight();
        Bitmap b = aBitmap.copy(Config.ARGB_8888, true);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR1) {
            b.setHasAlpha(true);
        }

        int[] pixels = new int[width * height];
        aBitmap.getPixels(pixels, 0, width, 0, 0, width, height);

        for (int i = 0; i < width * height; i++) {
            if (pixels[i] == aColor) {
                pixels[i] = 0;
            }
        }

        b.setPixels(pixels, 0, width, 0, 0, width, height);

        return b;
    }
}