NO IMAGE
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();
}
}
}