Java-图片加水印-与图片缩放

来源:互联网 发布:拳皇97网络对战平台 编辑:程序博客网 时间:2024/05/17 06:29
public static void main(String[] args) throws Exception {        // 创建文件对象        File srcFile = new File("C:\\image\\原图.jpg");        // 将文件对象变成图片对象        Image src = ImageIO.read(srcFile);        int width=src.getWidth(null);        int height=src.getHeight(null);        // 创建图片流        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);        // 创建画笔,在图片流上画想要的图        Graphics g = image.createGraphics();        // 采用getScaledInstance方式在坐标0.0处画图        //getScaledInstance方法按照比例缩放不会失真        g.drawImage(src.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);        //上述代码可以用来处理图片缩放功能        File markFile = new File("C:\\image\\水印图片.jpg");        Image srcMark = ImageIO.read(markFile);        int widthMark = srcMark.getWidth(null);        int heightMark = srcMark.getHeight(null);        // 图片流上已经有源图片了,这时在图片流中间开始画        g.drawImage(srcMark, (width - widthMark) / 2, (height - heightMark) / 2, widthMark, heightMark, null);        // 把打完水印的图片放到哪里?        FileOutputStream out = new FileOutputStream("C:\\image\\水印图片.jpg");        ImageIO.write(image, "JPG", out);// 创建图片        out.close();        g.dispose();}
0 0
原创粉丝点击