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
| import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.client.j2se.MatrixToImageConfig; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.util.Base64Utils;
import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.util.HashMap;
public class QrCodeUtils {
private static int weight = 400;
private static int height = 400;
private static String format = "png";
public static String encodeWithBase64(String content) { return "data:image/" + format + ";base64," + encodeWithBase64(content, weight, height, format); }
public static String encodeWithBase64(String content, int width, int height, String format) { BufferedImage bufferedImage = encodeOfImg(content, width, height, format);
try { byte[] imageByteArray = IOUtils.toByteArray(bufferedImageToInputStream(bufferedImage, format));
return Base64Utils.encodeToString(imageByteArray); } catch (Exception e) { e.printStackTrace(); } return null; }
public static void encodeAndSave(String content, int width, int height, String format, String path) { BitMatrix bitMatrix = encode(content, width, height, format);
try { MatrixToImageWriter.writeToPath(bitMatrix, format, new File(path).toPath()); } catch (IOException e) { e.printStackTrace(); } }
private static BitMatrix encode(String content, int width, int height, String format) { HashMap hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); hints.put(EncodeHintType.MARGIN, 2); try {
BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
return bitMatrix; } catch (Exception e) { e.printStackTrace(); } return null; }
private static BufferedImage encodeOfImg(String content, int width, int height, String format) { try { BitMatrix bitMatrix = encode(content, width, height, format);
BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix, new MatrixToImageConfig(Color.BLACK.getRGB(), Color.WHITE.getRGB()));
return bufferedImage; } catch (Exception e) { e.printStackTrace(); }
return null; }
private static InputStream bufferedImageToInputStream(BufferedImage image, String format) { ByteArrayOutputStream os = new ByteArrayOutputStream(); try { ImageIO.write(image, format, os); InputStream input = new ByteArrayInputStream(os.toByteArray()); return input; } catch (IOException e) { e.printStackTrace(); } return null; } }
|