Package www.ablanxue.com; Import Java.awt.image.bufferedimage;import java.io.file;import java.io.ioexception;import Javax.imageio.ImageIO; Import Jp.sourceforge.qrcode.qrcodedecoder;import Jp.sourceforge.qrcode.exception.decodingfailedexception;public Class QRCode {/** * parse QR code (qrcode) * @param imgpath Picture Path * @return * */public String DECODERQRC Ode (String Imgpath) {//qrcode QR code picture file ImageFile = new files (imgpath); BufferedImage bufimg = null; String content = null; try {bufimg = Imageio.read (ImageFile); Qrcodedecoder decoder = new Qrcodedecoder (); Content = new String (Decoder.decode (New Qrcodeimagebean (bufimg)), "Utf-8"); } catch (IOException e) {System.out.println ("Error:" + e.getmessage ()); E.printstacktrace (); } catch (Decodingfailedexception DfE) {System.out.println ("Error:" + dfe.getmessage ()); Dfe.printstacktrace (); } return content; } public static void Main (string[] args) {String Imgpath = "K:/aaa.png"; QRCode handler = new QRCode (); String Qrcon = Handler.decoderqrcode (Imgpath); SYSTEM.OUT.PRINTLN ("Decoder success!!!"); System.out.println ("Two-dimensional code content:" + Qrcon); }}qrcodeimagebean.javapackage www.ablanxue.com; Import Java.awt.image.bufferedimage;import Jp.sourceforge.qrcode.data.qrcodeimage;public class QRCodeImageBean Implements Qrcodeimage {BufferedImage bufimg; Public Qrcodeimagebean (BufferedImage bufimg) {this.bufimg = bufimg; } @Override public int getheight () {return bufimg.getheight (); } @Override public int getPixel (int x, int y) {return Bufimg.getrgb (x, y); } @Override public int getwidth () {return bufimg.getwidth (); } }
Parsing two-dimensional code