|
@@ -0,0 +1,107 @@
|
|
|
+package com.huimv.farm.damsubsidy.test;
|
|
|
+
|
|
|
+import java.awt.Color;
|
|
|
+import java.awt.Graphics2D;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+
|
|
|
+import com.google.zxing.BarcodeFormat;
|
|
|
+import com.google.zxing.common.BitMatrix;
|
|
|
+import com.google.zxing.qrcode.QRCodeWriter;
|
|
|
+
|
|
|
+
|
|
|
+public class QrCodeExcelPrinter {
|
|
|
+
|
|
|
+ private static final String QRCODE_IMAGE_PATH = "D://qrcode.png"; // 二维码图片路径
|
|
|
+// private static final String EXCEL_PATH = "qrcode_excel.xls"; // Excel文件路径
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ String content = "https://www.baidu.com"; // 二维码内容
|
|
|
+ int width = 300; // 二维码宽度
|
|
|
+
|
|
|
+ // 生成二维码图片
|
|
|
+ BufferedImage qrCodeImage = generateQrCodeImage(content, width);
|
|
|
+ ImageIO.write(qrCodeImage, "png", new File(QRCODE_IMAGE_PATH));
|
|
|
+
|
|
|
+ // 使用JXL库生成Excel
|
|
|
+ /* WritableWorkbook workbook = Workbook.createWorkbook(new File(EXCEL_PATH));
|
|
|
+ WritableSheet sheet = workbook.createSheet("Sheet1", 0);
|
|
|
+
|
|
|
+ // 设置单元格格式
|
|
|
+ Font font = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD);
|
|
|
+ CellFormat labelFormat = new WritableCellFormat(font);
|
|
|
+ labelFormat.setAlignment(jxl.format.Alignment.CENTRE);
|
|
|
+ labelFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
|
|
|
+ labelFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
|
|
|
+
|
|
|
+ // 插入二维码图片
|
|
|
+ WritableImage qrCodeWritableImage = new WritableImage(0, 0, 5, 5, new File(QRCODE_IMAGE_PATH));
|
|
|
+ sheet.addImage(qrCodeWritableImage);
|
|
|
+ sheet.setRowView(0, 800); // 设置第1行的行高
|
|
|
+
|
|
|
+ // 插入二维码内容
|
|
|
+ String[] contentArray = splitContent(content, 20); // 将二维码内容每20个字符分割为一行
|
|
|
+ for (int i = 0; i < contentArray.length; i++) {
|
|
|
+ Label contentLabel = new Label(1, i, contentArray[i], labelFormat);
|
|
|
+ sheet.addCell(contentLabel);
|
|
|
+ sheet.setRowView(i, 800); // 设置行高
|
|
|
+ }
|
|
|
+
|
|
|
+ workbook.write();
|
|
|
+ workbook.close();*/
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成二维码图片
|
|
|
+ * @param content 二维码内容
|
|
|
+ * @param width 二维码图片宽度
|
|
|
+ * @return 二维码图片
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private static BufferedImage generateQrCodeImage(String content, int width) throws Exception {
|
|
|
+ QRCodeWriter writer = new QRCodeWriter();
|
|
|
+ BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, width, width);
|
|
|
+ int w = matrix.getWidth();
|
|
|
+ int h = matrix.getHeight();
|
|
|
+ BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
|
|
|
+ Graphics2D graphics = image.createGraphics();
|
|
|
+ graphics.setColor(Color.WHITE);
|
|
|
+ graphics.fillRect(0, 0, w, h);
|
|
|
+ graphics.setColor(Color.BLACK);
|
|
|
+ for (int x = 0; x < w; x++) {
|
|
|
+ for (int y = 0; y < h; y++) {
|
|
|
+ if (matrix.get(x, y)) {
|
|
|
+ graphics.fillRect(x, y, 1, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return image;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将字符串每n个字符分割为一行
|
|
|
+ * @param str 字符串
|
|
|
+ * @param n 长度
|
|
|
+ * @return 分割后的字符串数组
|
|
|
+ */
|
|
|
+ private static String[] splitContent(String str, int n) {
|
|
|
+ int strLength = str.length();
|
|
|
+ int size = strLength / n;
|
|
|
+ if (strLength % n != 0) {
|
|
|
+ size++;
|
|
|
+ }
|
|
|
+ String[] result = new String[size];
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ if (i == size - 1) {
|
|
|
+ result[i] = str.substring(i * n);
|
|
|
+ } else {
|
|
|
+ result[i] = str.substring(i * n, (i + 1) * n);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|