润乾报表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());
}
}
}