Two-dimensional code generation tool - Google zxing

1. Import dependency in the pom

<dependencies>

    <dependency>
      <groupId>com.google.zxing</groupId>
      <artifactId>core</artifactId>
      <version>3.4.0</version>
    </dependency>
    <dependency>
      <groupId>com.google.zxing</groupId>
      <artifactId>javase</artifactId>
      <version>3.4.0 </version>
    </dependency>

    ......

</dependencies>

 

 

2. generating / parsing two-dimensional code

 

public class QRCodeUtil {

    // two-dimensional code width in pixels 
    Private  static  Final  int CODE_WIDTH = 400 ;
     // two-dimensional code height in pixels 
    Private  static  Final  int CODE_HEIGHT = 400 ;
     // two-dimensional code image format 
    Private  static  Final String the FORMAT = "JPG" ;
     // encoding format 
    Private  static  Final String the CHARSET = "UTF-. 8" ;
     // default folder path dimensional code 
    Private  static  Final String DEFAULT_FILE_DIR = "E: / tmp / qrcode /" ;

    // two-dimensional code parameter 
    Private  static the Map <EncodeHintType, Object> = hints new new the HashMap ();

    static {
         // set the character encoding type 
        hints.put (EncodeHintType.CHARACTER_SET, the CHARSET);
         // Set the error correction level L / M / Q / H, the more difficult the higher the error correction level identification, the current level is the highest level set H 
        hints .put (EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
         // set two-dimensional code margin, the unit pixel, the smaller the value the closer the two-dimensional code from four weeks 
        hints.put (EncodeHintType.MARGIN,. 1 );
    }

    // ------------------------------ -------------- generate two-dimensional code ----------------

    /**
     * Generate two-dimensional code, write to the file
     * @Param codeContent two-dimensional code content
     * @Param fileName file name
     * @return
     */
    public static String createQRFile(String codeContent, String fileName) throws Exception {
        File File = new new File (DEFAULT_FILE_DIR, + fileName + "." The FORMAT);
         // generate two-dimensional code and save the image file 
        BitMatrix bitMatrix = new new MultiFormatWriter () encode (codeContent, BarcodeFormat.QR_CODE, CODE_WIDTH, CODE_HEIGHT, hints).;
        MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, file.toPath());
        return file.getPath();
    }

    /**
     * Generating a two-dimensional code, the output stream (e.g.: response.getOutputStream ())
     * @Param codeContent two-dimensional code content
     * @param out 输出流
     */
    public static void createQROutput(String codeContent, OutputStream out) throws Exception {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(codeContent, BarcodeFormat.QR_CODE, CODE_WIDTH, CODE_HEIGHT, hints);
        MatrixToImageWriter.writeToStream(bitMatrix, FORMAT, out);
    }

    // ------------------------------ -------------- resolve the two-dimensional code ----------------

    /**
     * Two-dimensional code parsing file
     * @Param filePath file path
     * @return
     */
    public static String parseQRFile(String filePath) throws Exception {
        File file = new File(filePath);
        if (file == null || !file.exists() || file.isDirectory()) {
            return null;
        }
        BufferedImage bufferedImage = ImageIO.read(file);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
        Hashtable hints = new Hashtable();
        hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
        Result result = new MultiFormatReader().decode(bitmap, hints);
        return result.getText();
    }

    /**
     * Network URL parsing two-dimensional code
     * @Param urlPath two-dimensional code images Network Address
     * @return
     */
    public static String parseQRUrl(String urlPath) throws Exception {
        URL url = new URL(urlPath);
        BufferedImage bufferedImage = ImageIO.read(url);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
        Hashtable hints = new Hashtable();
        hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
        Result result = new MultiFormatReader().decode(bitmap, hints);
        return result.getText();
    }

}

 

Guess you like

Origin www.cnblogs.com/vettel0329/p/11090666.html