可以用Jasper制作圆形的图像边框吗? [英] Is it possible to make rounded image border with Jasper?

查看:46
本文介绍了可以用Jasper制作圆形的图像边框吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在碧玉上应用圆角图像效果?

我进行了很多搜索,但是在Jasper Report中找不到如何在图像中简单添加边框半径的方法.

解决方案

AFIK在Jasper Report(jrxml)中没有设置,该设置可让您将圆角边框直接添加到图像但是

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

例如,我将从此

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.

解决方案

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

这篇关于可以用Jasper制作圆形的图像边框吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆