Android Bitmap Color Change tiltShift(Bitmap sentBitmap, int radius, int x, int y)

Here you can find the source of tiltShift(Bitmap sentBitmap, int radius, int x, int y)

Description

Tilt shift effect on bitmap.

Parameter

Parameter Description
sentBitmap the sent bitmap
radius the radius
x the x
y the y

Return

the bitmap

Declaration

public static Bitmap tiltShift(Bitmap sentBitmap, int radius, int x,
        int y) 

Method Source Code

//package com.java2s;

import java.io.BufferedOutputStream;

import java.io.IOException;
import java.io.InputStream;

import android.graphics.Bitmap;

public class Main {
    /**/*from w w w.ja v a2s . c o  m*/
     * Tilt shift effect on bitmap.
     * 
     * @param sentBitmap
     *            the sent bitmap
     * @param radius
     *            the radius
     * @param x
     *            the x
     * @param y
     *            the y
     * @return the bitmap
     * @author siddhesh
     */
    public static Bitmap tiltShift(Bitmap sentBitmap, int radius, int x,
            int y) {

        Bitmap bmp = sentBitmap.copy(sentBitmap.getConfig(), true);

        int w = sentBitmap.getWidth();
        int h = sentBitmap.getHeight();
        int[] pix = new int[w * h];

        bmp.getPixels(pix, 0, w, 0, 0, w, h);
        int radiusSquared = 110 * 110;

        for (int r = radius; r > 1; r /= 2) {
            for (int i = r; i < h - r; i++) {
                for (int j = r; j < w - r; j++) {
                    double distanceSquared = Math.pow(i / 2 - x, 2)
                            + Math.pow(j - y, 2);

                    if (distanceSquared >= radiusSquared) {
                        int tl = pix[(i - r) * w + j - r];
                        int tr = pix[(i - r) * w + j + r];
                        int tc = pix[(i - r) * w + j];
                        int bl = pix[(i + r) * w + j - r];
                        int br = pix[(i + r) * w + j + r];
                        int bc = pix[(i + r) * w + j];
                        int cl = pix[i * w + j - r];
                        int cr = pix[i * w + j + r];

                        pix[(i * w) + j] = 0xFF000000
                                | (((tl & 0xFF) + (tr & 0xFF) + (tc & 0xFF)
                                        + (bl & 0xFF) + (br & 0xFF)
                                        + (bc & 0xFF) + (cl & 0xFF) + (cr & 0xFF)) >> 3)
                                & 0xFF
                                | (((tl & 0xFF00) + (tr & 0xFF00)
                                        + (tc & 0xFF00) + (bl & 0xFF00)
                                        + (br & 0xFF00) + (bc & 0xFF00)
                                        + (cl & 0xFF00) + (cr & 0xFF00)) >> 3)
                                & 0xFF00
                                | (((tl & 0xFF0000) + (tr & 0xFF0000)
                                        + (tc & 0xFF0000) + (bl & 0xFF0000)
                                        + (br & 0xFF0000) + (bc & 0xFF0000)
                                        + (cl & 0xFF0000) + (cr & 0xFF0000)) >> 3)
                                & 0xFF0000;
                    }

                }

            }
        }
        bmp.setPixels(pix, 0, w, 0, 0, w, h);
        return bmp;
    }

    /**
     * Copy.
     *
     * @param in the in
     * @param out the out
     * @throws IOException Signals that an I/O exception has occurred.
     */
    private static void copy(InputStream in, BufferedOutputStream out)
            throws IOException {
        int byte_;
        while ((byte_ = in.read()) != -1)
            out.write(byte_);
    }
}

Related

  1. initBitmapFactoryOptions( BitmapFactory.Options bitmapOptions, int maxWidth, int maxHeight)
  2. makeSaturated(Bitmap source, int level)
  3. makeVivid(Bitmap src, int type, float percent)
  4. setAlpha(Bitmap sourceImg, int number)
  5. setAlpha(Bitmap sourceImg, int number)
  6. changeBitmapColor(Bitmap bmp, int color)
  7. toGrayscale(Bitmap bmpOriginal)
  8. toGrayscale(Bitmap bmpOriginal, int pixels)
  9. createMaskBitmap(Bitmap src, Resources resource, int mask)