二维码生成

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
    评论
二维码生成驱动C语言的过程如下: 首先,需要了解二维码的生成原理和格式。二维码是一种矩阵型的图形编码,可以存储大量信息。它的生成过程包括数据编码、纠错码计算和图形绘制三个主要步骤。 在C语言中,可以使用开源的二维码生成库,如ZXing或QRcode等。这些库提供了控制二维码生成的函数和接口,方便我们使用C语言进行二维码生成。 首先,需要初始化一个二维码生成器对象。然后,调用相关函数设置生成器的参数,如二维码版本、纠错级别、编码方式等。这些参数将影响生成的二维码的容量和可靠性。 接下来,需要将待编码的数据传递给生成器。生成器会根据设置的参数将数据编码成二维码格式。编码过程中,可以根据需要进行错误校正,以提高二维码的可靠性。 最后,调用绘制函数将生成的二维码图形绘制到指定的输出设备或文件中。绘制过程中,可以设置二维码的尺寸、颜色、间距等属性。 需要注意的是,二维码的生成需要依赖相应的驱动程序和库文件,因此在使用C语言进行二维码生成前,需要安装和配置相应的驱动和库。 总而言之,生成二维码的过程需要借助于C语言的二维码生成库,通过设置参数、传递数据、进行编码和绘制等步骤来实现。通过合理的调用和配置,可以生成符合要求的二维码图形。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值