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

这个Java程序展示了如何创建一个包含文字和图片的jpg文件。它首先定义图片的尺寸,然后在BufferedImage对象上绘制文字和从指定URL加载的图片。最后,程序将图像编码为JPEG并保存到指定的文件路径。
摘要由CSDN通过智能技术生成

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

}

}

}

取消

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值