1、使用前端jquery.qrcode.min.js框架生成二维码

    在https://github.com/jeromeetienne/jquery-qrcode上下载对应的夹包,生成二维码的JSP页面如下:

<%@ 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="<%=request.getContextPath()%>/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.qrcode.min.js"></script>
<!--或者使用如下引入方式也可
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.2.1.min.js"></script>   
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.qrcode.min.js"></script> 
 -->

</head>
<body>
	<script type="text/javascript">
		//将Unicode编码转为UTF-8编码
		function utf16to8(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>

	<h1>生成二维码</h1>
	<div id="qrcode"></div>
	<script type="text/javascript">
		var str = "清华大学";
		var out = utf16to8(str);
		//jQuery('#qrcode').qrcode(out);
		jQuery('#qrcode').qrcode({
			width : 300,
			height : 300,
			text : out
		});
	</script>
</html>


2、使用后端的zxing夹包生成二维码

    在GitHub上面有开源项目https://github.com/zxing/,下载到本地后解压缩,将core和javase文件夹中的所有.java文件拷贝到Java项目中,然后export为zxing.jar的夹包,作为项目的引用。

生成二位码的源码如下:

public class Genarate2DCode {   
	public static void main(String[] args) throws Exception {
		//二维码宽度
		int width=300;
		//二维码高度
		int height=300;
		//二维码图片格式
		String format="png";
		//二维码内容,这里为VCard规范,可以保存个人名片信息
		String contents=
				"BEGIN:VCARD"+"\n"
		        +"VERSION:2.1"+"\n"
				+"N:姓;名"+"\n"
				+"FN:姓名"+"\n"
				+"NICKNAME:昵称"+"\n"
				+"ORG:公司;部门"+"\n"
				+"TITLE:职位"+"\n"
				+"TEL;WORK;VOICE:工作电话"+"\n"
				+"TEL;HOME;VOICE:家庭电话"+"\n"
				+"TEL;CELL;VOICE:移动电话"+"\n"				
				+"ROLE:职称"+"\n"
				+"ADR;WORK:门牌号;街道;福田;深圳;广东;30314;国家"+"\n"				
				+"ADR;HOME:门牌号;街道;盐田;深圳;广东;30314;国家"+"\n"
				+"EMAIL;PREF;INTERNET:邮箱地址"+"\n"	
				+"URL:https://www.baidu.com"+"\n"
				+"BDAY:出生日期"+"\n"
				+"PHOTO;VALUE=uri:https://timgsa.baidu.com/timg?xxxx"+"\n"
				+"REV:20080424T195243Z"+"\n"
				+"END:VCARD";
		
		//定义二维码参数的哈希映射表
		HashMap<EncodeHintType,Object> hints=new HashMap<EncodeHintType,Object>();
		//编码方式,支持中文
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		//容错等级
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
		//二维码边距
		hints.put(EncodeHintType.MARGIN, 1);
		//生成点阵
		BitMatrix bitMatrix=new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height,hints);
		Path file=new File("E:/Img.png").toPath();
		MatrixToImageWriter.writeToPath(bitMatrix, format, file);		
	}	
}


  二维码扫描结果为:



识别二维码的源码如下:

public class ReadQRCode {
	public static void main(String[] args) throws Exception {
		MultiFormatReader multiFormatReader = new MultiFormatReader();

		File file = new File("E:/Img.png");
		BufferedImage image = ImageIO.read(file);
		BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
		
		// 定义二维码参数的哈希映射表
		HashMap<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
		// 编码方式,支持中文
		hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
                //输出识别结果
		Result result = multiFormatReader.decode(binaryBitmap, hints);
		System.out.println(result);

	}

}


   

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐