1

Is possible to apply the effect of rounded image with jasper?

I have searched a lot but I do not find in anywhere how to simply add border-radius to a image in Jasper Report.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Higor Tavares
  • 67
  • 1
  • 2
  • 11

1 Answers1

3

AFIK there is no setting in Jasper Report (jrxml) that allows you to add rounded borders directly to your image, but

You can add a java class that elaborates the image before adding it to the report

As example I will add Philipp Reichart's metodo from this answer in a class that I called ImageUtil

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;
  }
}

Quoting whole classes for readability, since the method is taken from Philipp's great answer

With this class in classpath you can now apply rounded borders to your image

jrxml example

<?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>

Output

Example output

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109