| | |
| | | import com.github.xiaoymin.knife4j.core.util.StrUtil; |
| | | 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 com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; |
| | |
| | | import java.net.URL; |
| | | import java.util.HashMap; |
| | | |
| | | import static com.google.zxing.client.j2se.MatrixToImageConfig.BLACK; |
| | | import static com.google.zxing.client.j2se.MatrixToImageConfig.WHITE; |
| | | import static org.bouncycastle.asn1.x500.style.RFC4519Style.cn; |
| | | |
| | | /** |
| | |
| | | ImageIO.write(image, IMAGE_FORMAT, output); |
| | | } |
| | | |
| | | /** |
| | | *高度——height, |
| | | * 宽度——width, |
| | | * 二维码内容——text(url需要自动跳转前面加上https://) |
| | | */ |
| | | public static byte[] getQRCodeImage(String text, int width, int height) 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); |
| | | //删除白边 |
| | | // BufferedImage bufferedImage = deleteWhite(bitMatrix); |
| | | |
| | | //BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); |
| | | |
| | | ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream(); |
| | | MatrixToImageWriter.writeToStream(bitMatrix, "JPG", pngOutputStream); |
| | | //返回 |
| | | return pngOutputStream.toByteArray(); |
| | | } |
| | | |
| | | /** |
| | | * 去白边的话,调用这个方法 |
| | | * @param matrix |
| | | * @return |
| | | */ |
| | | private static BufferedImage deleteWhite(BitMatrix matrix) { |
| | | int[] rec = matrix.getEnclosingRectangle(); |
| | | int resWidth = rec[2] + 1; |
| | | int resHeight = rec[3] + 1; |
| | | |
| | | BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); |
| | | resMatrix.clear(); |
| | | for (int i = 0; i < resWidth; i++) { |
| | | for (int j = 0; j < resHeight; j++) { |
| | | if (matrix.get(i + rec[0], j + rec[1])) |
| | | resMatrix.set(i, j); |
| | | } |
| | | } |
| | | |
| | | int width = resMatrix.getWidth(); |
| | | int height = resMatrix.getHeight(); |
| | | BufferedImage image = new BufferedImage(width, height, |
| | | BufferedImage.TYPE_INT_RGB); |
| | | for (int x = 0; x < width; x++) { |
| | | for (int y = 0; y < height; y++) { |
| | | image.setRGB(x, y, resMatrix.get(x, y) ? BLACK |
| | | : WHITE); |
| | | } |
| | | } |
| | | return image; |
| | | } |
| | | |
| | | |
| | | // private static BitMatrix deleteWhite(BitMatrix matrix) { |
| | | // int[] rec = matrix.getEnclosingRectangle(); |
| | | // int resWidth = rec[2] + 1; |
| | | // int resHeight = rec[3] + 1; |
| | | // |
| | | // BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); |
| | | // resMatrix.clear(); |
| | | // for (int i = 0; i < resWidth; i++) { |
| | | // for (int j = 0; j < resHeight; j++) { |
| | | // if (matrix.get(i + rec[0], j + rec[1])) |
| | | // resMatrix.set(i, j); |
| | | // } |
| | | // } |
| | | // return resMatrix; |
| | | // } |
| | | |
| | | } |