生成二维码功能(js前端)

生成二维码需要引入qrcode.js和jquery.min.js

<!DOCTYPE html>
<head>
<title>二维码</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/qrcode.js"></script>
</head>
<body>
<input id="text" type="text" value="www.baidu.com" style="width:200px;" />
<button id="btn" onclick="makeCode()">生成</button><br />
<div id="qrcode" style="width:100px; height:100px;margin-top: 15px;"></div>

<script type="text/javascript">
var qrcode = new QRCode(document.getElementById("qrcode"), {
	width : 200,
	height : 200
});

function makeCode () {		
	var elText = document.getElementById("text");
	qrcode.makeCode(elText.value);
}
</script>
</body>
</html>

转载于:https://www.cnblogs.com/zhi-ming/p/10453181.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将Java生成的二维码展示到前端,可以通过以下步骤实现: 1. 使用Java库生成二维码图片,例如zxing或Qrcode。 2. 将生成的二维码图片转换为Base64编码格式,可以使用Java库将图片转换为Base64字符串。 3. 在Java中将Base64字符串返回给前端,可以使用Spring MVC或其他Web框架将Base64字符串作为响应返回给前端。 4. 在前端中使用JavaScript解码Base64字符串,并将其展示为图片。 以下是一个示例Java代码,用于生成二维码并将其返回给前端: ``` @RequestMapping(value = "/qrcode", produces = MediaType.IMAGE_PNG_VALUE) @ResponseBody public ResponseEntity<byte[]> generateQRCode(@RequestParam("text") String text) throws Exception { // 生成二维码图片 QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, 200, 200); // 将二维码图片转换为BufferedImage BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix); // 将BufferedImage转换为Base64字符串 ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "png", baos); byte[] bytes = baos.toByteArray(); String base64Encoded = Base64.getEncoder().encodeToString(bytes); // 将Base64字符串返回给前端 final HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_PNG); return new ResponseEntity<>(Base64.getDecoder().decode(base64Encoded), headers, HttpStatus.CREATED); } ``` 在前端中,可以使用以下JavaScript代码将Base64字符串转换为图片并展示: ``` // 将Base64字符串转换为图片并展示 var img = new Image(); img.src = 'data:image/png;base64,' + base64Encoded; document.getElementById('qrcode').appendChild(img); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值