润乾报表自定义函数显示二维码

润乾报表V4.5里面直接支持条形码,如果要显示二维码,需要自定义函数来实现。

大体的步骤如下:

1、继承并实现对应的接口-详见代码

2、接口中,生成二维码图片,可调用zxing -详见代码

3、将生成的类文件拷贝到服务器的指定位置

拷贝到:web\WEB-INF\classes 下,注意包的路径要拷贝完整,不要只拷生成的class文件

4、配置注册自定义函数

在\web\WEB-INF\classes\config下配置文件:customFunctions.properties 

内容如下:QRCode=0,com.QRCodeFunc 

其中QRCode是自己定义的函数名称,在设计器表达式里面使用

com.QRCodeFunc  是类的全名称

5、在表达式里面调用方法


效果如下:



代码:

import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;


public final class MatrixToImageWriter {

  private static final int BLACK = 0xFF000000;
  private static final int WHITE = 0xFFFFFFFF;

  private MatrixToImageWriter() {}

  
  public static BufferedImage toBufferedImage(BitMatrix matrix) {
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < width; x++) {
      for (int y = 0; y < height; y++) {
        image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
      }
    }
    return image;
  }

  
  public static void writeToFile(BitMatrix matrix, String format, File file)
      throws IOException {
    BufferedImage image = toBufferedImage(matrix);
    if (!ImageIO.write(image, format, file)) {
      throw new IOException("Could not write an image of format " + format + " to " + file);
    }
  }

  
  public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
      throws IOException {
    BufferedImage image = toBufferedImage(matrix);
    if (!ImageIO.write(image, format, stream)) {
      throw new IOException("Could not write an image of format " + format);
    }
  }

}
public class QRCodeHelper {
	
	public static byte[] GetQRCode(String content,int size) throws Exception{
		
	     ByteArrayOutputStream bos = null;	    	     
		try {  
			MultiFormatWriter multiFormatWriter = new MultiFormatWriter();			 
			Map<EncodeHintType,String> hints = new HashMap<EncodeHintType,String>();		    
		    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");    
		    BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, size, size,hints);	    
		    bos = new ByteArrayOutputStream();	  
		    MatrixToImageWriter.writeToStream(bitMatrix, "jpg", bos);
		    return bos.toByteArray();
		 
		 } catch (Exception e) {			
		     throw e;		     
		 }finally{
			 if (bos != null){
				 try {
					bos.close();
				} catch (IOException e) {
					//无需处理
				}
			 }
		 }
	}
}

public abstract class BaseFunc extends Function{
	
	protected String tryGetParamValue(Context ctx, boolean isInput,int paramIndex){
		int paramCount = this.paramList.size();
		if( paramIndex <0 || paramIndex >= paramCount ){
			return null;
		}
		Object paramValue = this.paramList.get(paramIndex);
		if ( paramValue == null ){
			 return null; 
		}
	    Expression paramExpr=(Expression)paramValue;
	    paramValue = Variant2.getValue(paramExpr.calculate(ctx, isInput), false, isInput);
		if ( paramValue == null ){
			 return null; 
		}
		return paramValue.toString();
	}
	protected String getExprValue(Context ctx,boolean isInput,String exprStr){	
		if ( isNullOrEmpty(exprStr) ){
			 MessageManager mm = EngineMessage.get();
	    	 throw new ReportError(mm.getMessage("function.missingParam")); 
		}
	    Expression expr= new Expression(exprStr);
	    Object value = Variant2.getValue(expr.calculate(ctx, isInput), false, isInput);
		if ( value == null ){
			 return exprStr;
		}
		return value.toString();
	}
	protected String getParamValue(Context ctx, boolean isInput,int paramIndex){
		int paramCount = this.paramList.size();
		if( paramIndex <0 || paramIndex >= paramCount ){
			 MessageManager mm = EngineMessage.get();
	    	 throw new ReportError(mm.getMessage("function.invalidParam")); 
		}
		Object paramValue = this.paramList.get(paramIndex);
		if ( paramValue == null ){
			 MessageManager mm = EngineMessage.get();
	    	 throw new ReportError(mm.getMessage("function.missingParam")); 
		}
	    Expression paramExpr=(Expression)paramValue;    
	    paramValue = Variant2.getValue(paramExpr.calculate(ctx, isInput), false, isInput);
		if ( paramValue == null ){
			 MessageManager mm = EngineMessage.get();
	    	 throw new ReportError(mm.getMessage("function.invalidParam")); 
		}
		return paramValue.toString();	    
	}
	
	protected boolean isNullOrEmpty(Object obj){
		if ( obj == null ){
			return true;
		}
		String str = obj.toString().trim();
		if (str.length() == 0 ){
			return true;
		}
		return false;
	}
}
public class QRCodeFunc  extends BaseFunc
{

	@Override
	public Object calculate(Context ctx, boolean isInput) {
		String content = getParamValue(ctx,isInput,0);
		String sizeStr = tryGetParamValue(ctx,isInput,1);
		if ( isNullOrEmpty(sizeStr)){
			sizeStr = "200";
		}
		int size = Integer.parseInt(sizeStr.trim());
		try {
			return QRCodeHelper.GetQRCode(content, size);
		} catch (Exception e) {
			throw new ReportError(e.getMessage()); 
		}		
	}
	
}




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值