package cn.com.bmsoft.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; /** * 把文字生成图片 * @author lizhiyong * @version $Id: ChartGraphics.java, v 0.1 2015-6-3 下午3:20:59 1111 Exp $ */ public class ChartGraphics { BufferedImage image; /** * 生成图片 * @param fileLocation 图片存放路径 */ void createImage(String fileLocation) { try { FileOutputStream fos = new FileOutputStream(fileLocation); BufferedOutputStream bos = new BufferedOutputStream(fos); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos); encoder.encode(image); bos.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 把文字生成图片 * @param name 图片上的文字 * @param id * @param classname * @param imgurl */ public void graphicsGeneration(String imageFont, String imgPath) { int imageWidth = 450;// 图片的宽度 int imageHeight = 500;// 图片的高度 image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB); Graphics graphics = image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, imageWidth, imageHeight); graphics.setColor(Color.BLACK); graphics.setFont(new Font("宋体", Font.BOLD, 10)); graphics.drawString(imageFont, 50, 20); // 改成这样: BufferedImage bimg = null; try { bimg = javax.imageio.ImageIO.read(new java.io.File(imgPath)); } catch (Exception e) { } if (bimg != null) graphics.drawImage(bimg, 50, 0, null); graphics.dispose(); createImage(imgPath); } public static void main(String[] args) { ChartGraphics cg = new ChartGraphics(); try { cg.graphicsGeneration("ewew", "\\ploanshare\\2\\11.jpg"); } catch (Exception e) { e.printStackTrace(); } } }