20180124-20180131 学习日记

在慕课网上学习一些有关于JAVA部分的二维码生成小程序,由JSP显示,由JAVA代码生成相应的二维码:

部分JAVA代码:

public static void main(String[] args) {
int width = 300;
int height = 300;
String format = "png";
String content = "https://www.baidu.com";

//定义二维码的参数
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("E:/eclipse/projeck/2DCode/2DCode.png").toPath();

MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (Exception e) {
e.printStackTrace();
}
}

//解析二维码

public static void main(String[] args){

try {
MultiFormatReader formatReader = new MultiFormatReader();

File  file = new File("E:/eclipse/projeck/2DCode/2DCode.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("2DCode格式类型:" + result.getBarcodeFormat());
System.out.println("2DCode文本内容:" + result.getText());
} catch (NotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

部分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.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.qrcode.min.js"></script>
</head>
<body>
<h1>生成二维码如下:<br /></h1>
<div id="qrcode"></div>
<script type="text/javascript">
jQuery('#qrcode').qrcode("http://wx.nsi-soft.com:5906/sso/main.do?requestUrl=http%3A%2F%2Fwx.nsi-soft.com%3A5906%2FnsiOOC%2F%3Bjsessionid%3DF26949EA325B818E435565A86C7476FB");
</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值