Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 321|回复: 0

[图像处理学习]java图片处理:文字水印

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-29 23:56:48 | 显示全部楼层 |阅读模式
    参考网上的资料 修正了里面的问题 增加了补白的功能 重构了代码
    import java.awt.AlphaComposite;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.geom.AffineTransform;
    import java.awt.image.AffineTransformOp;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    /**
    * @author Eric Xu
    *
    */
    public final class ImageUtils {
            /**
             * 图片水印
             * @param pressImg 水印图片
             * @param targetImg 目标图片
             * @param x 修正值 默认在中间
             * @param y 修正值 默认在中间
             * @param alpha 透明度
             */
            public final static void pressImage(String pressImg, String targetImg, int x, int y, float alpha) {
                    try {
                            File img = new File(targetImg);
                            Image src = ImageIO.read(img);
                            int wideth = src.getWidth(null);
                            int height = src.getHeight(null);
                            BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
                            Graphics2D g = image.createGraphics();
                            g.drawImage(src, 0, 0, wideth, height, null);
                            //水印文件
                            Image src_biao = ImageIO.read(new File(pressImg));
                            int wideth_biao = src_biao.getWidth(null);
                            int height_biao = src_biao.getHeight(null);
                            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
                            g.drawImage(src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wi
                 deth_biao, height_biao, null);
                            //水印文件结束
                            g.dispose();
                            ImageIO.write((BufferedImage) image, "jpg", img);
                    } catch (Exception e) {
                            e.printStackTrace();
                    }
            }
            /**
             * 文字水印
             * @param pressText 水印文字
             * @param targetImg 目标图片
             * @param fontName 字体名称
             * @param fontStyle 字体样式
             * @param color 字体颜色
             * @param fontSize 字体大小
             * @param x 修正值
             * @param y 修正值
             * @param alpha 透明度
             */
            public static void pressText(String pressText, String targetImg, String fontName, int fontStyle,
       Color color, int fontSize, int x, int y, float alpha) {
                    try {
                            File img = new File(targetImg);
                            Image src = ImageIO.read(img);
                            int width = src.getWidth(null);
                            int height = src.getHeight(null);
                            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                            Graphics2D g = image.createGraphics();
                            g.drawImage(src, 0, 0, width, height, null);
                            g.setColor(color);
                            g.setFont(new Font(fontName, fontStyle, fontSize));
                            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
                            g.drawString(pressText, (width - (getLength(pressText) * fontSize)) / 2 + x, (he
                 ight - fontSize) / 2 + y);
                            g.dispose();
                            ImageIO.write((BufferedImage) image, "jpg", img);
                    } catch (Exception e) {
                            e.printStackTrace();
                    }
            }
            /**
             * 缩放
             * @param filePath 图片路径
             * @param height 高度
             * @param width 宽度
             * @param bb 比例不对时是否需要补白
             */
            public static void resize(String filePath, int height, int width, boolean bb) {
                    try {
                            double ratio = 0.0; //缩放比例
                            File f = new File(filePath);
                            BufferedImage bi = ImageIO.read(f);
                            Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
                            //计算比例
                            if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
                                    if (bi.getHeight() > bi.getWidth()) {
                                            ratio = (new Integer(height)).doubleValue() / bi.getHeight();
                                    } else {
                                            ratio = (new Integer(width)).doubleValue() / bi.getWidth();
                                    }
                                    AffineTransformOp op = new
                    AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
                                    itemp = op.filter(bi, null);
                            }
                            if (bb) {
                                    BufferedImage image =
                     new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                                    Graphics2D g = image.createGraphics();
                                    g.setColor(Color.white);
                                    g.fillRect(0, 0, width, height);
                                    if (width == itemp.getWidth(null))
                                            g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
                       itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
                                    else
                    g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), i
                        temp.getHeight(null), Color.white, null);
                                    g.dispose();
                                    itemp = image;
                            }
                            ImageIO.write((BufferedImage) itemp, "jpg", f);
                    } catch (IOException e) {
                            e.printStackTrace();
                    }
            }
            public static void main(String[] args) throws IOException {
                    pressImage("G:\imgtest\sy.jpg", "G:\imgtest\test1.jpg", 0, 0, 0.5f);
                    pressText("我是文字水印", "G:\imgtest\test1.jpg", "黑体", 36, Color.white, 80, 0, 0, 0.3f);
                    resize("G:\imgtest\test1.jpg", 500, 500, true);
            }
            public static int getLength(String text) {
                    int length = 0;
                    for (int i = 0; i < text.length(); i++) {
                            if (new String(text.charAt(i) + "").getBytes().length > 1) {
                                    length += 2;
                            } else {
                                    length += 1;
                            }
                    }
                    return length / 2;
            }
    }
                            [/code] getLength 这个方法用来得到文字的长度 全角一个字 半角算半个字 但是感觉这种方法不太好 不知道有没有更好地方法~


       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/10/29/235648312.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 01:34 , Processed in 0.551812 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表