圖片自動生成子圖並添加圖片水印

//定義好水印 Logo 和水印顏色

private static Boolean isLogoImage=false;

private static String logoText="www.grainger.cn";

private static Color color=new Color(220,220,220);

main方法開始

 public static void main(String[] args) throws Exception{
 
        String imgFile="D://test/001.jpg";
        
        InputStream in = null;
        
        byte[] data = null;
        
        // 讀取圖片字節數組
        try {
            in = new FileInputStream(imgFile);
            
            data = new byte[in.available()];
            
            in.read(data);
            
            in.close();
            
        } catch (IOException e) {
        
            e.printStackTrace();
            
        }

        String originalFilename=StringUtils.substringAfterLast(imgFile,"/");
        
        store(originalFilename,data);

    }

store方法將圖片文件轉化的byte[]數組轉化爲inputStream流和文件名傳入storeimpl(有點雞肋,當時這樣顯得邏輯清晰一些)

 public static String store(String originalFilename, byte[] contents) throws Exception {
 
        InputStream inputStream = new ByteArrayInputStream(contents);
        
        return storeImpl(originalFilename, inputStream, false);
    }

storeimpl定義水印和子圖生成的新地址與子圖的規格

private static String storeImpl(String originalFilename, InputStream inputStream, Boolean isTemp) throws Exception {

    String newPath = "D://test/product/" + StringUtils.substringBefore(originalFilename, ".");

    int[] newWidth = null;

    BufferedImage bufferedImage = ImageIO.read(inputStream);
    
    if (null == bufferedImage) {
    
        throw new BizException("invalid file name");
    }
    if(isLogoImage) {
        newWidth=new int[3];
        newWidth[0]=80;
        newWidth[1]=160;
        newWidth[2]=350;
    }
    else {
        newWidth=new int[4];
        newWidth[0]=115;
        newWidth[1]=350;
        newWidth[2]=800;
        newWidth[3]=1200;
    }
    
    for (int i = 0; i < newWidth.length; i++) {
    
        int originalWidth = bufferedImage.getWidth();
        
        int originalHeight = bufferedImage.getHeight();
        
        //子圖生成
        if(bufferedImage.getWidth()>bufferedImage.getHeight()) {
        
            double scale = (double) originalWidth / (double) newWidth[i];    // 縮放的比例
            
            int newHeight = (int) (originalHeight / scale);
            
            zoomImageUtils(originalFilename, newPath, bufferedImage, newWidth[i], newHeight);
        }
        else{
        
            double scale = (double) originalHeight / (double) newWidth[i];    // 縮放的比例
            
            int newHeight=(int) (originalWidth/scale);
            
            zoomImageUtils(originalFilename, newPath, bufferedImage,newHeight, newWidth[i]);
        }
    }
    return null;
}

//生產指定size子圖

 private static void zoomImageUtils(String originalFilename, String newPath, BufferedImage bufferedImage, int width, int height) throws IOException {
 
        String suffix = StringUtils.substringAfterLast(originalFilename, ".");

        // 處理 png 背景變黑的問題
        if (suffix != null && (suffix.trim().toLowerCase().endsWith("png") || suffix.trim().toLowerCase().endsWith("gif"))) {
        
            BufferedImage to = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            
            Graphics2D g2d = to.createGraphics();
            
            to = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
            
            g2d.dispose();
            
            g2d = to.createGraphics();
            
            Image from = bufferedImage.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
            
            g2d.drawImage(from, 0, 0, null);

            //加水印
            if (maxValue(width,height) > 799) {
            
                g2d.setColor(new Color(220, 220, 220));
                
                g2d.setFont(new java.awt.Font("微軟雅黑", java.awt.Font.BOLD, (width+height) / 20));
                
                g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.55f));
                
                g2d.rotate(Math.toRadians(45),width/5,height/5);
                
                g2d.drawString(logoText, width/5, height /5);
                
            }
            g2d.dispose();
            
            newPath = selectNewPath(width, newPath, suffix);
            
            ImageIO.write(to, suffix, new File(newPath));
            
        } else {
        
            BufferedImage newImage = new BufferedImage(width, height, bufferedImage.getType());
            
            Graphics g = newImage.getGraphics();
            
            g.drawImage(bufferedImage, 0, 0, width, height, null);

            //加水印
            if (maxValue(width,height) > 799) {
            
                g.setColor(new Color(220, 220, 220));
                
                g.setFont(new java.awt.Font("微軟雅黑", java.awt.Font.BOLD, (width+height) / 20));
                
                ((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.55f));
                
                ((Graphics2D) g).rotate(Math.toRadians(45),width/5,height/5);
                
                g.drawString(logoText, width /5, height /5);
                
            }
            g.dispose();
            
            width=maxValue(width,height);
            
            newPath = selectNewPath(width, newPath, suffix);
            
            ImageIO.write(newImage, suffix, new File(newPath));
            
            System.out.println("完成!");
        }
    }

// 獲取寬高最大值

private static int maxValue(int width, int height) {
    return width>height?width:height;
}

//生成子圖名稱後綴

private static String selectNewPath(Integer width, String newPath, String suffix) {

    if (width.equals(80)) {
        newPath += "_s80.";
        newPath += suffix;
    }else if (width.equals(115)) {
        newPath += "_s115.";
        newPath += suffix;
    } else if (width.equals(160)) {
        newPath += "_s160.";
        newPath += suffix;
    } else if (width.equals(350)) {
        newPath += "_s350.";
        newPath += suffix;
    }else if (width.equals(800)) {
        newPath += "_s800.";
        newPath += suffix;
    } else {
        newPath += "_s1200.";
        newPath += suffix;
    }
    return newPath;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章