zoukankan      html  css  js  c++  java
  • Java 图片加文字水印和图片水印

      1:将字节数组转换为本地图片

     /**
             * 将字节数组转换为本地图片
             * @param data
             * @param path
             */
        public static void byte2image(byte[] data, String path) {
                if (data.length < 3 || path.equals("")){
                    return;
                }
                try {
                    FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
                    imageOutput.write(data, 0, data.length);
                    imageOutput.close();
                    System.out.println("生成图片成功:" + path);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
        }

    2:给图片加图片水印

    /**
     * 为背景图片增加水印图片
     *
     * @param path          水印图片,本地地址,未做网络图片转换
     * @param bgPath        背景图片,同上
     * @param x              横坐标,左上角0,往右递增
     * @param y              纵坐标,左上角0,往下递增
     * @param width          水印图片宽度
     * @param height         水印图片高度
     * @return
     */
    public static byte[] generateImage(String path,String bgPath , int x, int y, int width, int height) {
        try {
            BufferedImage watermarkImage = ImageIO.read(new File(path));
            BufferedImage bgImage = ImageIO.read(new File(bgPath));
            int bgWidth = bgImage.getWidth(null);
            int bgHeight = bgImage.getHeight(null);
            BufferedImage image = new BufferedImage(bgWidth, bgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();
            g.drawImage(bgImage, 0, 0, bgWidth, bgHeight, null);
            width = width == 0 ? watermarkImage.getWidth() : width;
            height = height == 0 ? watermarkImage.getHeight() : height;
            g.drawImage(watermarkImage, x, y, width, height, null);
            g.dispose();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", out);
            return out.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

     测试

    public static void main(String[] args) {
        String path = "D:/image/src/main/picture/1218395382.jpg";
        String waterPath = "D:/image/src/main/picture/sijiali_s1.jpg";
        String imgResult = "D:/image/src/mina/picture/水印.jpg";
        byte[] bytes = generateImage(waterPath,path, 250, 10,  80, 100);
        byte2image(bytes,imgResult);
    }

     效果:

    原图

    水印图片

    结果图片

    3:给图片加文字水印

    /**
     * 添加文字水印的方法
     * @param pressText 要添加的文字
     * @param path 文件路径,这里只处理了了本地文件,未处理网络图片
     * @param x 文字在图片的横坐标,左上角为0,往右递增
     * @param y 文字在图片的纵坐标,左上角为0,往下递增
     * @param fontName 文字的字体名称
     * @param fontSize 文字的大小
     * @param fontColor 文字的眼神
     * @param style 文字的格式,如Font.BOLD++Font.ITALIC表示粗体斜体
     * @return
     */
    private static byte[] generateText(String pressText, String path, int x, int y, String fontName,
                             int fontSize, Color fontColor, int style){
        try {
            BufferedImage bgImage= ImageIO.read(new File(path));
            int wideth = bgImage.getWidth(null);
            int height = bgImage.getHeight(null);
            BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();
            g.drawImage(bgImage, 0, 0, wideth, height, null);
    
            //设置字体大小、颜色等
            g.setColor(fontColor);
            g.setFont(new Font(fontName, style, fontSize));
    
            g.drawString(pressText, x, y);
            g.dispose();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", out);
            return out.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

     测试

    public static void main(String[] args) {
        String path = "D:/image/src/main/picture/1218395382.jpg";
        String path2 = "D:/image/src/main/picture/文字水印.jpg";
        //Font.BOLD+Font.ITALIC表示加粗且斜体
        byte[] bytes = generateText("斯嘉丽", path, 210, 50, "微软雅黑", 30, Color.RED, Font.BOLD+Font.ITALIC);
        byte2image(bytes,path2);
    }

    结果:

    原图

     结果图片

    至此,图片水印,文字水印完成了!

  • 相关阅读:
    linux系统编程程序员必备
    postgresql查询优化之GIN(Generalized Inverted Index)索引与全文检索
    postgresql并行执行
    lightdb for postgresql日志详解
    PostgreSQL 使用数组类型
    postgresql wal文件误删恢复
    汉语词性标注集
    postgresql----JSON类型、函数及全文检索的支持
    postgresql等待事件之wait_event为空解析
    lightdb for pg查看锁
  • 原文地址:https://www.cnblogs.com/zhaosq/p/13897067.html
Copyright © 2011-2022 走看看