代码很简单,没有什么新意!就是利用JDK做的简单绘图和变换!
package com.oracle.day16;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.RenderingHints;import java.awt.font.FontRenderContext;import java.awt.geom.Rectangle2D;import java.awt.p_w_picpath.BufferedImage;import java.io.File;import java.io.IOException;import javax.p_w_picpathio.ImageIO;/** * *@author huyongjian Oracle(Compus Solution Group) * @Date 2013-8-16 * @version 2.0 * @since JDK1.6(建议) Copy Right Information COMPUS SOLUTION GROUP IDE:Eclipse class: 打印生成jpg图片生成文字水印 JDK2D02 */public class TestJdk2D02 { public String create(String str,String filePath, int width,int height){ String fileName=System.currentTimeMillis()+".jpg"; String path=filePath+"/"+fileName; File file=new File(path); BufferedImage bi=new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB); Graphics2D g2=(Graphics2D) bi.getGraphics(); g2.setColor.WHITE); g2.clearRect(0, 0, width, height); Font font=new Font("黑体",Font.BOLD,25); g2.setFont(font); g2.setPaint(Color.RED); FontRenderContext context=g2.getFontRenderContext(); Rectangle2D bounds=font.getStringBounds(str, context); double x=(width-bounds.getWidth())/2; double y=(height-bounds.getHeight())/2; double ascent=-bounds.getY(); double baseY=y+ascent; g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2.drawString(str, (int)x, (int)baseY); try { ImageIO.write(bi, "jpg", file); } catch (IOException e) { e.printStackTrace(); } return file.getPath(); } public static void main(String[] args) { TestJdk2D02 t= new TestJdk2D02(); System.out.println(t.create("小夜的传说","c:/",500,38)); }}
运行之后在您的C盘中可以查看生成图片的水印!
群号:1936625305 Java程序员联盟 欢迎大家的加入!