前端将base64转图片(二维码生成)

  1. 效果
  2. 按钮
    					<view  class="flex-item">
    						<button class="custom-button" @click="handleStyledButtonClick()">二维码</button>
    					</view>

  3. 函数
    			handleStyledButtonClick() {
    				var that = this
    				// 接口调用
    				roomdata.getRoomQRCode(function() {
                    //获取返回值base64
    					that.base64 = that.detailmodel.roomQRCode
    					// 将base64字符串转换为ArrayBuffer格式
    					const arrayBuffer = uni.base64ToArrayBuffer(that.base64);
    
    					// 将ArrayBuffer转换为Blob对象
    					const blob = new Blob([arrayBuffer], {
    						type: 'image/png'
    					});
    
    					// 创建Blob URL
    					const imgUrl = URL.createObjectURL(blob);
    					// 弹框显示图片
    					uni.previewImage({
    						urls: [imgUrl],
    					})
    				})
    			},
    			// 将 Base64 字符串转换为 Blob 对象
    			base64ToBlob(base64) {
    				const parts = base64.split(';base64,');
    				const contentType = parts[0].split(':')[1];
    				const raw = window.atob(parts[1]);
    				const rawLength = raw.length;
    				const uInt8Array = new Uint8Array(rawLength);
    
    				for (let i = 0; i < rawLength; ++i) {
    					uInt8Array[i] = raw.charCodeAt(i);
    				}
    
    				return new Blob([uInt8Array], {
    					type: contentType
    				});
    			},

  4. 样式
    	.flex-item {
    		margin-right: 10px;
    	}
    	.custom-button {
    		width: 90px;
    		height: 20px;
    		line-height: 20px;
    		background-color: #2979FF;
    		border: none;
    		cursor: pointer;
    	}

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值