继续潜水

导航

 

底层工具类,参数为文件流,返回也是文件流,具体是上传云还是下载这里就不写了,主要是图片合成

业务是一张底图,一张或者多张子图片,更具子图的坐标x和坐标y生成图片

PictureSynthesisSonFo 对象为子图对象
PictureSynthesisFo 对象为底图对象并包含子图对象集合
public static InputStream overlapImage(PictureSynthesisFo pictureSynthesisFo){
        InputStream is;
        try {
            //设置图片大小
            //背景图片
            BufferedImage background = ImageIO.read(pictureSynthesisFo.getInputStream());
            Graphics2D g = background.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1));
            for (PictureSynthesisSonFo sonFiles : pictureSynthesisFo.getSonFiles()  ) {
                BufferedImage qrCode = ImageIO.read(sonFiles.getInputStream());
                log.info("合成签字图fileCode:{},原width:{},width:{},原height:{},height:{}",sonFiles.getFileCode(),
                        qrCode.getWidth(),sonFiles.getWidth(),qrCode.getHeight(), sonFiles.getHeight());
                //在背景图片上添加图片
                g.drawImage(qrCode .getScaledInstance(sonFiles.getWidth(), sonFiles.getHeight(), Image.SCALE_SMOOTH),  sonFiles.getX(), sonFiles.getY(), null);
            }
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            g.dispose();
            ByteArrayOutputStream bs =new ByteArrayOutputStream();
            is = new ByteArrayInputStream(bs.toByteArray());
            return  is ;
        }catch (Exception e){
            LogUtil.error(log,"图片转换错误:{0}",e);
        }
        return null;
    }

 

posted on 2020-08-24 14:26  继续潜水  阅读(666)  评论(0编辑  收藏  举报