1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| package com.bjtcrj.scm.common.utils;
import com.alibaba.fastjson.JSONObject; import com.google.zxing.*; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import lombok.extern.slf4j.Slf4j;
import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map;
@Slf4j public class QRCodeUtil {
public static String encodeQRCode(String text, String path) { return encodeQRCode(text, path, null, null, null); }
public static String encodeQRCode(String text, String path, Integer width, Integer height, String format) throws RuntimeException { try { if (width == null) { width = 300; } if (height == null) { height = 300; } if (format == null) { format = "png"; }
String filepath = path + "/" + String.valueOf(UniqueIDGen.getUniqueID()) + "." + format; File file = new File(Constants.PRO_REAL_PATH + filepath); if (!file.getParentFile().exists()) { log.info("目标文件所在目录不存在,准备创建它!"); if (!file.getParentFile().mkdirs()) { log.info("创建目标文件所在目录失败!"); throw new RuntimeException(); } }
Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); Path outputPath = Paths.get(Constants.PRO_REAL_PATH + filepath); MatrixToImageWriter.writeToPath(bitMatrix, format, outputPath); return filepath; } catch (Exception e) { log.error(e.getMessage(), e); e.printStackTrace(); throw new RuntimeException(); } }
public static void updateQRCode(String text, String path) throws RuntimeException { try { File file = new File(Constants.PRO_REAL_PATH + path); if(file.exists()) { file.delete(); } if (!file.getParentFile().exists()) { log.info("目标文件所在目录不存在,准备创建它!"); if (!file.getParentFile().mkdirs()) { log.info("创建目标文件所在目录失败!"); throw new RuntimeException(); } }
Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, 300, 300, hints); Path outputPath = Paths.get(Constants.PRO_REAL_PATH + path); MatrixToImageWriter.writeToPath(bitMatrix, "png", outputPath); } catch (Exception e) { log.error(e.getMessage(), e); e.printStackTrace(); throw new RuntimeException(); } }
public static JSONObject decodeQRCode(String filePath) { try { BufferedImage image = ImageIO.read(new File(filePath));
LuminanceSource source = new BufferedImageLuminanceSource(image); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map<DecodeHintType, String> hints = new HashMap<>(); hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = new MultiFormatReader().decode(binaryBitmap, hints); JSONObject content = JSONObject.parseObject(result.getText());
System.out.println("图片内容: "); System.out.println("content: " + content.toJSONString()); System.out.println("图片中格式: "); System.out.println("encode: " + result.getBarcodeFormat());
return content; } catch (Exception e) { log.error(e.getMessage(), e); }
return null; } }
|