`

java image

    博客分类:
  • Java
阅读更多
package com.lindows.util;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;

public class ImageProcess {

	/**
	 * @param processImg
	 *            水印文件,最好用gif或者png可以支持透明
	 * @param oldImg
	 *            原始图片文件
	 * @param newImg
	 *            生成图片文件
	 * @param x
	 *            水印的横坐标
	 * @param y
	 *            水印的纵坐标
	 */
	public static final void processImg(String processImg, String oldImg,
			String newImg, int x, int y) {

		try {
			// 目标文件
			File file = new File(oldImg);
			Image oldImage = ImageIO.read(file);
			int width = oldImage.getWidth(null);
			int height = oldImage.getHeight(null);
			BufferedImage image = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics g = image.createGraphics();
			g.drawImage(oldImage, 0, 0, width, height, null);
			// 水印文件
			File file2 = new File(processImg);
			Image addImg = ImageIO.read(file2);
			int width1 = addImg.getWidth(null);
			int height1 = addImg.getHeight(null);
			g.drawImage(addImg, width - width1 - x, height - height1 - y,
					width1, height1, null);
			// g.drawImage(addImg, (width - width1) / 2 - x, (height - height1)/
			// 2 - y, null);
			// 水印结束
			g.dispose();
			FileOutputStream out = new FileOutputStream(newImg);
			JPEGImageEncoder encorder = JPEGCodec.createJPEGEncoder(out);
			encorder.encode(image);
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// 测试方法
		ImageProcess.processImg("G:/png.png", "G:/1.png", "G:/3.jpg", 0, 0);
	}
}
 
分享到:
评论
2 楼 siemens800 2009-12-21  
不让使用,肯定有其他替代的API之类的,你再找找看,指不定藏哪了。
1 楼 riching 2009-12-20  
JPEGImageEncoder jdk1.6已经不让使用了,有什么好办法保存一个IMAGE对象么?

相关推荐

Global site tag (gtag.js) - Google Analytics