二维码生成

4 篇文章 0 订阅
2 篇文章 0 订阅

一,jQuery生成二维码,有两种方式,需要引入jQuery的js文件和jquery.qrcode.min.js文件qrcode.min.js下载地址,代码如下:

<!doctype html>
<html>
    <head>
        <style>
            *{margin:0;padding:0;}
            a{text-decoration:none;}
            li{list-style:none;}
        </style>
    <script type="text/javascript" src="jquery-3.2.0.min.js"></script> 
    <script type="text/javascript" src="jquery.qrcode.min.js"></script>
    </head>
    <body>
        <div id="code"></div>
        <script type="text/javascript">
            $("#code").qrcode({ 
                render: "table", //table方式,也可以选择canvas方式
                width: 200, //二维码宽度 
                height:200, //二维码高度 
                text: "http://www.baidu.com" //任意内容,网址或文字也可以,中文需调用转码函数
            }); 
            /**
            *中文转码
            */
            function toUtf8(str) {    
                var out, i, len, c;    
                out = "";    
                len = str.length;    
                for(i = 0; i < len; i++) {    
                    c = str.charCodeAt(i);    
                    if ((c >= 0x0001) && (c <= 0x007F)) {    
                        out += str.charAt(i);    
                    } else if (c > 0x07FF) {    
                        out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));    
                        out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));    
                        out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));    
                    } else {    
                        out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));    
                        out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));    
                    }    
                }    
                return out;    
            }
        </script>
    </body>
</html>

二,java方式生成二维码,主要是利用gui的绘图原理,需要引入一个core-x.x.x.jar包下载地址,同时需要依赖两个类MatrixToImageConfig.java和MatrixToImageWriter.java:


package test;

import java.io.File;
import java.util.Hashtable;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;

public class Test {
    public static void main(String[] args) throws Exception {
        String text = "https://www.baidu.com/";//
        int width = 400;
        int height = 400;
        String format = "png";
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text,
        BarcodeFormat.QR_CODE, width, height, hints);
        File outputFile = new File("D:/codeImg.png");//将二维码输出到"D:/codeImg.png"路径的图片中
        MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
        System.out.println("It is ok!");
    }
}
**MatrixToImageConfig.java代码**:
package test;

import java.awt.image.BufferedImage;

/**
 * Encapsulates custom configuration used in methods of
 * {@link MatrixToImageWriter}.
 */
public final class MatrixToImageConfig {

    public static final int BLACK = 0xFF000000;
    public static final int WHITE = 0xFFFFFFFF;

    private final int onColor;
    private final int offColor;

    /**
     * Creates a default config with on color {@link #BLACK} and off color
     * {@link #WHITE}, generating normal black-on-white barcodes.
     */
    public MatrixToImageConfig() {
        this(BLACK, WHITE);
    }

    /**
     * @param onColor
     *            pixel on color, specified as an ARGB value as an int
     * @param offColor
     *            pixel off color, specified as an ARGB value as an int
     */
    public MatrixToImageConfig(int onColor, int offColor) {
        this.onColor = onColor;
        this.offColor = offColor;
    }

    public int getPixelOnColor() {
        return onColor;
    }

    public int getPixelOffColor() {
        return offColor;
    }

    int getBufferedImageColorModel() {
        // Use faster BINARY if colors match default
        return onColor == BLACK && offColor == WHITE ? BufferedImage.TYPE_BYTE_BINARY
                : BufferedImage.TYPE_INT_RGB;
    }

}

MatrixToImageWriter.java代码

package test;

import javax.imageio.ImageIO;

import com.google.zxing.common.BitMatrix;

import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;

public class MatrixToImageWriter {
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    private MatrixToImageWriter() {
    }
    /**
    * 以缓冲图片的形式输出二维码
    */
    public static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
            }
        }
        return image;
    }
    /**
    * 将二维码输出到指定的文件中
    */
    public static void writeToFile(BitMatrix matrix, String format, File file)
            throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, file)) {
            throw new IOException("Could not write an image of format "
                    + format + " to " + file);
        }
    }
    /**
    * 将二维码输出到指定的输出流,可应用于网页
    */
    public static void writeToStream(BitMatrix matrix, String format,
            OutputStream stream) throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, stream)) {
            throw new IOException("Could not write an image of format "
                    + format);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值