欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

图片自动生成子图并添加图片水印

程序员文章站 2024-01-19 13:43:16
...

//定义好水印 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;
}