[轉] 給網站上傳的圖片蓋章
?/************************************************
?* <p>java對圖片的操作(只能使用jpg)</p>
?* 對圖片的簽章<br>
?* 對圖片的縮圖<br>
?* <p>Title:java對圖片的操作(只能使用jpg)</p>
?***********************************************/
package com.cn.wangk.test;
import java.io.*;
import com.sun.image.codec.jpeg.*;//sun公司僅提供了jpg圖片文件的編碼api
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import javax.imageio.ImageIO;
/**
?* @author wangkai
?*/
public class Test {
??? /**
???? *?
???? */
??? public Test() {
??????? try {
??????????? //生成以后新的圖片地址
??????????? File fo = new File("c:\\4.jpg");
??????????? //讀取的圖片文件
??????????? String imagePath = "C:\\Documents and Settings\\Administrator"
??????????????????? + "\\My Documents\\My Pictures\\1.jpg";
??????????? //蓋章的圖片文件
??????????? String toimagepth = "C:\\1.jpg";
??????????? //得到圖片的文件流
??????????? InputStream imageIn;
??????????? imageIn = new FileInputStream(new File(imagePath));
??????????? //得到輸入的編碼器,將文件流進行jpg格式編碼
??????????? JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
??????????? //得到編碼后的圖片對象
??????????? BufferedImage image = decoder.decodeAsBufferedImage();
??????????? Graphics g = image.getGraphics();
??????????? try {
??????????????? InputStream imageIn2 = null;
??????????????? imageIn2 = new FileInputStream(new File(toimagepth));
??????????????? //得到輸入的編碼器,將文件流進行jpg格式編碼
??????????????? JPEGImageDecoder decoder2 = JPEGCodec
??????????????????????? .createJPEGDecoder(imageIn2);
??????????????? //得到編碼后的圖片對象
??????????????? BufferedImage image2 = decoder2.decodeAsBufferedImage();
??????????????? //加蓋圖片章
??????????????? ImageObserver obser = null;
??????????????? int x = image.getWidth() - image2.getWidth();
??????????????? int y = image.getHeight() - image2.getHeight();
??????????????? g.drawImage(image2, x, y, obser);
??????????? } catch (FileNotFoundException e) {
??????????????? //打開文件失敗,表示章圖片不存在,這時候直接加蓋文件章(簽名)
??????????????? g.setFont(new Font("宋體", Font.PLAIN, 18));
??????????????? g.drawString("秋水工作室", image.getWidth() - 100,
??????????????????????? image.getHeight() - 20);
??????????????? g.drawString("water_wang@xs.zj.cn", image.getWidth() - 180,
??????????????????????? image.getHeight() - 10);
??????????? }
??????????? g.dispose();
??????????? ImageIO.write(image, "jpeg", fo);
??????????? System.out.println("ok");
??????? } catch (FileNotFoundException e) {
??????????? // 自動生成 catch 塊
??????????? e.printStackTrace();
??????? } catch (ImageFormatException e) {
??????????? // 自動生成 catch 塊
??????????? e.printStackTrace();
??????? } catch (IOException e) {
??????????? // 自動生成 catch 塊
??????????? e.printStackTrace();
??????? }
??? }
??? public static void saveFixedBoundIcon(File imageFile, int height, int width)
??????????? throws Exception {
??????? double Ratio = 0.0;
??????? if (imageFile == null || !imageFile.isFile())
??????????? throw new Exception(imageFile + "找不到指定的文件!");
??????? String filePath = imageFile.getPath();
??????? BufferedImage Bi = ImageIO.read(imageFile);
??????? if ((Bi.getHeight() > height) || (Bi.getWidth() > width)) {
??????????? if (Bi.getHeight() > Bi.getWidth()) {
??????????????? Ratio = (new Integer(height)).doubleValue() / Bi.getHeight();
??????????? } else {
??????????????? Ratio = (new Integer(width)).doubleValue() / Bi.getWidth();
??????????? }
??????????? File savefile = new File(filePath + "_" + height + "_" + width
??????????????????? + ".jpg");
??????????? Image Itemp = Bi.getScaledInstance(width, height,
??????????????????? Image.SCALE_SMOOTH);
??????????? AffineTransformOp op = new AffineTransformOp(AffineTransform
??????????????????? .getScaleInstance(Ratio, Ratio), null);
??????????? Itemp = op.filter(Bi, null);
??????????? try {
??????????????? ImageIO.write((BufferedImage) Itemp, "jpeg", savefile);
??????????? } catch (Exception ex) {
??????????? }
??????? }
??? }
??? public static void main(String[] args) {
??????? //?????? Test ts = new Test();
??????? try {
??????????? Test.saveFixedBoundIcon(new File(
??????????????????? "C:\\test.jpg"), 200, 200);
??????? } catch (Exception e) {
??????????? // 自動生成 catch 塊
??????????? e.printStackTrace();
??????? }
??? }
}
出自:Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=219915
posted on 2007-04-01 17:52 javaboys 閱讀(460) 評論(0) 編輯 收藏 所屬分類: java web