introduce
The zxing package of goole is mainly used. The following is a sample code, which is very convenient for everyone to understand and learn. The code belongs to the preliminary framework and has functions, and it needs to be improved and optimized according to actual usage.
Step 1: Maven import zxing
<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.2.1</version></dependency>
Step 2: Start generating the QR code:
private static final int BLACK = 0xFF000000;private static final int WHITE = 0xFFFFFFFF;/** * Save the generated QR code into the picture* @param matrix QR code class under zxing package* @return */public static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE); } } return image;}/** * Generate QR code and write to file* @param content Scan the content of the QR code* @param format Image format jpg * @param file file* @throws Exception */public static void writeToFile(String content, String format, File file) throws Exception { MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); @SuppressWarnings("rawtypes") Map hints = new HashMap(); //Set UTF-8 to prevent Chinese garbled hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //Set the size of the white area around the QR code hints.put(EncodeHintType.MARGIN,1); //Set the fault tolerance of QR code hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //Draw the QR code BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints); BufferedImage image = toBufferedImage(bitMatrix); if (!ImageIO.write(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); }}Step 3: Write the text into the QR code picture:
/** * Add text to the QR code image* @param pressText text* @param qrFile QR code file* @param fontStyle * @param color * @param fontSize */public static void pressText(String pressText, File qrFile, int fontStyle, Color color, int fontSize) throws Exception { pressText = new String(pressText.getBytes(), "utf-8"); Image src = ImageIO.read(qrFile); int imageW = src.getWidth(null); int imageH = src.getHeight(null); BufferedImage image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_RGB); Graphics g = image.createGraphics(); g.drawImage(src, 0, 0, imageW, imageH, null); //Set the color of the brush g.setColor(color); //Set the font Font font = new Font("安安", fontStyle, fontSize); FontMetrics metrics = g.getFontMetrics(font); //The coordinates of the text in the picture are set in the middle here int startX = (WIDTH - metrics.stringWidth(pressText)) / 2; int startY = HEIGHT/2; g.setFont(font); g.drawString(pressText, startX, startY); g.dispose(); FileOutputStream out = new FileOutputStream(qrFile); ImageIO.write(image, "JPEG", out); out.close(); System.out.println("image press success");}Step 4: Test in the main method, a QR code with text in the middle will be generated
public static void main(String[] args) throws Exception { File qrcFile = new File("/Users/deweixu/","google.jpg"); writeToFile("www.google.com.hk", "jpg", qrcFile); pressText("Google", qrcFile, 5, Color.RED, 32);}Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.