是否可以用 Jasper 制作圆形图像边框?

Is it possible to make rounded image border with Jasper?

是否可以用jasper应用圆角效果?

我搜索了很多,但在任何地方都找不到如何在 Jasper Report 中简单地为图像添加边框半径。

AFIK Jasper Report (jrxml) 中 no 设置允许您直接向图像添加圆角边框,但是

您可以添加一个 java class 来详细说明图像,然后再将其添加到报告中

作为示例,我将在我称为 ImageUtil

的 class 中添加 Philipp Reichart's metodo from this answer
package it.jdd;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;

public class ImageUtil {
  
  private ImageUtil() {
      super();
  }
  
  public static BufferedImage makeRoundedCorner(BufferedImage image, int cornerRadius) {
      int w = image.getWidth();
      int h = image.getHeight();
      BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

      Graphics2D g2 = output.createGraphics();
      
      // This is what we want, but it only does hard-clipping, i.e. aliasing
      // g2.setClip(new RoundRectangle2D ...)

      // so instead fake soft-clipping by first drawing the desired clip shape
      // in fully opaque white with antialiasing enabled...
      g2.setComposite(AlphaComposite.Src);
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g2.setColor(Color.WHITE);
      g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius));
      
      // ... then compositing the image on top,
      // using the white shape from above as alpha source
      g2.setComposite(AlphaComposite.SrcIn);
      g2.drawImage(image, 0, 0, null);        
      g2.dispose();    
      return output;
  }
}

引用整个 classes 以提高可读性,因为该方法取自 Philipp 的精彩回答

有了 class 中的这个 class 路径,您现在可以对图像应用圆角边框

jrxml 示例

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="ImageRounded" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ea1bfa6c-be34-45af-b1cf-1bf35e3f359c">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="200" splitType="Stretch">
            <image>
                <reportElement x="20" y="20" width="200" height="153" uuid="ae9f7e0a-84cb-4e3e-8183-5bd00017a4f0"/>
                <imageExpression><![CDATA[it.jdd.ImageUtil.makeRoundedCorner(javax.imageio.ImageIO.read(new java.io.File("C:/testImage.png")),50)]]></imageExpression>
            </image>
        </band>
    </title>
</jasperReport>

输出