Java生成二维码

内容大部分属于转载

并且个人进行一些内容搜集


编码:

QRCodeEncoderHandler.java

package QrTest;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;

public class QRCodeEncodeHandler {
    
    public void encoderQRcode(String content, String imgPath) {
        try {
            Qrcode qrcodeHandler = new Qrcode();
            qrcodeHandler.setQrcodeErrorCorrect('M');
            qrcodeHandler.setQrcodeEncodeMode('B');
            qrcodeHandler.setQrcodeVersion(7);
            
            System.out.println(content);
            byte[] contentBytes = content.getBytes("gb2312");
            
            BufferedImage bufImg = new BufferedImage(140, 140, BufferedImage.TYPE_INT_RGB);
            
            Graphics2D gs = bufImg.createGraphics();
            
            gs.setBackground(Color.WHITE);
            gs.clearRect(0, 0, 140, 140);
            
            //设定图像颜色 > Color.Black
            gs.setColor(Color.BLACK);
            
            //设置偏移量,不设置可能会导致解析出错
            int pixoff = 3;
            //输出内容 > 二维码
            if (contentBytes.length > 0 && contentBytes.length < 120) {
                boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
                for (int i = 0; i < codeOut.length; i++) {
                    for (int j = 0; j < codeOut.length; j++) {
                        //这里注意,二维码打印应该是
                        if (codeOut[j][i]) {
                            gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
                        }
                    }
                }
            }
            else {
                System.err.println("QRCode content bytes length = " + content.length() + " not in [0, 120]");
            }
            
            gs.dispose();
            bufImg.flush();
            
            File imgFile = new File(imgPath);
            //生成二维码的图片
            ImageIO.write(bufImg, "png", imgFile);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String imgPath = "H:/qrtest/1.png";
        String content = "I love you";
        
        QRCodeEncodeHandler handler = new QRCodeEncodeHandler();
        handler.encoderQRcode(content, imgPath);
        
        System.out.println("succeed");
    }

}

引自:http://www.micmiu.com/enterprise-app/java-qrcode/

里面有更多的内容,比如解码和新的类库zxing的介绍


链接:http://download.csdn.net/detail/liuzhen_333/7346643

csdn 0积分下载qrcode_swetake.jar


链接:http://coolshell.cn/articles/10590.html

二维码详解


链接:http://blog.csdn.net/lcx46/article/details/13626841

使用zxing生成嵌入图片的二维码


链接:http://blog.csdn.net/u014184244/article/details/39692971

更加详细的

你可能感兴趣的:(Java生成二维码)