美文网首页
添加水印和缩放图片

添加水印和缩放图片

作者: TinyThing | 来源:发表于2022-01-10 16:57 被阅读0次
        //读取图片
        InputStream inputStream = file.getInputStream();
        BufferedImage image = ImgUtil.read(inputStream);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        //压缩图片,将大于1080p的图片压缩到1080p
        int w = image.getWidth();
        image = image.getWidth() > 1080 ? toBufferedImage(scale(image, 1080f / w)) : image;

        //添加水印
        String datetime = LocalDateTime.now().format(DATETIME_FORMAT);
        int x = 0;
        int y = image.getHeight() * 9 / 20;
        ImgUtil.pressText(image, outputStream, datetime, LIGHT_GRAY, new Font(MONOSPACED, BOLD, 30), x, y, .7f);
        byte[] bytes = outputStream.toByteArray();

相关文章

网友评论

      本文标题:添加水印和缩放图片

      本文链接:https://www.haomeiwen.com/subject/xuomcrtx.html