1. 二维码第三方包下载地址,该第三方包由日本QRCode开源组织提供:
http://pan.baidu.com/s/1gdrHcrh
2. 演示代码下载地址(作者原创):
http://pan.baidu.com/s/1jGGD2q6
3 源代码清单说明:
解码器:输入二维码图像数据,或者包含二维码图像数据的InputStream,输出解析好的字符数据。
EnhancedQRCodeDecoder.java
编码器:输入字符串,编码成二维码图像数据,可以根据自己的喜好保存成任意图片格式。
QRCodeEncoder.java
实现第三方接口标准的默认二维码图像类。
QRCodeImageDefult.java
编码解码演示。
QRCodeDemo.java
一个图形化界面,可以调整各自参数的二维码演示,考虑到内容较多,读者可以点击上方的下载地址,自行下载运行。内含源代码zip包。
qrcodeDemo.jar
QRCodeUI.java
4.部分源代码明细
EnhancedQRCodeDecoder.java
package home.ibm.tony.qrcode; import java.io.IOException; import java.io.InputStream; 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; public class EnhancedQRCodeDecoder extends QRCodeDecoder { public byte[] decode(InputStream input) throws DecodingFailedException, IOException { QRCodeImageDefult image = new QRCodeImageDefult(ImageIO.read(input)); return super.decode(image); } public String decodeToString(QRCodeImage qrCodeImage) throws DecodingFailedException, UnsupportedEncodingException { return new String(super.decode(qrCodeImage), "utf-8"); } public String decodeToString(InputStream input) throws DecodingFailedException, IOException { return new String(decode(input)); } }
QRCodeEncoder.java
package home.ibm.tony.qrcode; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.UnsupportedEncodingException; import com.swetake.util.Qrcode; public class QRCodeEncoder { private Color bgColor; private Qrcode configure; private Color fgColor; private int margin; private int pixelSize; public QRCodeEncoder() { setDefault(); bgColor = Color.WHITE; fgColor = Color.BLACK; pixelSize = 5; margin = 2; } public BufferedImage encode(String content) throws UnsupportedEncodingException { if(content == null || content.length() == 0 || content.length() > 256) { content = "http://ieqq.iteye.com/blog/2114077"; } byte[] bstr = content.getBytes("UTF-8"); boolean[][] result = new boolean[0][0]; result = configure.calQrcode(bstr); int imageSize = result.length * pixelSize + margin * 2; BufferedImage bi = new BufferedImage(imageSize, imageSize, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.createGraphics(); g.setBackground(bgColor); // 背景颜色 g.clearRect(0, 0, imageSize, imageSize); g.setColor(fgColor); // 条码颜色 for (int i = 0; i < result.length; i++) { for (int j = 0; j < result.length; j++) { if (result[j][i]) { g.fillRect(j * pixelSize + margin, i * pixelSize + margin, pixelSize, pixelSize); } } } g.dispose(); bi.flush(); return bi; } public Color getBgColor() { return bgColor; } public Qrcode getConfigure() { return configure; } public char getErrorCorrect() { return configure.getQrcodeErrorCorrect(); } public Color getFgColor() { return fgColor; } public int getMargin() { return margin; } public int getPixelSize() { return pixelSize; } public int getVersion() { return configure.getQrcodeVersion(); } public void setBgColor(Color bgColor) { this.bgColor = bgColor; } public void setConfigure(Qrcode configure) { this.configure = configure; } protected void setDefault() { configure = new Qrcode(); // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小 configure.setQrcodeErrorCorrect('M'); configure.setQrcodeEncodeMode('B'); // 设置设置二维码版本号,取值范围1-40,值越大尺寸越大,可存储的信息越大 configure.setQrcodeVersion(3); } public void setErrorCorrect(char errorCorrect) { this.configure.setQrcodeErrorCorrect(errorCorrect); } public void setFgColor(Color fgColor) { this.fgColor = fgColor; } public void setMargin(int margin) { this.margin = margin; } public void setPixelSize(int pixelSize) { this.pixelSize = pixelSize; } public void setVersion(int version) { this.configure.setQrcodeVersion(version); } }
QRCodeImageDefult.java
package home.ibm.tony.qrcode; import java.awt.image.BufferedImage; import jp.sourceforge.qrcode.data.QRCodeImage; public class QRCodeImageDefult implements QRCodeImage { private BufferedImage image; public QRCodeImageDefult() { } public QRCodeImageDefult(BufferedImage image) { this.image = image; } public int getHeight() { return image.getHeight(); } public BufferedImage getImage() { return image; } public int getPixel(int x, int y) { return image.getRGB(x, y); } public int getWidth() { return image.getWidth(); } public void setImage(BufferedImage image) { this.image = image; } }
QRCodeDemo.java
package home.ibm.tony.qrcode.test; import home.ibm.tony.qrcode.EnhancedQRCodeDecoder; import home.ibm.tony.qrcode.QRCodeEncoder; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.imageio.ImageIO; public class QRCodeDemo { public static void main(String[] args) throws IOException { String imgPath = "c:/z.png"; String encoderContent = "http://ieqq.iteye.com/"; QRCodeEncoder encoder = new QRCodeEncoder(); ImageIO.write(encoder.encode(encoderContent), "png", new File(imgPath)); System.out.println("done!"); EnhancedQRCodeDecoder decoder = new EnhancedQRCodeDecoder(); String decoderContent = decoder.decodeToString(new FileInputStream( new File(imgPath))); System.out.println("解析结果如下:"); System.out.println(decoderContent); } }