Java生成二维码

该博客主要展示了如何使用Java后台生成二维码,并结合前端页面展示二维码及相关证书信息。通过HttpServletRequest和HttpServletResponse处理HTTP请求,根据$id$获取实体类信息,生成包含证书ID的二维码内容。同时,将证书的详细信息如证书编号、生产者名称等以HTML表格形式在前端页面展示。
摘要由CSDN通过智能技术生成

Java后台代码

	/**
	 * 生成二维码
	 * @param request
	 * @param response
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public ModelAndView getQCIMG(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
		String id = request.getParameter("id");
		// 获取实体类信息
		LicenceManageInfo licence = licenceManageInfoService.editLicenceManageInfoById(id);
		String content = "http://ip:port或域名/licencemanageinfoopen/getQCodeDate.do?id="+id;
		request.setAttribute("content", content);
		
		QrConfig config = new QrConfig(300, 300);
		// 设置边距,既二维码和背景之间的边距
		config.setMargin(3);
//		// 设置前景色,既二维码颜色(青色)
//		config.setForeColor(Color.black.getRGB());
//		// 设置背景色(灰色)
//		config.setBackColor(Color.GRAY.getRGB());
		
		// 生成二维码到文件,也可以到流
		try {
			ServletOutputStream outputStream = response.getOutputStream();
			BufferedImage bufferedImage = QrCodeUtil.generate(content, config);
			ImageIO.write(bufferedImage, "png", outputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 生成二维码
	 * @param request
	 * @param response
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public ModelAndView getQCodeDate(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
		String id = request.getParameter("id");
		request.setAttribute("id", id);
		// 获取实体类信息
		LicenceManageInfo licence = licenceManageInfoService.editLicenceManageInfoById(id);
			
		StringBuffer sb = new StringBuffer();
		sb.append("证书编证书号:"+licenceNumber);sb.append("\n");
		sb.append("生产者名称:"+companyName);sb.append("\n");
		sb.append("注册地址:"+companyAddress);sb.append("\n");
		sb.append("产品名称:"+productName);sb.append("\n");
		sb.append("产品型号:"+productModel);sb.append("\n");
		sb.append("涵盖型号:"+productSubModel);sb.append("\n");
		sb.append("有效期:"+sdf.format(new Date(licenceEndDate)));sb.append("\n");
		sb.append("发证机构:山东省农业机械试验鉴定站");sb.append("\n");
		sb.append("售后服务电话:"+shhou);
			
		String content = new String(sb.toString().getBytes("gb2312"),"ISO8859-1");
		request.setAttribute("qcodeContent", content);
		
		request.setAttribute("licenceNumber", licenceNumber);
		request.setAttribute("companyName", companyName);
		request.setAttribute("companyAddress", companyAddress);
		request.setAttribute("productName", productName);
		request.setAttribute("productModel", productModel);
		request.setAttribute("productSubModel", productSubModel);
		request.setAttribute("shhouTel", shhou);
		request.setAttribute("licenceEndDate", sdf.format(new Date(licenceEndDate)));
		return new ModelAndView("app/qcode.jsp");
	}

<title>二维码</title>
</head>
<body>
<%-- 	<div id="qrcode" style="text-align: center;margin-top: 5px;">
		<img alt="证书二维码" src="getQCIMG.do?id=${id}" id="showQRCode" height="100%" width="100%">
	</div>
	<input  type="hidden" value="${qcodeContent}" id="qcodeContent"> --%>
	
	<div class="panel-body" style="margin-top: 10px;">
		<table class="table table-bordered table-hover detail-table" id="alternatecolor">
			<tr>
				<td width="35%"><h4 style="font-weight: bold;">证书编号</h4></td>
				<td width="65%"><span class="tablevalue">${licenceNumber}</span></td>
			</tr>
			<tr>
				<td width="35%"><h4 style="font-weight: bold;">生产者名称</h4></td>
				<td width="65%"><span class="tablevalue">${companyName}</span></td>
			</tr>
			<tr>
				<td width="35%"><h4 style="font-weight: bold;">注册地址</h4></td>
				<td width="65%"><span class="tablevalue">${companyAddress}</span></td>
			</tr>
			<tr>
				<td width="35%"><h4 style="font-weight: bold;">产品名称</h4></td>
				<td width="65%"><span class="tablevalue">${productName}</span></td>
			</tr>
			<tr>
				<td width="35%"><h4 style="font-weight: bold;">产品型号</h4></td>
				<td width="65%"><span class="tablevalue">${productModel}</span></td>
			</tr>
			<tr>
				<td width="35%"><h4 style="font-weight: bold;">涵盖型号</h4></td>
				<td width="65%"><span class="tablevalue">${productSubModel}</span></td>
			</tr>
			<tr>
				<td width="35%"><h4 style="font-weight: bold;">有效期</h4></td>
				<td width="65%"><span class="tablevalue">${licenceEndDate}</span></td>
			</tr>
			<tr>
				<td width="35%"><h4 style="font-weight: bold;">售后服务电话</h4></td>
				<td width="65%"><span class="tablevalue">${shhouTel}</span></td>
			</tr>
		</table>
	</div>
</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值