maven所需jar
代码如下 | 复制代码 |
|
生成二维码:
代码如下 | 复制代码 |
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()); } |