Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the method of generating QR code by zxing

Shulou Source: shulou.com Published: 2022-06-01 23:58:03 10月19日 Update

This article mainly explains "what is the method of generating QR code by zxing". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the method of generating QR code by zxing"?

POM

Com.google.zxing core 3.1.0 com.google.zxing javase 3.1.0

Utils

Package com.egl.utils;import java.awt.BasicStroke;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.Shape;import java.awt.geom.RoundRectangle2D;import java.awt.image.BufferedImage;import java.io.File;import java.io.OutputStream;import java.util.Hashtable;import java.util.Random;import javax.imageio.ImageIO;import com.google.zxing.BarcodeFormat;import com.google.zxing.BinaryBitmap;import com.google.zxing.DecodeHintType;import com.google.zxing.EncodeHintType Import com.google.zxing.MultiFormatReader;import com.google.zxing.MultiFormatWriter;import com.google.zxing.Result;import com.google.zxing.client.j2se.BufferedImageLuminanceSource;import com.google.zxing.common.BitMatrix;import com.google.zxing.common.HybridBinarizer;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;/** * lee * / public class QRCodeUtil {private static final String CHARSET = "utf-8"; private static final String FORMAT = "JPG" / / QR code size private static final int QRCODE_SIZE = 300; / / LOGO width private static final int LOGO_WIDTH = 60; / / LOGO height private static final int LOGO_HEIGHT = 60; private static BufferedImage createImage (String content, String logoPath, boolean needCompress) throws Exception {Hashtable hints = new Hashtable (); hints.put (EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put (EncodeHintType.CHARACTER_SET, CHARSET) Hints.put (EncodeHintType.MARGIN, 1); BitMatrix bitMatrix = new MultiFormatWriter (). Encode (content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints); int width = bitMatrix.getWidth (); int height = bitMatrix.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, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } if (logoPath == null || "".equals(logoPath)) { return image; } // 插入图片 QRCodeUtil.insertImage(image, logoPath, needCompress); return image; } /** * 插入LOGO * * @param source * 二维码图片 * @param logoPath * LOGO图片地址 * @param needCompress * 是否压缩 * @throws Exception */ private static void insertImage(BufferedImage source, String logoPath, boolean needCompress) throws Exception { File file = new File(logoPath); if (!file.exists()) { throw new Exception("logo file not found."); } Image src = ImageIO.read(new File(logoPath)); int width = src.getWidth(null); int height = src.getHeight(null); if (needCompress) { // 压缩LOGO if (width >

Tags: 2D QR code generation content address file directory method picture file name storage learning output practical deeper interest multi-layer practical actual width Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn Redmi Shulou Information MySQL