java实现图片上添加水印

图片上添加水印

 /**
    * 图片添加水印
    * @author zhangpu
    * @date 2021/3/29
    * @param response
    * @param startTime 水印开始时间
    * @param endTime   水印结束时间
    * @param filePath  文件路径
    * @return void
    */
    public void drawTextInImg(HttpServletResponse response, String startTime, String endTime,String filePath) {
        ImageIcon imgIcon = new ImageIcon(filePath);
        Image img = imgIcon.getImage();
        int width = img.getWidth(null);
        int height = img.getHeight(null);
        BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bimage.createGraphics();
        g.setColor(getColor("#AF2222"));
        g.setBackground(Color.white);
        g.drawImage(img, 0, 0, null);
        //设置字体样式,风格,大小
        Font font = new Font("Arial", Font.PLAIN, 27);
        g.setFont(font);
        //以左上角为坐标轴原点,left为横坐标,top为纵坐标
        int left = 330;
        int top = 1079;
        //抗锯齿
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //绘制开始时间
        g.drawString(startTime, left, top);
        //绘制结束时间
        g.drawString(endTime, left, top+65);
        //释放对象
        g.dispose();
        try {
            //BufferedImage 转 InputStream
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ImageOutputStream imageOutput = ImageIO.createImageOutputStream(byteArrayOutputStream);
            ImageIO.write(bimage, "png", imageOutput);
            InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            long length = imageOutput.length();
            //设置response
            response.setContentType("image/png");
            response.setContentLength((int)length);
            response.setHeader("Content-Disposition","attachment;filename="+new String("license.png".getBytes("gbk"),"iso-8859-1"));
            //输出流
            byte[] bytes = new byte[1024];
            OutputStream outputStream = response.getOutputStream();
            long count = 0;
            while(count < length){
                int len = inputStream.read(bytes, 0, 1024);
                count +=len;
                outputStream.write(bytes, 0, len);
            }
            outputStream.flush();
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    public static Color getColor(String color) {
        if (color.charAt(0) == '#') {
            color = color.substring(1);
        }
        if (color.length() != 6) {
            return null;
        }
        try {
            int r = Integer.parseInt(color.substring(0, 2), 16);
            int g = Integer.parseInt(color.substring(2, 4), 16);
            int b = Integer.parseInt(color.substring(4), 16);
            return new Color(r, g, b);
        } catch (NumberFormatException nfe) {
            return null;
        }
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值