java生成jpg图片 并且实现文字和图片混排

importcom.sun.image.codec.jpeg.JPEGCodec;importcom.sun.image.codec.jpeg.JPEGImageEnco... import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.awt.image.BufferedImage;
import java.awt.*;
import javax.swing.ImageIcon;

public class ChartGraphics {

BufferedImage image;

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

public void graphicsGeneration(String name, String id, String classname, String imgurl) {

int imageWidth = 500;//图片的宽度

int imageHeight = 400;//图片的高度

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.drawString("姓名 : " + name, 50, 75);
graphics.drawString("学号 : " + id, 50, 150);
graphics.drawString("班级 : " + classname, 50, 225);
ImageIcon imageIcon = new ImageIcon(imgurl);
graphics.drawImage(imageIcon.getImage(), 230, 0, null);
createImage("G:\\TDDOWNLOAD\\hehe.jpg");

}

public static void main(String[] args) {
ChartGraphics cg = new ChartGraphics();
try {
cg.graphicsGeneration("ewew", "1", "12", "C:/Documents and Settings/Administrator/My Documents/My Pictures/7.jpg");
} catch (Exception e) {
e.printStackTrace();
}
}
}
其中 图象路径都是正确的 文字能显示 图片却不能 很急
楼下的帮看下啊 是SE 不是EE啊 谢谢
展开
 我来答
匿名用户
推荐于2018-03-21
展开全部
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.awt.image.BufferedImage;
import java.awt.*;
import javax.swing.ImageIcon;

public class ChartGraphics {

BufferedImage image;

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

public void graphicsGeneration(String name, String id, String classname, String imgurl) {

int imageWidth = 500;//图片的宽度

int imageHeight = 400;//图片的高度

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.drawString("姓名 : " + name, 50, 75);
graphics.drawString("学号 : " + id, 50, 150);
graphics.drawString("班级 : " + classname, 50, 225);
//ImageIcon imageIcon = new ImageIcon(imgurl);
//graphics.drawImage(imageIcon.getImage(), 230, 0, null);

//改成这样:
BufferedImage bimg = null;
try {
bimg = javax.imageio.ImageIO.read(new java.io.File(imgurl));
} catch (Exception e) {}

if(bimg!=null)
graphics.drawImage(bimg, 230, 0, null);
graphics.dispose();

createImage("G:\\TDDOWNLOAD\\hehe.jpg");

}

public static void main(String[] args) {
ChartGraphics cg = new ChartGraphics();
try {
cg.graphicsGeneration("ewew", "1", "12", "C:/Documents and Settings/Administrator/My Documents/My Pictures/7.jpg");
} catch (Exception e) {
e.printStackTrace();
}
}
}
lzj203355
2008-11-23 · TA获得超过2022个赞
知道小有建树答主
回答量:3147
采纳率:100%
帮助的人:1012万
展开全部
response.setHeader("Cache-Control","no-cache");
String str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String sum="";
for(int i=0;i<4;i++){
Random random=new Random();
int j=Math.round(random.nextFloat()*35);
char x=str.charAt(j);
sum+=x+"";
}
request.getSession().setAttribute("Code",sum);
BufferedImage bufferedImage=new BufferedImage(50,20,BufferedImage.TYPE_3BYTE_BGR);
Graphics2D graphics2D=(Graphics2D)bufferedImage.getGraphics();
graphics2D.setColor(Color.blue);
graphics2D.fill3DRect(0,0,50,20,false);
graphics2D.setColor(Color.YELLOW);
graphics2D.drawString(sum,10,12);
response.setContentType("image/jpeg");
ServletOutputStream output;
try {
output = response.getOutputStream();
JPEGImageEncoder encoder= JPEGCodec.createJPEGEncoder(output);
encoder.encode(bufferedImage);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式