首先需要兩個(gè)jar :QRCode.jar尚卫,Qrcodeen.jar 接下來就是具體實(shí)現(xiàn)了
下載地址 http://pan.baidu.com/s/1o82VdTS
package com.utils; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import javax.imageio.ImageIO; import com.swetake.util.Qrcode; import jp.sourceforge.qrcode.QRCodeDecoder; import jp.sourceforge.qrcode.data.QRCodeImage; import jp.sourceforge.qrcode.exception.DecodingFailedException;
public class QRCodeUtils {
/**
* 編碼字符串內(nèi)容到目標(biāo)File對象中
* @param encodeddata
* @param destFile
* @throws IOException
*/
public static void qrCodeEncode(String encodeddata,File destFile) throws IOException{ Qrcode qrcode = new Qrcode(); //錯(cuò)誤修正容量 //L水平 7%的字碼可被修正 //M水平 15%的字碼可被修正 //Q水平 25%的字碼可被修正 //H水平 30%的字碼可被修正 //QR碼有容錯(cuò)能力裤唠,QR碼圖形如果有破損兼蕊,仍然可以被機(jī)器讀取內(nèi)容,最高可以到7%~30%面積破損仍可被讀取窘游。 //相對而言乘综,容錯(cuò)率愈高祭埂,QR碼圖形面積愈大舅巷。所以一般折衷使用15%容錯(cuò)能力羔味。 qrcode.setQrcodeErrorCorrect('M'); qrcode.setQrcodeEncodeMode('B'); //設(shè)置二維碼大小,這個(gè)跟你二維碼的內(nèi)容大小又關(guān)系,內(nèi)容太大就不能識別出來了钠右,7是40個(gè)中文字符(編碼為utf-8的情況下赋元,編碼不一樣,容納的內(nèi)容長度也有變化,gbk為61) qrcode.setQrcodeVersion(7); //設(shè)置二位碼編碼 byte[] d = encodeddata.getBytes("utf-8"); //設(shè)置圖片大小 BufferedImage bi = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.createGraphics(); // 設(shè)置圖片背景色 g.setBackground(Color.WHITE); g.clearRect(0, 0, 139, 139); //設(shè)置二維碼圖片顏色 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); } } } } g.dispose(); bi.flush(); ImageIO.write(bi, "png", destFile); System.out.println("Input Encoded data is:"+encodeddata); }
/**
* 解析二維碼们陆,返回解析內(nèi)容
* @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)), "utf-8"); 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:\\erweima\\QRCode.png"; File qrFile = new File(FilePath); System.out.println(qrFile.exists()); if(qrFile.exists()){ qrFile.delete(); } //編碼 String encodeddata = "阿三發(fā)射點(diǎn)阿三發(fā)射點(diǎn)阿三發(fā)射點(diǎn)阿三發(fā)射點(diǎn)阿三發(fā)射點(diǎn)阿三發(fā)射點(diǎn)阿三阿斯蒂芬阿斯頓阿"; System.out.println(encodeddata.length()); try { QRCodeUtils.qrCodeEncode(encodeddata, qrFile); } catch (IOException e) { // TODO Auto-generated catch block 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); } }