get Lighted Bitmap - Android Graphics

Android examples for Graphics:Bitmap Effect

Description

get Lighted Bitmap

Demo Code


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.YuvImage;
import android.graphics.drawable.Drawable;

public class Main{
    //from  ww w  .  j a  va  2s.c o  m

    private static final String TAG = ImgUtil.class.getSimpleName();
    
    public static Bitmap getLightedBitmap(Bitmap src, int centerX,
            int centerY, boolean recycleSrc) {
        Bitmap lighted = lightBitmap(src.copy(Config.RGB_565, true),
                centerX, centerY);
        if (recycleSrc && lighted != src) {
            src.recycle();
        }
        return lighted;
    }
    
    public static Bitmap lightBitmap(Bitmap src, int centerX, int centerY) {
        if (!src.isMutable()) {
            LogUtil.w(TAG, "lightBitmap", "source bitmap is not mutable");
            return src;
        }

        int width = src.getWidth();
        int height = src.getHeight();
        int pixR = 0;
        int pixG = 0;
        int pixB = 0;
        int pixColor = 0;
        int newR = 0;
        int newG = 0;
        int newB = 0;
        int radius = Math.min(centerX, centerY);
        final float strength = 150F; //  100~150
        int[] pixels = new int[width * height];
        src.getPixels(pixels, 0, width, 0, 0, width, height);
        int pos = 0;
        for (int i = 1, length = height - 1; i < length; i++) {
            for (int k = 1, len = width - 1; k < len; k++) {
                pos = i * width + k;
                pixColor = pixels[pos];
                pixR = Color.red(pixColor);
                pixG = Color.green(pixColor);
                pixB = Color.blue(pixColor);
                newR = pixR;
                newG = pixG;
                newB = pixB;

                // ?
                int distance = (int) (Math.pow((centerY - i), 2) + Math
                        .pow(centerX - k, 2));
                if (distance < radius * radius) {
                    // 
                    int result = (int) (strength * (1.0 - Math
                            .sqrt(distance) / radius));
                    newR = pixR + result;
                    newG = pixG + result;
                    newB = pixB + result;
                }
                newR = Math.min(255, Math.max(0, newR));
                newG = Math.min(255, Math.max(0, newG));
                newB = Math.min(255, Math.max(0, newB));
                pixels[pos] = Color.argb(255, newR, newG, newB);
            }
        }

        src.setPixels(pixels, 0, width, 0, 0, width, height);
        return src;
    }
}

Related Tutorials