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
| package com.devnp.qrcode;
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 com.google.zxing.datamatrix.DataMatrixReader; import com.google.zxing.qrcode.QRCodeReader; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import sun.jvm.hotspot.utilities.BitMap;
import javax.imageio.ImageIO; import javax.print.DocFlavor; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; import java.util.HashMap; import java.util.Map;
public class GenerateQRCode {
public static void generateImage(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);
}
public static void encodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
Path path = FileSystems.getDefault().getPath(filePath);
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map<EncodeHintType, Object> map = new HashMap<>();
map.put(EncodeHintType.CHARACTER_SET, "utf-8"); map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); map.put(EncodeHintType.MARGIN, 2);
BitMatrix bitMatrix = multiFormatWriter.encode(text, BarcodeFormat.QR_CODE, width, height, map) ;
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); }
public static void generateIcoImage(String icoPath, String text, int width, int height, String filePath) throws WriterException, IOException {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
Map<EncodeHintType, Object> map = new HashMap<>();
map.put(EncodeHintType.CHARACTER_SET, "utf-8"); map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); map.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, map);
BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
int qrHeight = qrImage.getHeight(); int qrWidth = qrImage.getWidth();
BufferedImage logoImage = adjustSize(qrHeight, qrWidth, icoPath);
BufferedImage combined = new BufferedImage(qrImage.getHeight(), qrImage.getWidth(), BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = (Graphics2D) combined.getGraphics();
g2d.drawImage(qrImage, 0, 0, null); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
g2d.drawImage(logoImage, (int) Math.round((qrWidth -logoImage.getWidth())/2), (int) Math.round((qrHeight-logoImage.getHeight())/2), null);
ImageIO.write(combined, "png", new File(filePath)); }
public static BufferedImage adjustSize(int qrHeight, int qrWidth, String logoPath) throws IOException { File file = new File(logoPath);
BufferedImage logoImage = ImageIO.read(file);
int icoHeight = qrHeight / 5 ; int icoWidth = qrWidth / 5 ;
BufferedImage newLogoImage = new BufferedImage(icoWidth, icoHeight,logoImage.getType()); Graphics g = newLogoImage.getGraphics(); g.drawImage(logoImage, 0,0,icoWidth, icoHeight,null);
g.dispose();
return newLogoImage ; }
}
|