版权声明:随便转,随便看 https://blog.csdn.net/qq_21780681/article/details/84825274
java批量生成下方带文字二维码,并输出到word文件中
1、pom文件,引入jar
<!--二维码生成-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.3.0</version>
</dependency>
<!--word相关jar-->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext-rtf</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
2、生成带下方带文字二维码
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
/**
* @author wilson
* @date 2018-12-3 09:34:21
* 二维码生成
*/
public class ZXingCode {
/**
* 颜色
*/
private static final int QRCOLOR = 0xFF000000;
/**
* 背景颜色
*/
private static final int BGWHITE = 0xFFFFFFFF;
/**
* 存放路径
*/
// private static final String CODEPATH = "..\\..\\codeImage\\";
public static void main(String[] args) {
try {
System.out.println(System.currentTimeMillis());
getQRCode("fe212ac018f4711811", "300-欧阳峰子");
System.out.println(System.currentTimeMillis());
System.out.println("生成ok");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成二维码方法
*
* @param data
* @param belowText
* @return
*/
public static com.lowagie.text.Image getQRCode(String data, String belowText) {
try {
ZXingCode zp = new ZXingCode();
BufferedImage bim = zp.generateQRCodeBufferedImage(data, BarcodeFormat.QR_CODE, 230, 230, zp.getDecodeHintType());
//字节数组
com.lowagie.text.Image imageByte = zp.addTextForQRCode(bim, belowText);
return imageByte;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @param bim 放在内存中图片
* @param belowText 二维码下方显示文字
* @return
*/
public com.lowagie.text.Image addTextForQRCode(BufferedImage bim, String belowText) {
try {
BufferedImage image = bim;
if (belowText != null && !belowText.equals("")) {
BufferedImage outImage = new BufferedImage(230, 245, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D outg = outImage.createGraphics();
outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
outg.setColor(Color.BLACK);
outg.setFont(new Font("宋体", Font.PLAIN, 18));
int strWidth = outg.getFontMetrics().stringWidth(belowText);
outg.drawString(belowText, 100 - strWidth / 2 + 8, image.getHeight() + (outImage.getHeight() - image.getHeight()) / 2 + 5);
outg.dispose();
outImage.flush();
image = outImage;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.flush();
ImageIO.write(image, "png", baos);
BufferedImage newBufferedImage = new BufferedImage(
image.getWidth(), image.getHeight(),
BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(image, 0, 0,
Color.WHITE, null);
// 存放本地
/*ImageIO.write(newBufferedImage, "png", new File(CODEPATH + "SZ-" + System.currentTimeMillis() + "code.png"));*/
com.lowagie.text.Image imageByte = com.lowagie.text.Image.getInstance(baos.toByteArray());
baos.close();
image.flush();
return imageByte;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 绘制二维码,不带文字
*
* @param content 扫描内容
* @param barcodeFormat 格式
* @param width
* @param height
* @param hints 二维码属性设置
* @return 放到内存中,后续再二维码下方添加文字
*/
public BufferedImage generateQRCodeBufferedImage(String content, BarcodeFormat barcodeFormat, int width, int height, Map<EncodeHintType, ?> hints) {
MultiFormatWriter multiFormatWriter = null;
BitMatrix bm = null;
BufferedImage image = null;
try {
multiFormatWriter = new MultiFormatWriter();
bm = multiFormatWriter.encode(content, barcodeFormat, width, height, hints);
int w = bm.getWidth();
int h = bm.getHeight();
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
}
}
} catch (WriterException e) {
e.printStackTrace();
}
return image;
}
/**
* 设置二维码属性
*
* @return
*/
public Map<EncodeHintType, Object> getDecodeHintType() {
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 0);
hints.put(EncodeHintType.MAX_SIZE, 350);
hints.put(EncodeHintType.MIN_SIZE, 100);
return hints;
}
}
3、将二维码输出到word中
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.*;
import com.lowagie.text.Image;
import com.lowagie.text.rtf.RtfWriter2;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
/**
* @author wilson
* @date 2018-12-5 09:32:52
*/
public class CreateWord {
//HttpServletRequest request, HttpServletResponse response可以不加这两个参数
public static void createDocContext(List<Image> imageList,HttpServletRequest request, HttpServletResponse response) throws DocumentException, IOException {
File file1 = new File("TwoCodeImage.doc");
judeFileExists(file1);
Document document = new Document();
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
RtfWriter2.getInstance(document, new FileOutputStream(file1));
document.open();
Paragraph title = new Paragraph("登录二维码信息");
//设置标题格式对齐方式
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
// 设置 Table 表格
//设置表格,将图片加到表格中进行方便定位
Table aTable = new Table(4);
// 设置每列所占比例
// 占页面宽度 90%
aTable.setWidth(100);
// 自动填满
/*aTable.setAutoFillEmptyCells(true);*/
//这里是imagelist集合,就是图片字节流的集合,图片从流中去获取放到word中
for (int i = 0; i < imageList.size(); i++) {
//设置图片等比例缩小
imageList.get(i).scalePercent(55);
aTable.addCell(new Cell(imageList.get(i)));
}
document.add(aTable);
document.add(new Paragraph("\n"));
System.out.println("word----success");
document.close();
//响应浏览器 返回下载
response.setContentType("applicaiton/x-download");
response.addHeader("Content-Disposition", "attachment;filename=" + "TwoCodeImage.doc");
InputStream is = null;
OutputStream os = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
is = new FileInputStream(new File("TwoCodeImage.doc"));
bis = new BufferedInputStream(is);
os = response.getOutputStream();
bos = new BufferedOutputStream(os);
byte[] b = new byte[1024];
int len = 0;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
bis.close();
is.close();
bos.close();
os.close();
//删除本地临时文件
file1.delete();
}
}