|
|
@ -2,11 +2,18 @@ package com.ccsens.util; |
|
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil; |
|
|
|
import cn.hutool.core.util.ZipUtil; |
|
|
|
import net.glxn.qrgen.core.image.ImageType; |
|
|
|
import net.glxn.qrgen.javase.QRCode; |
|
|
|
import com.google.zxing.BarcodeFormat; |
|
|
|
import com.google.zxing.EncodeHintType; |
|
|
|
import com.google.zxing.WriterException; |
|
|
|
import com.google.zxing.client.j2se.MatrixToImageWriter; |
|
|
|
import com.google.zxing.common.BitMatrix; |
|
|
|
import com.google.zxing.qrcode.QRCodeWriter; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
import java.io.*; |
|
|
|
import java.io.IOException; |
|
|
|
import java.nio.file.FileSystems; |
|
|
|
import java.nio.file.Path; |
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
public class TestQrCord { |
|
|
|
|
|
|
@ -18,23 +25,43 @@ public class TestQrCord { |
|
|
|
*/ |
|
|
|
@Test |
|
|
|
public void test01() throws Exception { |
|
|
|
String fileName = "zzz/" + DateUtil.today() + "/" + System.currentTimeMillis() + ".png"; |
|
|
|
ByteArrayOutputStream stream = QRCode.from("https://www.tall.wiki/respond?groupId=1").to(ImageType.BMP).withSize(1000, 1000).stream(); |
|
|
|
byte[] codeByte = stream.toByteArray(); |
|
|
|
File file = new File(WebConstant.UPLOAD_PATH_BASE + "/", fileName); |
|
|
|
if (!file.getParentFile().exists()) { |
|
|
|
file.getParentFile().mkdirs(); |
|
|
|
} |
|
|
|
OutputStream out = null; |
|
|
|
try { |
|
|
|
out = new FileOutputStream(file); |
|
|
|
out.write(codeByte); |
|
|
|
out.flush(); |
|
|
|
} finally { |
|
|
|
if (out != null) { |
|
|
|
out.close(); |
|
|
|
} |
|
|
|
for (int i = 1;i < 11; i++){ |
|
|
|
String fileName = "zzz/" + DateUtil.today() + "/" + i+"组" + ".png"; |
|
|
|
String text = "https://test.tall.wiki/pt-dev/respond?groupId="+i+"&groupName="+i+"组"; |
|
|
|
|
|
|
|
QRCodeWriter qrCodeWriter = new QRCodeWriter(); |
|
|
|
|
|
|
|
HashMap<EncodeHintType, Object> hints = new HashMap<>(); |
|
|
|
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); |
|
|
|
|
|
|
|
BitMatrix bitMatrix =qrCodeWriter.encode(text,BarcodeFormat.QR_CODE, 1000, 1000,hints); |
|
|
|
//BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
|
|
|
|
|
|
|
|
Path path = FileSystems.getDefault().getPath(WebConstant.UPLOAD_PATH_BASE + "/" + fileName); |
|
|
|
|
|
|
|
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); |
|
|
|
} |
|
|
|
// String text = "https://test.tall.wiki/respond?groupId=1&groupName=1组";
|
|
|
|
// String fileName = "zzz/" + DateUtil.today() + "/" + 1+"组" + ".png";
|
|
|
|
// ByteArrayOutputStream stream = QRCode.from("https://test.tall.wiki/respond?groupId=1&groupName='1组'")
|
|
|
|
// .to(ImageType.BMP).withSize(1000, 1000).stream();
|
|
|
|
// byte[] codeByte = stream.toByteArray();
|
|
|
|
// File file = new File(WebConstant.UPLOAD_PATH_BASE + "/", fileName);
|
|
|
|
// if (!file.getParentFile().exists()) {
|
|
|
|
// file.getParentFile().mkdirs();
|
|
|
|
// }
|
|
|
|
// OutputStream out = null;
|
|
|
|
// try {
|
|
|
|
// out = new FileOutputStream(file);
|
|
|
|
// out.write(codeByte);
|
|
|
|
// out.flush();
|
|
|
|
// } finally {
|
|
|
|
// if (out != null) {
|
|
|
|
// out.close();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -50,5 +77,31 @@ public class TestQrCord { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// private static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
|
|
|
|
// QRCodeWriter qrCodeWriter = new QRCodeWriter();
|
|
|
|
//
|
|
|
|
// BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
|
|
|
|
//
|
|
|
|
// Path path = FileSystems.getDefault().getPath(filePath);
|
|
|
|
//
|
|
|
|
// MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
|
|
|
private static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException { |
|
|
|
QRCodeWriter qrCodeWriter = new QRCodeWriter(); |
|
|
|
|
|
|
|
HashMap<EncodeHintType, Object> hints = new HashMap<>(); |
|
|
|
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); |
|
|
|
|
|
|
|
BitMatrix bitMatrix =qrCodeWriter.encode(text,BarcodeFormat.QR_CODE, width, height,hints); |
|
|
|
//BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
|
|
|
|
|
|
|
|
Path path = FileSystems.getDefault().getPath(filePath); |
|
|
|
|
|
|
|
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|