package org.jeecg.modules.tms.utils;
|
import com.google.zxing.BarcodeFormat;
|
import com.google.zxing.EncodeHintType;
|
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
import com.google.zxing.common.BitMatrix;
|
import com.google.zxing.qrcode.QRCodeWriter;
|
|
import javax.print.*;
|
import javax.print.attribute.HashPrintRequestAttributeSet;
|
import javax.print.attribute.PrintRequestAttributeSet;
|
import javax.print.attribute.standard.Copies;
|
import javax.swing.*;
|
import java.awt.*;
|
import java.awt.image.BufferedImage;
|
import java.awt.print.*;
|
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayOutputStream;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
|
public class QrCodeUtils {
|
/**
|
* 生成二维码图片
|
* @param content 二维码内容
|
* @param width 图片宽度
|
* @param height 图片高度
|
* @return BufferedImage 对象
|
*/
|
public static BufferedImage generateQrCode(String content, int width, int height) {
|
try {
|
Map<EncodeHintType, Object> hints = new HashMap<>();
|
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
|
QRCodeWriter writer = new QRCodeWriter();
|
BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
|
return MatrixToImageWriter.toBufferedImage(bitMatrix);
|
} catch (Exception e) {
|
throw new RuntimeException("生成二维码失败", e);
|
}
|
}
|
|
/**
|
* 打印二维码
|
*/
|
public static void printBarcode(final BufferedImage image) {
|
PrinterJob job = PrinterJob.getPrinterJob();
|
|
// 设置打印页面
|
PageFormat pf = job.defaultPage();
|
Paper paper = pf.getPaper();
|
|
// 设置纸张大小匹配条形码图像
|
paper.setSize(image.getWidth(), image.getHeight() + 50);
|
paper.setImageableArea(0, 0, image.getWidth(), image.getHeight() + 50);
|
pf.setPaper(paper);
|
|
// 设置打印内容
|
job.setPrintable(new Printable() {
|
@Override
|
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
|
if (pageIndex > 0) {
|
return NO_SUCH_PAGE;
|
}
|
|
Graphics2D g2d = (Graphics2D) graphics;
|
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
|
g2d.drawImage(image, 0, 0, null);
|
|
return PAGE_EXISTS;
|
}
|
}, pf);
|
|
// 显示打印对话框
|
if (job.printDialog()) {
|
try {
|
job.print();
|
JOptionPane.showMessageDialog(null, "打印任务已发送到打印机");
|
} catch (PrinterException e) {
|
JOptionPane.showMessageDialog(null, "打印失败: " + e.getMessage());
|
e.printStackTrace();
|
}
|
}
|
}
|
|
|
/**
|
* 默认打印机打印二维码
|
*/
|
private static void printBarcode2(BufferedImage image) throws Exception {
|
// 1. 将BufferedImage转换为字节数组
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
javax.imageio.ImageIO.write(image, "png", baos);
|
byte[] imageData = baos.toByteArray();
|
|
// 2. 查找打印服务
|
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
|
if (services.length == 0) {
|
throw new Exception("没有找到可用的打印机");
|
}
|
|
// 3. 选择默认打印机(也可以让用户选择)
|
PrintService defaultPrinter = PrintServiceLookup.lookupDefaultPrintService();
|
if (defaultPrinter == null) {
|
defaultPrinter = services[0]; // 使用第一个找到的打印机
|
}
|
|
// 4. 创建打印作业
|
DocPrintJob job = defaultPrinter.createPrintJob();
|
|
// 5. 设置打印属性
|
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
|
attributes.add(new Copies(1)); // 打印份数
|
|
// 6. 创建打印文档
|
DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
|
Doc doc = new SimpleDoc(new ByteArrayInputStream(imageData), flavor, null);
|
|
// 7. 执行打印
|
job.print(doc, attributes);
|
}
|
}
|