<%@ page autoFlush="false" contentType="text/html;charset=GBK" import="java.io.FileInputStream, java.io.FileOutputStream, java.awt.*, java.awt.image.*, com.sun.image.codec.jpeg.*, java.util.*"%> <% out.clear(); response.addHeader("pragma","NO-cache"); response.addHeader("Cache-Control","no-cache"); response.addDateHeader("Expries",0); String FileName = "E:\\2.jpg"; String OutFileName = "C:\\ww.jpg"; //創(chuàng)建一個FileInputStream對象從源圖片獲取數(shù)據(jù)流 FileInputStream sFile = new FileInputStream(FileName); //創(chuàng)建一個Image對象并以源圖片數(shù)據(jù)流填充 Image src = javax.imageio.ImageIO.read(sFile); int width = src.getWidth(null); //得到源圖寬 int height = src.getHeight(null); //得到源圖長 if (width>70 && height>30){ //創(chuàng)建一個BufferedImage來作為圖像操作容器 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //創(chuàng)建一個繪圖環(huán)境來進(jìn)行繪制圖象 Graphics g = image.getGraphics(); //將原圖像數(shù)據(jù)流載入這個BufferedImage g.drawImage(src,0,0,width,height,null); //設(shè)定文本字體 g.setFont(new Font("宋體",Font.PLAIN,48)); //設(shè)定文本 String rand = "中國"; //設(shè)定文本顏色 g.setColor(Color.black); //向BufferedImage寫入文本字符 g.drawString(rand,20,50); //使更改生效 g.dispose(); //創(chuàng)建輸出文件流 FileOutputStream outi = new FileOutputStream(OutFileName); //創(chuàng)建JPEG編碼對象 JPEGImageEncoder encodera = JPEGCodec.createJPEGEncoder(outi); //對這個BufferedImage (image)進(jìn)行JPEG編碼 encodera.encode(image); outi.close(); //關(guān)閉輸出文件流 } %> |
|