生成简单二维码的方法

1.基于zxing的二维码

相关文件下载地址:https://github.com/zxing/zxing/releases

生成二维码

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class CreateQrcode {

	public static void main(String[] args) throws IOException {
		int width=300;
		int height=300;
		String format="png";
		String content="hello world";
		//配置参数
		HashMap hints=new HashMap();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN, 2);
        
        try {
		BitMatrix bitmatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
		Path file=new File("D:/img.img.png").toPath();
		MatrixToImageWriter.writeToPath(bitmatrix, format, file);
		} catch (WriterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

解析二维码

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class ReadQrcode {

	public static void main(String[] args) throws IOException, NotFoundException {
		MultiFormatReader formatReader=new MultiFormatReader();
		File file=new File("D:/img.png");
		BufferedImage image=ImageIO.read(file);
		BinaryBitmap binaryBitmap=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
		HashMap hints=new HashMap();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		Result result=formatReader.decode(binaryBitmap,hints);
		System.out.println("解析结果:"+result.toString());
		System.out.println("二维码格式类型:"+result.getBarcodeFormat());
		System.out.println("二维码文本内容:"+result.getText());
	}

}

2.jquery生成二维码

js下载链接:https://github.com/jeromeetienne/jquery-qrcode

首先,用通常的脚本标签将它包含在你的网页中。

<script type="text/javascript" src="jquery.qrcode.min.js"></script>

然后创建一个包含生成的qrcode映像的DOM元素。

<div id="qrcode"></div>

然后在这个容器中添加qrcode

$('#qrcode').qrcode("this plugin is great");

您可以设置生成的qrcode的高度和宽度:

$('#qrcode').qrcode({width: 64,height: 64,text: "size doesn't matter"});

样例:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>生成二维码</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.qrcode.min.js"></script>
</head>
<body>
生成的二维码如下:<br>
   <div id="qrcode"></div>
 <script type="text/javascript">
	   $('#qrcode').qrcode("this plugin is great");
</script>
</body>
</html>

注意:jquery的js文件也要被引用进去,同时应该在jquery.qrcode.min.js之前被引用。

猜你喜欢

转载自blog.csdn.net/ljcgit/article/details/81384301