1. 程式人生 > 實用技巧 >java 圖片合成(子圖已經優化)

java 圖片合成(子圖已經優化)

底層工具類,引數為檔案流,返回也是檔案流,具體是上傳雲還是下載這裡就不寫了,主要是圖片合成

業務是一張底圖,一張或者多張子圖片,更具子圖的座標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; }