ureport2插入图片,重写图片参数

项目场景:

ureport2直接插入图片时某些参数有可能会出现问题,或者图片请求方式不同导致无法插入的问题。


解决方案:

        在ureport2中,实现ImageProvider, ApplicationContextAware接口,并且重写getImage方法,进行参数图片重写的操作。

        getImage返回的是图片的inputStream的数据流的形式,getImage方法的参数是图片的路径,并且需要对support方法进行处理,即设置为return true,如代码所示,即ureport2中设置的图片路径,如图2。

package com.newcapec.sd.report.report.impl;

import com.bstek.ureport.exception.ReportComputeException;
import com.bstek.ureport.provider.image.ImageProvider;
import com.newcapec.sd.report.util.UrlUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;
import org.springframework.web.context.WebApplicationContext;
import sun.misc.BASE64Decoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

@Component
public class ReportImageProviderImpl implements ImageProvider, ApplicationContextAware {
    private ApplicationContext applicationContext;
    private String baseWebPath;
    @Override
    public InputStream getImage(String path) {
        try {
            if(path.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX) || path.startsWith("/WEB-INF")){
                return applicationContext.getResource(path).getInputStream();
            }else if(path.startsWith("data:image/png;base64,")){
                path = path.substring(path.indexOf(",", 1) + 1, path.length());

                Integer width = 120;
                Integer height = 30;

                if(path.contains("width") && path.contains("height")){
                    width = Integer.valueOf(path.substring(path.lastIndexOf(",width=") + 7,path.lastIndexOf("&height=")));
                    height = Integer.valueOf(path.substring(path.lastIndexOf("=") + 1,path.length()));
                    path = path.substring(0, path.lastIndexOf(",width="));
                }

                BASE64Decoder decoder = new BASE64Decoder();
                // Base64解码
                byte[] byteArr = decoder.decodeBuffer(path);
                InputStream inputStream = new ByteArrayInputStream(byteArr);

                BufferedImage bufferedImage = ImageIO.read(inputStream);
                Image tmp = bufferedImage.getScaledInstance(width,height,Image.SCALE_SMOOTH);
                BufferedImage dimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                Graphics2D g2d = dimg.createGraphics();
                g2d.drawImage(tmp, 0, 0, null);
                g2d.dispose();

                ByteArrayOutputStream os = new ByteArrayOutputStream();
                InputStream input = null;
                try {
                    ImageIO.write(dimg, "png", os);
                    input = new ByteArrayInputStream(os.toByteArray());
                    return input;
                } catch (IOException e) {
                }
                return input;
            }else if(path.startsWith("http://")||path.startsWith("https://")) {
                URL url = new URL(path);
                //打开链接
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                //设置请求方式为"GET"
                conn.setRequestMethod("GET");
                //超时响应时间为5秒
                conn.setConnectTimeout(5 * 1000);
                //通过输入流获取图片数据
                InputStream inStream = conn.getInputStream();
                return inStream;
            }else if(path.startsWith("src:")) {
                String realpath=path.substring(4);

                URL url = new URL(UrlUtils.getIpandport()+realpath);
                //赣西内网地址 - 服务器无法访问外网
//                URL url = new URL("http://192.168.5.6:18080" + File.separator + realpath);
                //打开链接
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                //设置请求方式为"GET"
                conn.setRequestMethod("GET");
                //超时响应时间为5秒
                conn.setConnectTimeout(5 * 1000);
                //通过输入流获取图片数据
                InputStream inStream = conn.getInputStream();

                if(path.contains("width") && path.contains("height")){
                    String width = path.substring(path.lastIndexOf("&width=") + 7,path.lastIndexOf("&height="));
                    String height = path.substring(path.lastIndexOf("=") + 1,path.length());
                    BufferedImage bufferedImage = ImageIO.read(inStream);
                    if(null == bufferedImage){
                        throw new ReportComputeException(new NullPointerException());
                    }
                    Image tmp = bufferedImage.getScaledInstance(Integer.valueOf(width),Integer.valueOf(height),Image.SCALE_SMOOTH);
                    BufferedImage dimg = new BufferedImage(Integer.valueOf(width),Integer.valueOf(height), BufferedImage.TYPE_INT_ARGB);
                    Graphics2D g2d = dimg.createGraphics();
                    g2d.drawImage(tmp, 0, 0, null);
                    g2d.dispose();

                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    InputStream input = null;
                    try {
                        ImageIO.write(dimg, "png", os);
                        input = new ByteArrayInputStream(os.toByteArray());
                        return input;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                return inStream;
            }else{
                path=baseWebPath+path;
                return new FileInputStream(path);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new ReportComputeException(e);
        }
    }
    @Override
    public boolean support(String path) {
        if(path.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)){
            return true;
        }else if(baseWebPath!=null && (path.startsWith("/") || path.startsWith("/WEB-INF"))){
            return true;
        }else if(path.startsWith("data:image/png;base64,")){
            return true;
        }else if(path.startsWith("http://")||path.startsWith("https://")){
            return true;
        }else if(path.startsWith("src:")){
            return true;
        }
        return false;
    }

        

 

例如:新建一个 Message 对象,并将读取到的数据存入 Message,然后 mHandler.obtainMessage(READ_DATA, bytes, -1, buffer).sendToTarget();换成 mHandler.sendMessage()

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值