1024programmer Blog QR code QrCode tool class_barcodeformat.qr_code

QR code QrCode tool class_barcodeformat.qr_code

pom

 
         
             com.google.zxing
             core
             3.2.1
         
         
             com.google.zxing
             javase
             3.2.1
         
 

QrCodeUtil

import java.awt.Color;
 import java.awt.Font;
 import java.awt.Graphics2D;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Objects;

 import javax.imageio.ImageIO;

 import org.apache.commons.lang3.StringUtils;
 import com.google.zxing.BarcodeFormat;
 import com.google.zxing.EncodeHintType;
 import com.google.zxing.MultiFormatWriter;
 import com.google.zxing.common.BitMatrix;
 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

 /**
  * Draw a QR code to formulate a logo and formulate a description
  * Dependence
  * 
  * com.google.zxing
  * core
  * 3.2.1
  * 
  * 
  * com.google.zxing
  * javase
  * 3.2.1
  * 
  */
 public class QrCodeUtil {
     private static final int QRCOLOR = 0xFF000000; //Default is black
     private static final int BGWHITE = 0xFFFFFFFF; // background color

     private static final int WIDTH = 400; // QR code width
     private static final int HEIGHT = 400; // QR code height

     // Used to set QR code parameters
     private static Map hints = new HashMap() {
         private static final long serialVersionUID = 1L;

         {
             put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//Set the error correction level of the QR code (H is the highest level) specific level information
             put(EncodeHintType.CHARACTER_SET, "utf-8");//Set encoding method
             put(EncodeHintType.MARGIN, 0);
         }
     };

     // Generate QR code image with logo
     public static void drawLogoQRCode(String logoPath, String codePath, String qrUrl, String note) {
         try {
             File logoFile = new File(logoPath);
             File codeFile = new File(codePath);
             MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
             //The order of parameters is: encoding content, encoding type, generated image width, generated image height, setting parameters
             BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
             BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

             // Start using QR code data to create Bitmap images, set them to black (0xFFFFFFFF) and white (0xFF000000).
             for (int x = 0; x < WIDTH; x++) {
                 for (int y = 0; y  399) {
                     // //If the length is too long, cut off the front part
                     // If the length is too long, wrap it in a new line
                     String note1 = note.substring(0, note.length() / 2);
                     String note2 = note.substring(note.length() / 2, note.length());
                     int strWidth1 = outg.getFontMetrics().stringWidth(note1);
                     int strWidth2 = outg.getFontMetrics().stringWidth(note2);
                     outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12);
                     BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);
                     Graphics2D outg2 = outImage2.createGraphics();
                     outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
                     outg2.setColor(Color.BLACK);
                     outg2.setFont(new Font("Default", Font.BOLD, 30)); // Font, font style, font size
                     outg2.drawString(note2, 200 - strWidth2 / 2, outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
                     outg2.dispose();
                     outImage2.flush();
                     outImage = outImage2;
                 } else {
                     outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // Draw text
                 }
                 outg.dispose();
                 outImage.flush();
                 image = outImage;
             }

             image.flush();
             //Create the directory if it does not exist
             if (!codeFile.exists() && !codeFile.isDirectory()) {
                 //Create a directory
                 codeFile.mkdirs();
             }
             ImageIO.write(image, "png", codeFile); // TODO
         } catch (Exception e) {
             e.printStackTrace();
         }
     }

 }

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/qr-code-qrcode-tool-class_barcodeformat-qr_code/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索