微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JAVA 通过URL生成水印图

@Override    public OutputStream watermark1(String ossURL,String logoOsskey,HttpServletResponse response) {        lock.lock();        OutputStream os = null;        InputStream imagestream=null;        //水印        String logoPath = ossService.getossURL(logoOsskey,bucket);        //原图ossURL        try {            os=response.getoutputStream();            imagestream = getimagestream(ossURL);            Image image2 = ImageIO.read(imagestream);            //获取原图信息            int width = image2.getWidth(null);            int height = image2.getHeight(null);            BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);            Graphics2D g = bufferedImage.createGraphics();            g.drawImage(image2,width,null);//            //设置多个图片水印            InputStream logo = getimagestream(logoPath);            Image imagelogo = ImageIO.read(logo);            int logoWidth = imagelogo.getWidth(null);            int logoHeight = imagelogo.getHeight(null);            //设置透明度,ALPHA为接口中自定义的值透明度 0.3F            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,ALPHA));            //完成图片旋转30            g.rotate(Math.toradians(0),bufferedImage.getWidth() / 2,bufferedImage.getHeight() / 2);            int x = -width / 2;            int y = -height / 2;            while (x < width * 1.5) {                y = -height / 2;                while (y < height * 1.5) {                    g.drawImage(imagelogo,x,y,null);                    y += logoHeight + 200;                }                x += logoHeight + 300;            }            g.dispose();            Thumbnails.Builder thumbnail = Thumbnails.of(bufferedImage);            thumbnail.size(x,y);            ImageIO.write(thumbnail.asBufferedImage(),"png",os);            //JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);            //en.encode(bufferedImage);        } catch (IOException e) {            e.printstacktrace();        } finally {            if (os != null) {                try {                    os.flush();                    os.close();                } catch (IOException e) {                    e.printstacktrace();                }            }            if(imagestream!=null){                try {                    imagestream.close();                }catch (IOException e){                    e.printstacktrace();                }            }        }        lock.unlock();        return os;    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐