解析二维码

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 {

    /** 
     * 解析二维码(QRCode) 
     * @param imgPath 图片路径 
     * @return 
     */  
    public String decoderQRCode(String imgPath) {  
        // QRCode 二维码图片的文件  
        File imageFile = new File(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("二维码内容为:" + qrCon);
    }  

}
















QRCodeImageBean.java
package 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();  
    }  
}

  

posted @ 2016-07-19 16:50  徘徊在人间的水墨  阅读(285)  评论(0编辑  收藏  举报