java 我如何/可以在 Jasper 报告模板中使用 base64 作为图像源?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33571520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:51:49  来源:igfitidea点击:

How/Can I use base64 as image source in a Jasper Report template?

javaimagejasper-reportsbase64

提问by iCodeLikeImDrunk

So in my jrxml file I have the following:

所以在我的 jrxml 文件中,我有以下内容:

<parameter name="smileyfaceimage" class="java.lang.String"/>

Then I reference it in:

然后我引用它:

<image scaleImage="FillFrame" onErrorType="Blank">
    <reportElement x="167" y="62" width="363" height="171" backcolor="#333333"/>
    <imageExpression class="java.lang.String"><![CDATA[$P{smileyfaceimage}]]></imageExpression>
</image>

Is this not correct?

这不正确吗?

I've tried the base64 both with and without:

我已经尝试过使用和不使用 base64:

data:image/png;base64,

Here's the image im working with

这是我正在使用的图像

just a random screenshot

只是随机截图

Then I used https://www.base64-image.de/or any random site to get the base64 string. I tested the string it produces and it's valid.

然后我使用https://www.base64-image.de/或任何随机站点来获取 base64 字符串。我测试了它产生的字符串,它是有效的。

Now in my code;

现在在我的代码中;

  • set the value of a variable to the based64 string
  • on the template
    • set the parameter: <parameter name="smileyfaceimage" class="java.lang.String"/>
  • then add the image data to the page:

    • <image scaleImage="FillFrame" onErrorType="Blank">
          <reportElement x="167" y="62" width="363" height="171" backcolor="#333333"/>
          <imageExpression><![CDATA[new java.io.ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64($P{smileyfaceimage}.getBytes()))]]></imageExpression>
      </image>
      
  • 将变量的值设置为 based64 字符串
  • 在模板上
    • 设置参数: <parameter name="smileyfaceimage" class="java.lang.String"/>
  • 然后将图像数据添加到页面:

    • <image scaleImage="FillFrame" onErrorType="Blank">
          <reportElement x="167" y="62" width="363" height="171" backcolor="#333333"/>
          <imageExpression><![CDATA[new java.io.ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64($P{smileyfaceimage}.getBytes()))]]></imageExpression>
      </image>
      

Am I missing a step?

我错过了一步吗?

回答by Petter Friberg

Passing parameter as Stringmakes jasper report believe its a absolute file path, so you need another class. The most obvious would be java.awt.Imageor java.io.InputStream.

传递参数 asString使 jasper report 相信它是绝对文件路径,因此您需要另一个类。最明显的是java.awt.Imageor java.io.InputStream

I choose java.io.InputStreamsince this will require less code, so the first thing we need to do now is to decodethe base64image String.

我选择java.io.InputStream,因为这将需要更少的代码,所以我们现在需要做的第一件事就是到decodebase64图像String

There are severalBase64 class that will do the job, I choose the org.apache.commons.codec.binary.Base64since apache commons-codec.jaris already distributed with jasper report (dependencies). The decode will give us a byte array byte[], so now we need only to add a ByteArrayInputStream

几个Base64 类可以完成这项工作,我选择了org.apache.commons.codec.binary.Base64因为 apachecommons-codec.jar已经与 jasper 报告(依赖项)一起分发。解码会给我们一个字节数组byte[],所以现在我们只需要添加一个ByteArrayInputStream

The java code would be:

Java代码将是:

InputStream stream = new ByteArrayInputStream(Base64.decodeBase64(smileyfaceimage.getBytes()));

Time to pass it into the jasper report imageExpression

是时候将它传递到 jasper 报告中了 imageExpression

<image scaleImage="FillFrame" onErrorType="Blank">
<reportElement x="167" y="62" width="363" height="171" backcolor="#333333"/>
    <imageExpression class="java.io.InputStream"><![CDATA[new java.io.ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64($P{smileyfaceimage}.getBytes()))]]></imageExpression>
</image>

Hope for the best and press the preview:

希望最好,然后按预览

Result

结果

Important notice: The smileyfaceimageneeds to be without:data:image/png;base64,

重要通知smileyfaceimage需要没有data:image/png;base64,

EDIT: The problem of the OP (comments) was that with old jasper report lib (3.0) you need to specify the class in the imageExpression@see class="java.io.InputStream"the post has been update consequently since this works also in 6.0.

编辑:OP(评论)的问题是,对于旧的 jasper 报告库(3.0),您需要在imageExpression@see 中指定类,因此class="java.io.InputStream"该帖子已更新,因为这也适用于 6.0。

回答by Durandal

You need to decode the image somehow, e.g. use an imageExpression:

您需要以某种方式解码图像,例如使用 imageExpression:

<image scaleImage="RetainShape" hAlign="Center" vAlign="Bottom" isUsingCache="false">
  <reportElement uuid="53a340b3-7d64-4104-9e9f-0f603059579a" key="Logo_Footer" x="55" y="760" width="370" height="42"/>
    <imageExpression><![CDATA[new java.io.StringBufferInputStream(new org.w3c.tools.codec.Base64Decoder(" Base 64 String Data ").processString())]]>
  </imageExpression>
</image>

I'm using this to embed images, but it should work with a variable, field or parameter too.

我正在使用它来嵌入图像,但它也应该与变量、字段或参数一起使用。

回答by Rok T.

Java 8+ without external libraries:

没有外部库的 Java 8+:

<imageExpression><![CDATA[javax.imageio.ImageIO.read(new java.io.ByteArrayInputStream(Base64.getDecoder().decode($P{barcodeHeader})))]]></imageExpression>

If that doesn't work, this should definitely:

如果这不起作用,这绝对应该:

<imageExpression><![CDATA[javax.imageio.ImageIO.read(new java.io.ByteArrayInputStream(java.util.Base64.getDecoder().decode($P{barcodeHeader})))]]></imageExpression>