为了账号安全,请及时绑定邮箱和手机立即绑定

Java实现图片水印

难度初级
时长 1小时15分
学习人数
综合评分9.80
41人评价 查看评价
10.0 内容实用
9.6 简洁易懂
9.8 逻辑清晰
  • 测试一下
    查看全部
    0 采集 收起 来源:引言

    2016-01-13

  • 不公开,10秒
    查看全部
    0 采集 收起 来源:引言

    2016-01-13

  • 添加水印图片的思路
    查看全部
    0 采集 收起 来源:水印编写准备

    2016-01-13

  • struts.xml 配置action
    查看全部
  • 在struts.xml中添加新的action配置
    查看全部
  • struts.xml 配置文件
    查看全部
  • struts2框架整合 web文件
    查看全部
  • jar
    查看全部
  • java图片水印实用工具类
    查看全部
  • 搭建框架
    查看全部
  • 项目案例演示
    查看全部
  • 项目结构。。。。。

    查看全部
  • 书写代码思路

    https://img1.sycdn.imooc.com//5bbbdcc000019d1313860776.jpg

    查看全部
  • 课程目标:

    1. 了解Java图片水印实现思路

    2. 掌握文字水印和图片水印的实现

    3. 掌握多图片批量水印的实现


    查看全部
    0 采集 收起 来源:课程目标

    2018-10-05

  • while循环判断条件中的width和height应该是图片的宽和高,老师误写了

    具体代码如下:

    public String waterMark(File image, String imageFileName,
                String uploadPath, String realUploadPath) {
            String logoFileName = "logo_" + imageFileName;
            OutputStream os = null;
            try {
                
                Image img = ImageIO.read(image);
                int imgWidth = img.getWidth(null);
                int imgHeight = img.getHeight(null);
                
                BufferedImage bufferedImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
                Graphics2D g = bufferedImage.createGraphics();
                g.drawImage(img, 0, 0, imgWidth, imgHeight, null);
                
                g.setFont(new Font(FONT_NAME,FONT_STYLE,FONT_SIZE));
                g.setColor(FONT_COLOR);
                int width = FONT_SIZE*getTextLength(MARK_TEXT);
                int height = FONT_SIZE;
                
                //设置透明度
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,ALPH));
                
                g.rotate(Math.toRadians(-30),bufferedImage.getWidth()/2,bufferedImage.getHeight()/2);
                
                int x = -width/2;
                int y = -height/2;
                
                while(x<imgWidth*1.5){
                    y = -height/2;
                    while(y<imgHeight*1.5){
                        g.drawString(MARK_TEXT, x, y);
                        y += height + 70;
                    }
                    x += width+70;
                }
                g.dispose();
                os = new FileOutputStream(realUploadPath+"/"+logoFileName);
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
                encoder.encode(bufferedImage);
                
            } catch (Exception e) {
                e.printStackTrace();
            } finally{
                try {
                    if(os!=null)
                        os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
            }
            return uploadPath+"/"+logoFileName;
        }
        
        public int getTextLength(String text){
            int length = text.length();
            for (int i = 0; i < text.length(); i++) {
                String s = String.valueOf(text.charAt(i));
                if(s.getBytes().length>1){
                    length++;
                }
            }
            length = length%2==0?length/2:length/2+1;
            return length;
        }

    查看全部

举报

0/150
提交
取消
课程须知
本课程是java web的中高级课程,建议各位小伙伴们先对 Jsp、以及Struts2有初步认识,再来进修。
老师告诉你能学到什么?
1、Java图片水印实现原理 2、Java实现图片添加文字水印 3、Java实现图片添加图片水印 4、Java实现多图片水印的添加

微信扫码,参与3人拼团

意见反馈 帮助中心 APP下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!