功能描述
引入依赖
< dependency>
< groupId> com.google.zxing</ groupId>
< artifactId> core</ artifactId>
< version> 3.4.1</ version>
</ dependency>
< dependency>
< groupId> com.google.zxing</ groupId>
< artifactId> javase</ artifactId>
< version> 3.4.1</ version>
</ dependency>
代码
package com. qiangesoft. qrcode. utils ;
import com. google. zxing. * ;
import com. google. zxing. client. j2se. BufferedImageLuminanceSource ;
import com. google. zxing. client. j2se. MatrixToImageConfig ;
import com. google. zxing. client. j2se. MatrixToImageWriter ;
import com. google. zxing. common. BitMatrix ;
import com. google. zxing. common. GlobalHistogramBinarizer ;
import com. google. zxing. datamatrix. encoder. SymbolShapeHint ;
import com. google. zxing. qrcode. decoder. ErrorCorrectionLevel ;
import javax. imageio. ImageIO ;
import java. awt. image. BufferedImage ;
import java. io. * ;
import java. util. HashMap ;
import java. util. Map ;
public class QrcodeUtil {
private static final int WIDTH = 300 ;
private static final int HEIGHT = 300 ;
private static final String FORMAT = "png" ;
private static final int BLACK = 0xFF000000 ;
private static final int WHITE = 0xFFFFFFFF ;
private static String CHARSET = "UTF-8" ;
public static void generateQRCode ( String content, OutputStream outputStream) {
generateQRCode ( content, WIDTH , HEIGHT , BLACK , WHITE , outputStream) ;
}
public static void generateQRCode ( String content, int width, int height, OutputStream outputStream) {
generateQRCode ( content, width, height, BLACK , WHITE , outputStream) ;
}
public static void generateQRCode ( String content, int width, int height, int onColor, int offColor, OutputStream outputStream) {
Map < EncodeHintType , Object > hints = new HashMap < > ( ) ;
hints. put ( EncodeHintType . CHARACTER_SET , CHARSET ) ;
hints. put ( EncodeHintType . ERROR_CORRECTION , ErrorCorrectionLevel. H ) ;
hints. put ( EncodeHintType . MARGIN , 1 ) ;
hints. put ( EncodeHintType . DATA_MATRIX_SHAPE , SymbolShapeHint . FORCE_SQUARE ) ;
generate ( content, BarcodeFormat . QR_CODE , width, height, hints, onColor, offColor, FORMAT , outputStream) ;
}
public static void generateCode128 ( String content, OutputStream outputStream) {
generateCode128 ( content, WIDTH , HEIGHT , BLACK , WHITE , outputStream) ;
}
public static void generateCode128 ( String content, int width, int height, OutputStream outputStream) {
generateCode128 ( content, width, height, BLACK , WHITE , outputStream) ;
}
public static void generateCode128 ( String content, int width, int height, int onColor, int offColor, OutputStream outputStream) {
Map < EncodeHintType , Object > hints = new HashMap < > ( ) ;
hints. put ( EncodeHintType . CHARACTER_SET , CHARSET ) ;
hints. put ( EncodeHintType . ERROR_CORRECTION , ErrorCorrectionLevel. H ) ;
hints. put ( EncodeHintType . MARGIN , 1 ) ;
hints. put ( EncodeHintType . DATA_MATRIX_SHAPE , SymbolShapeHint . FORCE_SQUARE ) ;
generate ( content, BarcodeFormat . CODE_128 , width, height, hints, onColor, offColor, FORMAT , outputStream) ;
}
private static void generate ( String content, BarcodeFormat barcodeFormat, int width, int height, Map < EncodeHintType , Object > hints, int onColor, int offColor, String imageFormat, OutputStream outputStream) {
try {
MultiFormatWriter writer = new MultiFormatWriter ( ) ;
BitMatrix matrix = writer. encode ( content, barcodeFormat, width, height, hints) ;
MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig ( onColor, offColor) ;
MatrixToImageWriter . writeToStream ( matrix, imageFormat, outputStream, matrixToImageConfig) ;
} catch ( Exception e) {
throw new RuntimeException ( "二维码生成异常" ) ;
}
}
public static void decode ( InputStream inputStream) {
Map < DecodeHintType , String > hints = new HashMap < > ( ) ;
hints. put ( DecodeHintType . CHARACTER_SET , CHARSET ) ;
try {
BufferedImage image = ImageIO . read ( inputStream) ;
LuminanceSource source = new BufferedImageLuminanceSource ( image) ;
BinaryBitmap bitmap = new BinaryBitmap ( new GlobalHistogramBinarizer ( source) ) ;
Result res = new MultiFormatReader ( ) . decode ( bitmap, hints) ;
System . out. println ( res. getText ( ) ) ;
} catch ( Exception e) {
throw new RuntimeException ( e) ;
}
}
public static void main ( String [ ] args) throws FileNotFoundException {
generateQRCode ( "aaaaaaaaaa" , new FileOutputStream ( "C:\\Users\\16\\Desktop\\qrcode.png" ) ) ;
generateCode128 ( "aaaaaaaaaa" , new FileOutputStream ( "C:\\Users\\16\\Desktop\\code128.png" ) ) ;
decode ( new FileInputStream ( "C:\\Users\\16\\Desktop\\qrcode.png" ) ) ;
}
}