xieegai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
package img;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;

public class Mark {
    
    public static void main(String[] args) throws IOException {
        // 输入输出文件路径/文件
        String src = "D:\\test\\map.jpg";
        String dest = "D:\\test\\map1.jpg";
        File srcFile = new File(src);
        File destFile = new File(dest);
        // 将输入文件转换为字节数组
        byte[] bytes = getByte(srcFile);
        // 生成水印(Base64)
        String mark = createMark();
        // 打水印
        byte[] result = addImageMark(bytes, "jpg", mark);
        FileOutputStream fos = new FileOutputStream(destFile);
        fos.write(result);
        fos.close();
    }
    
    /**
     * 生成水印(Base64)
     */
    public static String createMark() throws IOException{
        // 水印文件路径
        String mark = "D:\\test\\time.jpg";
        File markFile = new File(mark);
        // 获取水印图片二进制文件
        byte[] bytes = getByte(markFile);
        // 获取水印图片Base64文件
        String base64String = Base64.encodeBase64String(bytes);
        System.out.println(base64String);
        return base64String;
    }
    
    /**
     * 将File文件转为字节数组
     */
    public static byte[] getByte(File file){
        byte[] bytes = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            bytes = new byte[fis.available()];
            fis.read(bytes);
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }
    
    /**
     * 水印之间的间隔
     */
    private static final int INTERVAL = 150;
    private static final float ALPHA = 0.3F;
    private static final float MULTIPLE = 1.5F;
    
    /**
     * 给图片加水印
     * @param content          图片内容
     * @param suffix           图片后缀
     * @param watermarkImage   Base64格式的水印
     * @return
     */
    public static byte[] addImageMark(byte[] bytes, String suffix, String watermarkImage) {
        if (bytes != null && bytes.length > 0) {
            BufferedImage bufferedImage = null;
            ByteArrayOutputStream byteArrayOutputStream = null;
            try {
                ByteArrayInputStream in = new ByteArrayInputStream(bytes);
                Image image2 = ImageIO.read(in);

                int width = image2.getWidth(null);
                int height = image2.getHeight(null);

                bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

                Graphics2D g = bufferedImage.createGraphics();
                g.drawImage(image2, 0, 0, width, height, null);

                byte[] watermarkArray = Base64.decodeBase64(watermarkImage);
                ByteArrayInputStream watermarkIn = new ByteArrayInputStream(watermarkArray);
                Image logoImage = ImageIO.read(watermarkIn);

                int width1 = logoImage.getWidth(null);
                int height1 = logoImage.getHeight(null);

                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
                g.rotate(Math.toRadians(30), bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2);

                int x = -width / 2;
                int y;
                while (x < width * MULTIPLE) {
                    y = -height / 2;
                    while (y < height * MULTIPLE) {
                        g.drawImage(logoImage, x, y, null);
                        y += height1 + INTERVAL;
                    }
                    x += width1 + INTERVAL;
                }
                
                g.dispose();
                byteArrayOutputStream = new ByteArrayOutputStream();
                ImageIO.write(bufferedImage, suffix, byteArrayOutputStream);
                return byteArrayOutputStream.toByteArray();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bufferedImage != null) {
                    bufferedImage.flush();
                    bufferedImage = null; // null是告诉jvm此资源可以回收
                }
                IOUtils.closeQuietly(byteArrayOutputStream);
                System.gc(); // 让系统回收资源,但不一定是回收刚才设成null的资源,也可能是回收其他没用的资源
            }
        }
        return bytes;
    }
}

 

posted on 2018-01-18 21:11  xieegai  阅读(222)  评论(0编辑  收藏  举报