Java处理图片背景透明度

public static void processPNGTransparency(String fromFile, int fromW, int fromH, String toFile, int toW, int toH) {   
	        try {   
	            BufferedImage to = new BufferedImage(toW, toH,   
	                    BufferedImage.TYPE_INT_RGB);   
	            Graphics2D g2d = to.createGraphics();   
	            to = g2d.getDeviceConfiguration().createCompatibleImage(toW, toH,   
	                    Transparency.TRANSLUCENT);   
	            g2d.dispose();   
	            g2d = to.createGraphics();   
	            File f2 = new File(fromFile);   
	            BufferedImage bi2 = ImageIO.read(f2);   
	            Image from = bi2.getScaledInstance(fromW, fromH, bi2.SCALE_DEFAULT);   
	            g2d.drawImage(from, 0,0,fromW, fromH,null);
	            g2d.dispose();   
	            ImageIO.write(to, "png", new File(toFile));   
	        } catch (IOException e) {   
	            e.printStackTrace();   
	        }   
	    } 

猜你喜欢

转载自aitaozhang.iteye.com/blog/2210447