Jaspereport QRCODE html 预览 二维码不显示问题解

主要 参考:https://stackoverflow.com/questions/6942373/export-to-single-html-with-embedded-images-using-jasper-report

环境:springboot2.0   Jaspereport 6.8.0  ,

在之前的增加二维码显示的依赖包

<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>6.8.1</version>
</dependency>
<!--addnew -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.2</version>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.1.0</version>
</dependency>

<dependency>
    <groupId>net.sf.barcode4j</groupId>
    <artifactId>barcode4j</artifactId>
    <version>2.1</version>
</dependency>

<dependency>
    <groupId>org.apache.xmlgraphics</groupId>
    <artifactId>batik-bridge</artifactId>
    <version>1.11</version>
</dependency>
<dependency>
    <groupId>cn.lesper</groupId>
    <artifactId>iTextAsian</artifactId>
    <version>3.0</version>
</dependency>

在使用HtmlExporter生成在线预览的时候不显示二维码 不显示的原因可能

返回的img的src的找不到,应用在启动的时候没用注册

net.sf.jasperreports.j2ee.servlets.ImageServlet

监听,在没用注册此监听的情况下,注册监听的方法待验证,

使用img src 流的方式在页面上显示QRCODE解决在页面不显示二维码的问题

关键代码片段:

预览:

   public void previewReport(String modelId, HttpServletResponse response) throws Exception {
      /*  //exportByJson(response,modelId,ReportUtil.PDF_TYPE,"",null);
        testjDBC(modelId,response);*/
         //jxrmxl的ReportModel 
        //生成HTML 预览用这种模式   
        ReportModel reportModel = reportModelService.findById(modelId).get();
        //添加导出记录
        //创建 JasperReport 对象
            JasperReport jasperReport = createJasperReport(reportModel);
            JREmptyDataSource jrEmptyDataSource = new JREmptyDataSource();
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), jrEmptyDataSource);
           ReportUtil.exportHtml(response, jasperPrint);
        //ReportUtil.exportPdf(response,"test1",jasperPrint);
    }
  /**
     * 创建 JasperReport 对象
     *
     * @param reportModel
     * @return
     * @throws Exception
     */
    @Override
    public JasperReport createJasperReport(ReportModel reportModel) throws Exception {
        JasperReport jasperReport;
        if (null == reportModel.getFileByte()) {
            throw new Exception("文件不存在!");
        }
        /*File reportFile = new File(reportModel.getFilePath());
        if (!reportFile.exists())
            throw new Exception("模板文件不存在");*/
        if (reportModel.getType() == 1) {
            //编译,并得到jasperReport对象,相当于.jasper文件
            jasperReport = JasperCompileManager.compileReport(new ByteArrayInputStream(reportModel.getFileByte()));
        } else {
            //直接加载编译后的模板文件
            jasperReport = (JasperReport) JRLoader.loadObject(new ByteArrayInputStream(reportModel.getFileByte()));
        }
        return jasperReport;
    }

 

 

 /**
     * 以html的形式打印报表
     *
     * @param response
     * @param jasperPrint 打印对象
     * @throws Exception
     */
    public static void exportHtml(HttpServletResponse response, JasperPrint jasperPrint) throws Exception {
        response.setContentType("text/html");//以页面的形式打开
        /*JRHtmlExporter jrHtmlExporter = new JRHtmlExporter();
        jrHtmlExporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, false);//解决错乱显示img标签问题
        jrHtmlExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
        jrHtmlExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
        jrHtmlExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        jrHtmlExporter.exportReport();*/
        Map<String, String> images = new HashMap<>();
        HtmlExporter exporter=new HtmlExporter();
        String[] propertyNames = jasperPrint.getPropertyNames();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
       // exporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getOutputStream()));
        SimpleHtmlReportConfiguration simpleHtmlReportConfiguration  = new SimpleHtmlReportConfiguration();
        //将svg转换成img(png)格式
        simpleHtmlReportConfiguration.setConvertSvgToImage(true);
        SimpleHtmlExporterConfiguration reportExportConfiguration =new SimpleHtmlExporterConfiguration();
        exporter.setConfiguration(reportExportConfiguration );
        exporter.setConfiguration(simpleHtmlReportConfiguration);
        //ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ServletOutputStream outputStream = response.getOutputStream();
        SimpleHtmlExporterOutput simpleHtmlExporterOutput = new SimpleHtmlExporterOutput(response.getOutputStream());
        //生成imgsrc为base64格式的资源
        simpleHtmlExporterOutput.setImageHandler(new HtmlResourceHandler() {
            @Override
            public void handleResource(String id, byte[] data) {
                System.err.println("id" + id);
                images.put(id, "data:image/png;base64," + Base64.encodeBytes(data));
            }

            @Override
            public String getResourcePath(String id) {
                return images.get(id);
            }
        });
        exporter.setExporterOutput(simpleHtmlExporterOutput);
        exporter.exportReport();
    }

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Html5-qrcode可以用于识别小二维码Html5-qrcode是一个基于JavaScript的库,用于在网页上进行二维码扫描和识别。它允许用户使用摄像头扫描网页上的二维码,并将其码为文本或URL。这样,用户就可以方便地识别小二维码。 使用Html5-qrcode进行小二维码的识别非常简单。首先,我们需要在网页中引入Html5-qrcode库,并创建一个用于显示扫描结果的元素。接下来,我们需要调用Html5-qrcode的API函数以开始扫描二维码。 在代码中,我们可以使用以下的示例代码来实现小二维码的识别: ``` <!DOCTYPE html> <html> <head> <title>Html5-qrcode</title> <script src="html5-qrcode.min.js"></script> </head> <body> <h1>小二维码识别</h1> <div id="qrcode"></div> <script> var qrcode = new Html5Qrcode("qrcode"); function onScanSuccess(qrCodeMessage) { alert("成功识别小二维码: " + qrCodeMessage); } function onScanFailure(error) { // 处理扫描失败的情况 } function onScanError(error) { // 处理扫描错误的情况 } qrcode.start({ facingMode: "environment" }, { qrbox: 250 }, onScanSuccess, onScanFailure, onScanError); </script> </body> </html> ``` 在上面的代码中,我们首先创建一个名为"qrcode"的div元素,用于显示二维码扫描结果。然后,我们创建一个Html5Qrcode对象,并将"qrcode"作为构造函数的参数传递进去。接下来,我们定义了三个回调函数,分别用于处理成功识别二维码的情况、扫描失败的情况和扫描错误的情况。 最后,我们调用Html5Qrcode对象的start方法,传入参数设置为使用后置摄像头进行扫描,并设置二维码框的大小为250。同时,我们将之前定义的三个回调函数作为参数传递给start方法。 这样,当用户打开该网页时,会自动启动摄像头,并进行二维码扫描。一旦成功识别到小二维码,就会触发onScanSuccess函数,并显示识别结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值