gaussian Blur Filter - Android Graphics

Android examples for Graphics:Bitmap Blur

Description

gaussian Blur Filter

Demo Code

/*/*from   ww w  .  j ava2  s .c om*/
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

public class Main {
    private static final int RED_MASK = 0xff0000;
    private static final int RED_MASK_SHIFT = 16;
    private static final int GREEN_MASK = 0x00ff00;
    private static final int GREEN_MASK_SHIFT = 8;
    private static final int BLUE_MASK = 0x0000ff;

    private static void gaussianBlurFilter(int[] in, int[] out, int width,
            int height) {
        // This function is currently hardcoded to blur with RADIUS = 4.
        // (If you change RADIUS, you'll have to change the weights[] too.)
        final int RADIUS = 4;
        final int[] weights = { 13, 23, 32, 39, 42, 39, 32, 23, 13 }; // Adds up to 256
        int inPos = 0;
        int widthMask = width - 1; // width must be a power of two.
        for (int y = 0; y < height; ++y) {
            // Compute the alpha value.
            int alpha = 0xff;
            // Compute output values for the row.
            int outPos = y;
            for (int x = 0; x < width; ++x) {
                int red = 0;
                int green = 0;
                int blue = 0;
                for (int i = -RADIUS; i <= RADIUS; ++i) {
                    int argb = in[inPos + (widthMask & (x + i))];
                    int weight = weights[i + RADIUS];
                    red += weight * ((argb & RED_MASK) >> RED_MASK_SHIFT);
                    green += weight
                            * ((argb & GREEN_MASK) >> GREEN_MASK_SHIFT);
                    blue += weight * (argb & BLUE_MASK);
                }
                // Output the current pixel.
                out[outPos] = (alpha << 24)
                        | ((red >> 8) << RED_MASK_SHIFT)
                        | ((green >> 8) << GREEN_MASK_SHIFT) | (blue >> 8);
                outPos += height;
            }
            inPos += width;
        }
    }
}

Related Tutorials