首页 > 代码库 > Java实现二维码QRCode的编码和解码
Java实现二维码QRCode的编码和解码
转:http://blog.csdn.net/ljb_blog/article/details/6700834
【一】、编码:
QRCodeEncoderHandler.java
1 package michael.qrcode; 2 3 import java.awt.Color; 4 import java.awt.Graphics2D; 5 import java.awt.image.BufferedImage; 6 import java.io.File; 7 8 import javax.imageio.ImageIO; 9 10 import com.swetake.util.Qrcode;11 12 /**13 * 二维码生成器14 * @blog http://sjsky.iteye.com15 * @author Michael16 */17 public class QRCodeEncoderHandler {18 19 /**20 * 生成二维码(QRCode)图片21 * @param content22 * @param imgPath23 */24 public void encoderQRCode(String content, String imgPath) {25 try {26 27 Qrcode qrcodeHandler = new Qrcode();28 qrcodeHandler.setQrcodeErrorCorrect(‘M‘);29 qrcodeHandler.setQrcodeEncodeMode(‘B‘);30 qrcodeHandler.setQrcodeVersion(7);31 32 System.out.println(content);33 byte[] contentBytes = content.getBytes("gb2312");34 35 BufferedImage bufImg = new BufferedImage(140, 140,36 BufferedImage.TYPE_INT_RGB);37 38 Graphics2D gs = bufImg.createGraphics();39 40 gs.setBackground(Color.WHITE);41 gs.clearRect(0, 0, 140, 140);42 43 // 设定图像颜色 > BLACK44 gs.setColor(Color.BLACK);45 46 // 设置偏移量 不设置可能导致解析出错47 int pixoff = 2;48 // 输出内容 > 二维码49 if (contentBytes.length > 0 && contentBytes.length < 120) {50 boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);51 for (int i = 0; i < codeOut.length; i++) {52 for (int j = 0; j < codeOut.length; j++) {53 if (codeOut[j][i]) {54 gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);55 }56 }57 }58 } else {59 System.err.println("QRCode content bytes length = "60 + contentBytes.length + " not in [ 0,120 ]. ");61 }62 63 gs.dispose();64 bufImg.flush();65 66 File imgFile = new File(imgPath);67 68 // 生成二维码QRCode图片69 ImageIO.write(bufImg, "png", imgFile);70 71 } catch (Exception e) {72 e.printStackTrace();73 }74 75 }76 77 /**78 * @param args the command line arguments79 */80 public static void main(String[] args) {81 String imgPath = "D:/test/twocode/Michael_QRCode.png";82 83 String content = "Hello 大大、小小,welcome to QRCode!"84 + "\nMyblog [ http://sjsky.iteye.com ]"85 + "\nEMail [ sjsky007@gmail.com ]" + "\nTwitter [ @suncto ]";86 87 QRCodeEncoderHandler handler = new QRCodeEncoderHandler();88 handler.encoderQRCode(content, imgPath);89 90 System.out.println("encoder QRcode success");91 }92 }
【二】、解码:
QRCodeDecoderHandler.java
package michael.qrcode;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.data.QRCodeImage;import jp.sourceforge.qrcode.exception.DecodingFailedException;/** * @blog http://sjsky.iteye.com * @author Michael */public class QRCodeDecoderHandler { /** * 解码二维码 * @param imgPath * @return String */ public String decoderQRCode(String imgPath) { // QRCode 二维码图片的文件 File imageFile = new File(imgPath); BufferedImage bufImg = null; String decodedData = http://www.mamicode.com/null;"gb2312"), // "gb2312")); // } catch (Exception e) { // // TODO: handle exception // } } catch (IOException e) { System.out.println("Error: " + e.getMessage()); e.printStackTrace(); } catch (DecodingFailedException dfe) { System.out.println("Error: " + dfe.getMessage()); dfe.printStackTrace(); } return decodedData; } /** * @param args the command line arguments */ public static void main(String[] args) { QRCodeDecoderHandler handler = new QRCodeDecoderHandler(); String imgPath = "d:/test/twocode/Michael_QRCode.png"; String decoderContent = handler.decoderQRCode(imgPath); System.out.println("解析结果如下:"); System.out.println(decoderContent); System.out.println("========decoder success!!!"); } class J2SEImage implements QRCodeImage { BufferedImage bufImg; public J2SEImage(BufferedImage bufImg) { this.bufImg = bufImg; } public int getWidth() { return bufImg.getWidth(); } public int getHeight() { return bufImg.getHeight(); } public int getPixel(int x, int y) { return bufImg.getRGB(x, y); } }}
Java实现二维码QRCode的编码和解码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。