effectutils.java
来自「Mobile 应用程序使用 Java Micro Edition (Java M」· Java 代码 · 共 410 行 · 第 1/2 页
JAVA
410 行
/* * @(#)EffectUtils.java 1.2 07/12/12 * * Copyright 2007 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.java.swing.plaf.nimbus;import java.awt.AlphaComposite;import java.awt.Graphics2D;import java.awt.Transparency;import java.awt.GraphicsConfiguration;import java.awt.GraphicsEnvironment;import java.awt.image.BufferedImage;import java.awt.image.Raster;import java.awt.image.WritableRaster;import java.awt.image.ColorModel;/** * EffectUtils * * @author Created by Jasper Potts (Jun 18, 2007) * @version 1.0 */class EffectUtils { /** * Clear a transparent image to 100% transparent * * @param img The image to clear */ static void clearImage(BufferedImage img) { Graphics2D g2 = img.createGraphics(); g2.setComposite(AlphaComposite.Clear); g2.fillRect(0, 0, img.getWidth(), img.getHeight()); g2.dispose(); } // ================================================================================================================= // Blur /** * Apply Gaussian Blur to Image * * @param src The image tp * @param dst The destination image to draw blured src image into, null if you want a new one created * @param radius The blur kernel radius * @return The blured image */ static BufferedImage gaussianBlur(BufferedImage src, BufferedImage dst, int radius) { int width = src.getWidth(); int height = src.getHeight(); if (dst == null || dst.getWidth() != width || dst.getHeight() != height || src.getType() != dst.getType()) { dst = createColorModelCompatibleImage(src); } float[] kernel = createGaussianKernel(radius); if (src.getType() == BufferedImage.TYPE_INT_ARGB) { int[] srcPixels = new int[width * height]; int[] dstPixels = new int[width * height]; getPixels(src, 0, 0, width, height, srcPixels); // horizontal pass blur(srcPixels, dstPixels, width, height, kernel, radius); // vertical pass //noinspection SuspiciousNameCombination blur(dstPixels, srcPixels, height, width, kernel, radius); // the result is now stored in srcPixels due to the 2nd pass setPixels(dst, 0, 0, width, height, srcPixels); } else if (src.getType() == BufferedImage.TYPE_BYTE_GRAY) { byte[] srcPixels = new byte[width * height]; byte[] dstPixels = new byte[width * height]; getPixels(src, 0, 0, width, height, srcPixels); // horizontal pass blur(srcPixels, dstPixels, width, height, kernel, radius); // vertical pass //noinspection SuspiciousNameCombination blur(dstPixels, srcPixels, height, width, kernel, radius); // the result is now stored in srcPixels due to the 2nd pass setPixels(dst, 0, 0, width, height, srcPixels); } else { throw new IllegalArgumentException("EffectUtils.gaussianBlur() src image is not a supported type, type=[" + src.getType() + "]"); } return dst; } /** * <p>Blurs the source pixels into the destination pixels. The force of the blur is specified by the radius which * must be greater than 0.</p> <p>The source and destination pixels arrays are expected to be in the INT_ARGB * format.</p> <p>After this method is executed, dstPixels contains a transposed and filtered copy of * srcPixels.</p> * * @param srcPixels the source pixels * @param dstPixels the destination pixels * @param width the width of the source picture * @param height the height of the source picture * @param kernel the kernel of the blur effect * @param radius the radius of the blur effect */ private static void blur(int[] srcPixels, int[] dstPixels, int width, int height, float[] kernel, int radius) { float a; float r; float g; float b; int ca; int cr; int cg; int cb; for (int y = 0; y < height; y++) { int index = y; int offset = y * width; for (int x = 0; x < width; x++) { a = r = g = b = 0.0f; for (int i = -radius; i <= radius; i++) { int subOffset = x + i; if (subOffset < 0 || subOffset >= width) { subOffset = (x + width) % width; } int pixel = srcPixels[offset + subOffset]; float blurFactor = kernel[radius + i]; a += blurFactor * ((pixel >> 24) & 0xFF); r += blurFactor * ((pixel >> 16) & 0xFF); g += blurFactor * ((pixel >> 8) & 0xFF); b += blurFactor * ((pixel) & 0xFF); } ca = (int) (a + 0.5f); cr = (int) (r + 0.5f); cg = (int) (g + 0.5f); cb = (int) (b + 0.5f); dstPixels[index] = ((ca > 255 ? 255 : ca) << 24) | ((cr > 255 ? 255 : cr) << 16) | ((cg > 255 ? 255 : cg) << 8) | (cb > 255 ? 255 : cb); index += height; } } } /** * <p>Blurs the source pixels into the destination pixels. The force of the blur is specified by the radius which * must be greater than 0.</p> <p>The source and destination pixels arrays are expected to be in the BYTE_GREY * format.</p> <p>After this method is executed, dstPixels contains a transposed and filtered copy of * srcPixels.</p> * * @param srcPixels the source pixels * @param dstPixels the destination pixels * @param width the width of the source picture * @param height the height of the source picture * @param kernel the kernel of the blur effect * @param radius the radius of the blur effect */ static void blur(byte[] srcPixels, byte[] dstPixels, int width, int height, float[] kernel, int radius) { float p; int cp; for (int y = 0; y < height; y++) { int index = y; int offset = y * width; for (int x = 0; x < width; x++) { p = 0.0f; for (int i = -radius; i <= radius; i++) { int subOffset = x + i;// if (subOffset < 0) subOffset = 0;// if (subOffset >= width) subOffset = width-1; if (subOffset < 0 || subOffset >= width) { subOffset = (x + width) % width; } int pixel = srcPixels[offset + subOffset] & 0xFF; float blurFactor = kernel[radius + i]; p += blurFactor * pixel; } cp = (int) (p + 0.5f); dstPixels[index] = (byte) (cp > 255 ? 255 : cp); index += height; } } } static float[] createGaussianKernel(int radius) { if (radius < 1) { throw new IllegalArgumentException("Radius must be >= 1"); } float[] data = new float[radius * 2 + 1]; float sigma = radius / 3.0f; float twoSigmaSquare = 2.0f * sigma * sigma; float sigmaRoot = (float) Math.sqrt(twoSigmaSquare * Math.PI); float total = 0.0f; for (int i = -radius; i <= radius; i++) { float distance = i * i; int index = i + radius; data[index] = (float) Math.exp(-distance / twoSigmaSquare) / sigmaRoot; total += data[index];
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?