JAVA用QRCode生成二維碼

QRCode jar下載地址:html

生成:http://www.swetake.com/qrcode/index-e.htmljava

讀取:https://zh.osdn.net/projects/qrcode/api

 

示例代碼(引用自:http://www.ibloger.net/article/160.html):this

package cn.utils;  
  
import java.awt.Color;  
import java.awt.Graphics2D;  
import java.awt.Image;  
import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.IOException;  
import java.io.UnsupportedEncodingException;  
  
import javax.imageio.ImageIO;  
  
import jp.sourceforge.qrcode.QRCodeDecoder;  
import jp.sourceforge.qrcode.data.QRCodeImage;  
import jp.sourceforge.qrcode.exception.DecodingFailedException;  
  
import com.swetake.util.Qrcode;  
  
public class QRCodeUtils {  
    /** 
     * 編碼字符串內容到目標File對象中 
     *  
     * @param encodeddata 編碼的內容 
     * @param destFile  生成file文件  1381090722   5029067275903 
     * @throws IOException 
     */  
    public static void qrCodeEncode(String encodeddata, File destFile) throws IOException {  
        Qrcode qrcode = new Qrcode();  
        qrcode.setQrcodeErrorCorrect('M');  // 糾錯級別(L 7%、M 15%、Q 25%、H 30%)和版本有關  
        qrcode.setQrcodeEncodeMode('B');      
        qrcode.setQrcodeVersion(7);     // 設置Qrcode包的版本  
          
        byte[] d = encodeddata.getBytes("GBK"); // 字符集  
        BufferedImage bi = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB);  
        // createGraphics   // 建立圖層  
        Graphics2D g = bi.createGraphics();  
          
        g.setBackground(Color.WHITE);   // 設置背景顏色(白色)  
        g.clearRect(0, 0, 139, 139);    // 矩形 X、Y、width、height  
        g.setColor(Color.BLACK);    // 設置圖像顏色(黑色)  
  
        if (d.length > 0 && d.length < 123) {  
            boolean[][] b = qrcode.calQrcode(d);  
            for (int i = 0; i < b.length; i++) {  
                for (int j = 0; j < b.length; j++) {  
                    if (b[j][i]) {  
                        g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);  
                    }  
                }  
            }  
        }  
          
//      Image img = ImageIO.read(new File("D:/tt.png"));  logo  
//      g.drawImage(img, 25, 55,60,50, null);  
                  
        g.dispose(); // 釋放此圖形的上下文以及它使用的全部系統資源。調用 dispose 以後,就不能再使用 Graphics 對象  
        bi.flush(); // 刷新此 Image 對象正在使用的全部可重構的資源  
  
        ImageIO.write(bi, "png", destFile);  
        System.out.println("Input Encoded data is:" + encodeddata);  
    }  
  
    /** 
     * 解析二維碼,返回解析內容 
     *  
     * @param imageFile 
     * @return 
     */  
    public static String qrCodeDecode(File imageFile) {  
        String decodedData = null;  
        QRCodeDecoder decoder = new QRCodeDecoder();  
        BufferedImage image = null;  
        try {  
            image = ImageIO.read(imageFile);  
        } catch (IOException e) {  
            System.out.println("Error: " + e.getMessage());  
        }  
  
        try {  
            decodedData = new String(decoder.decode(new J2SEImage(image)), "GBK");  
            System.out.println("Output Decoded Data is:" + decodedData);  
        } catch (DecodingFailedException dfe) {  
            System.out.println("Error: " + dfe.getMessage());  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
        return decodedData;  
    }  
  
    public static void main(String[] args) {  
        String FilePath = "d:/qrcode.png";  
        File qrFile = new File(FilePath);  
  
        // 二維碼內容  
        String encodeddata = "Hello X-rapido";  
        try {  
            QRCodeUtils.qrCodeEncode(encodeddata, qrFile);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
  
        // 解碼  
        String reText = QRCodeUtils.qrCodeDecode(qrFile);  
        System.out.println(reText);  
    }  
}  
  
class J2SEImage implements QRCodeImage {  
    BufferedImage image;  
  
    public J2SEImage(BufferedImage image) {  
        this.image = image;  
    }  
  
    public int getWidth() {  
        return image.getWidth();  
    }  
  
    public int getHeight() {  
        return image.getHeight();  
    }  
  
    public int getPixel(int x, int y) {  
        return image.getRGB(x, y);  
    }  
}
相關文章
相關標籤/搜索