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
| public class QrCodeUtils {
public static void generateQrCode(String content, String filePath, Integer width, Integer height)throws Exception { HashMap<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.MARGIN, 1); BitMatrix encode = new MultiFormatWriter().encode( content, BarcodeFormat.QR_CODE, width, height, hints ); Path path = new File(filePath).toPath(); MatrixToImageWriter.writeToPath(encode, "PNG", path); }
public static void generateQrCodeWithLogo(String content, String logoPath, String outputPath) throws Exception { int width = 300; int height = 300;
Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.MARGIN, 1); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
BitMatrix bitMatrix = new MultiFormatWriter().encode( content, BarcodeFormat.QR_CODE, width, height, hints );
BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix, new MatrixToImageConfig());
BufferedImage logo = ImageIO.read(new File(logoPath));
int logoWidth = qrImage.getWidth() / 5; int logoHeight = qrImage.getHeight() / 5;
int x = (qrImage.getWidth() - logoWidth) / 2; int y = (qrImage.getHeight() - logoHeight) / 2;
Graphics2D g = qrImage.createGraphics(); g.drawImage(logo, x, y, logoWidth, logoHeight, null); g.dispose();
ImageIO.write(qrImage, "PNG", new File(outputPath)); } }
|