一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Java中基于maven实现zxing二维码功能

时间:2017-03-13 编辑:简简单单 来源:一聚教程网

maven所需jar

 代码如下 复制代码

  com.google.zxing

  core

  3.0.0

  

  

  com.google.zxing

  javase

  3.0.0 

  

生成二维码

 代码如下 复制代码

publicstaticvoidmain(String[] args)throwsIOException {

String text="www.baidu.com";

intwidth=100;

intheight=100;

String format="png";

Hashtable hints=newHashtable();

hints.put(EncodeHintType.CHARACTER_SET,"utf-8");

hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);

hints.put(EncodeHintType.MARGIN,2);

try{

BitMatrix bitMatrix=newMultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height,hints);

 Path file=newJava.io.File("D:/new.png").toPath();

MatrixToImageWriter.writeToPath(bitMatrix, format, file);

}catch(WriterException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

解析二维码:

publicstaticvoidmain(String[] args)throwsNotFoundException {

MultiFormatReader formatReader=newMultiFormatReader();

File file =newFile("D:/new.png");

BufferedImage image=null;

try{

image = ImageIO.read(file);

}catch(IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

BinaryBitmap binaryBitmap =newBinaryBitmap(newHybridBinarizer(newBufferedImageLuminanceSource(image)));

Hashtable hints=newHashtable();

hints.put(EncodeHintType.CHARACTER_SET,"utf-8");

Result result=formatReader.decode(binaryBitmap,hints);

System.err.println("解析结果:"+result.toString());

System.out.println(result.getBarcodeFormat());

System.out.println(result.getText());

}

热门栏目