java生成二维码,并在前端展示。
生活随笔
收集整理的這篇文章主要介紹了
java生成二维码,并在前端展示。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
java生成二維碼,并在前端展示,掃碼實現下載功能。
- 后端生成二維碼以流的形式
- 前端接收二維碼并展示
后端生成二維碼以流的形式
這是以流的形式展示二維碼。當然也可以以文件的格式,文件格式就是Path path = FileSystems.getDefault().getPath("3.png");MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); 釋放就行 //text 掃描二維碼展示的內容,width寬度,height長度public static String generateQRCodeImage(String text, int width, int height) throws WriterException, IOException {QRCodeWriter qrCodeWriter = new QRCodeWriter();BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);//Path path = FileSystems.getDefault().getPath("3.png");ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();//MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);byte[] pngData = pngOutputStream.toByteArray();return Base64.encode(pngData );}前端接收二維碼并展示
前端代碼展示,前端以base64進行回顯后端傳輸的流。 <img id="img" src = "image">$.ajax({url: "${base}/risen/rfb/whglRwgl/qrcode?rfbUuid=${result.rfbUuid!''}",type: "post",success: function (obj) {$("#img").attr("src",'data:image/png;base64,' + obj);},})總結
以上是生活随笔為你收集整理的java生成二维码,并在前端展示。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring boot 中阿里druid
- 下一篇: 【Java 后端接收前端的富文本数据,其