图形处理1:处理图片背景为斜线填充背景

需求:将图片背景处理为斜线填充背景(45度);


java源码:

package com.fivescity.seeker;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
/**
 * 图片处理背景为斜线填充背景
 * @author song
 *
 */
public class Image2ItalicBackground {

	public static void main(String[] args) {
		try {
			BufferedImage bufferedImage = ImageIO
					.read(new File("D:\\test.png"));
			BufferedImage tmpImage=rotateImage(bufferedImage,45);
			ImageFilter imgf = new ItalicBackGroundImgFilter(255);
		    FilteredImageSource fis = new FilteredImageSource(tmpImage
		      .getSource(), imgf);
		    Image im = Toolkit.getDefaultToolkit().createImage(fis);
		    
		    BufferedImage distImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
		    Graphics2D g2 = (Graphics2D) distImage.getGraphics();
		    
		    g2.drawImage(im, 0, 0, bufferedImage.getWidth(), bufferedImage.getHeight(), null);
		    g2.dispose();
		    
		    BufferedImage dist2Image=rotateImage(distImage,-45);
			ImageIO.write(dist2Image, "png", new File("D:\\t2.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	/**
     * 旋转图片为指定角度
     * 
     * @param bufferedimage
     *            目标图像
     * @param degree
     *            旋转角度
     * @return
     */
    public static BufferedImage rotateImage(final BufferedImage bufferedimage,
            final int degree) {
        int w = bufferedimage.getWidth();
        int h = bufferedimage.getHeight();
        int type = bufferedimage.getColorModel().getTransparency();
        BufferedImage img;
        Graphics2D graphics2d;
        (graphics2d = (img = new BufferedImage(w, h, type))
                .createGraphics()).setRenderingHint(
                RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
        graphics2d.drawImage(bufferedimage, 0, 0, null);
        graphics2d.dispose();
        return img;
    }
	
}
package com.fivescity.seeker;

import java.awt.image.ColorModel;
import java.awt.image.DirectColorModel;
import java.awt.image.RGBImageFilter;

public class   ItalicBackGroundImgFilter extends RGBImageFilter {// 抽象类RGBImageFilter是ImageFilter的子类,
	// 继承它实现图象ARGB的处理
	int alpha = 0;
	public ItalicBackGroundImgFilter(int alpha) {// 构造器,用来接收需要过滤图象的尺寸,以及透明度
		this.canFilterIndexColorModel = true;
		// TransparentImageFilter类继承自RGBImageFilter,它的构造函数要求传入原始图象的宽度和高度。
		// ,缺省的方式下,该函数将x,y所标识的象素的ARGB值传入,程序员按照一定的程序逻辑处理后返回该象素新的ARGB值
		this.alpha = alpha;
	}

	public int filterRGB(int x, int y, int rgb) {
		DirectColorModel dcm = (DirectColorModel) ColorModel
				.getRGBdefault();
		// DirectColorModel类用来将ARGB值独立分解出来
		int red = dcm.getRed(rgb);
		int green = dcm.getGreen(rgb);
		int blue = dcm.getBlue(rgb);
		int alp = dcm.getAlpha(rgb);

		if ((alp==0)|| (x%10!=0)) {// 填充间隔10像素
			alpha = 0;
		} else {
			alpha = 255;
		}

		return alpha << 24 | red << 16 | green << 8 | blue;// 进行标准ARGB输出以实现图象过滤
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值