使用java实现图片的合成
1、保存图片
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();
}
}
2、将两张图片生成一张图片
public void graphicsGeneration(String name, String id, String classname, String imgurl,String qrimg) {
int imageWidth = 295;// 图片的宽度
int imageHeight =333;// 图片的高度
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, 80, 220);
graphics.drawString(id, 130,260);
graphics.drawString(classname,130,290);
// 改成这样:
BufferedImage bimg = null;
try {
bimg = javax.imageio.ImageIO.read(new java.io.File(imgurl));
} catch (Exception e) {
}
if (bimg != null){
graphics.drawImage(bimg,80, 20, null);
}
BufferedImage qimg =null;
try {
qimg = javax.imageio.ImageIO.read(new java.io.File(qrimg));
} catch (Exception e) {
}
if (qimg != null){
graphics.drawImage(qimg,20, 220, null);
}
graphics.dispose();
createImage("E:\\55.jpg");
}
3、将合成的图片批量合成到一张打印图片上
public void gG(String[] imgurl) {
int imageWidth =1240;// 图片的宽度
int imageHeight =1754;// 图片的高度
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);
int i=0;
int j=0;
int p=0;
for (String img : imgurl) {
BufferedImage bimg = null;
try {
bimg = javax.imageio.ImageIO.read(new java.io.File(img));
} catch (Exception e) {
}
if (bimg != null){
graphics.drawImage(bimg,20+i, 20+p, null);
}
i=i+320;
j++;
if(j%4==0){
p=p+350;
i=0;
}
}
graphics.dispose();
createImage("E:\\100.jpg");
}
4、除了可以直接将图片合成到打印图片上,还可排版到word文档中
public static void createDoc(String[] str) throws Exception{
//创建word文档,并设置纸张的大小
String fileName="E:/照片.doc";
FileOutputStream fos = new FileOutputStream(fileName);
Document document = new Document(PageSize.A4);
RtfWriter2.getInstance(document,fos);
document.open();
Table table = new Table(4);
table.setBorderWidth(0);
for(int i=0;i<str.length;i++){
//添加图片
File file = new File(str[i]);
Image img = null;
if(file.exists()) {
img= Image.getInstance(str[i]);
img.scaleAbsolute(100, 100);
table.addCell(new Cell(img));
}
}
document.add(table);
document.close();
}
5、在main方法里执行
public static void main(String[] args) {
ChartGraphics cg = new ChartGraphics();
try {
//cg.graphicsGeneration("1478654", "沈22", "维修", "E:/3.jpg", "E:/5.gif");
String[] str = new String[40];
for (int i=0;i<str.length;i++) {
str[i]="E:\\55.jpg";
}
//cg.gG(str);
createDoc(str);
System.out.println("success");
} catch (Exception e) {
e.printStackTrace();
}
}