圖片添加浮水印(Java 實現)

來源:互聯網
上載者:User

某些應用情境下需要對圖片加上浮水印防止盜用,例如微博使用者圖片。Java中實現添加浮水印需要用到BufferedImage、Graphics2D 和ImageIO類。 1. 添加文字浮水印

import java.awt.AlphaComposite;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.Image;import java.awt.RenderingHints;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import javax.imageio.ImageIO;/** * 添加文字浮水印 * * @author Ricky Fung */public class TextMarkProcessor {    /**     * @param args     */    public static void main(String[] args) {        new TextMarkProcessor().testTextMark();    }    public void testTextMark() {        File srcImgFile = new File("D:/test/desktop.png");        String logoText = "[ 天使的翅膀 ]";        File outputRotateImageFile = new File("D:/test/desktop_text_mark.jpg");        createWaterMarkByText(srcImgFile, logoText, outputRotateImageFile, 0);    }    public void createWaterMarkByText(File srcImgFile, String logoText,                                             File outputImageFile, double degree) {        OutputStream os = null;        try {            Image srcImg = ImageIO.read(srcImgFile);            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);            Graphics2D graphics = buffImg.createGraphics();            graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);            graphics.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null),                            srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);            if (degree>0) {                graphics.rotate(Math.toRadians(degree),                        (double) buffImg.getWidth() / 2,                        (double) buffImg.getHeight() / 2);            }            graphics.setColor(Color.RED);            graphics.setFont(new Font("宋體", Font.BOLD, 36));            float alpha = 0.8f;            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,                    alpha));            graphics.drawString(logoText, buffImg.getWidth()/3, buffImg.getHeight()/2);            graphics.dispose();            os = new FileOutputStream(outputImageFile);            // 產生圖片            ImageIO.write(buffImg, "JPG", os);        } catch (Exception e) {            e.printStackTrace();        } finally {            try {                if (null != os)                    os.close();            } catch (Exception e) {                e.printStackTrace();            }        }    }}
2. 添加圖片浮水印
import java.awt.AlphaComposite;import java.awt.Graphics2D;import java.awt.Image;import java.awt.RenderingHints;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import javax.imageio.ImageIO;import javax.swing.ImageIcon;/** * 添加圖片浮水印 * * @author Ricky Fung */public class PictureMarkProcessor {    /**     * @param args     */    public static void main(String[] args) {        new PictureMarkProcessor().testPictureMark();    }    public void testPictureMark() {        File srcImageFile = new File("D:/test/desktop.png");        File logoImageFile = new File("D:/test/tools.png");        File outputRoateImageFile = new File("D:/test/desktop_pic_mark.jpg");        createWaterMarkByIcon(srcImageFile, logoImageFile, outputRoateImageFile, 0);    }    public void createWaterMarkByIcon(File srcImageFile, File logoImageFile,                                             File outputImageFile, double degree) {        OutputStream os = null;        try {            Image srcImg = ImageIO.read(srcImageFile);            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);            Graphics2D graphics = buffImg.createGraphics();            graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);            graphics.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null),                            srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);            ImageIcon logoImgIcon = new ImageIcon(ImageIO.read(logoImageFile));            Image logoImg = logoImgIcon.getImage();            //旋轉            if (degree>0) {                graphics.rotate(Math.toRadians(degree),                        (double) buffImg.getWidth() / 2,                        (double) buffImg.getWidth() / 2);            }            float alpha = 0.8f; // 透明度            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));            //浮水印 的位置            graphics.drawImage(logoImg, buffImg.getWidth()/3, buffImg.getHeight()/2, null);            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));            graphics.dispose();            os = new FileOutputStream(outputImageFile);            // 產生圖片            ImageIO.write(buffImg, "JPG", os);        } catch (Exception e) {            e.printStackTrace();        } finally {            try {                if (null != os)                    os.close();            } catch (Exception e) {                e.printStackTrace();            }        }    }}

實現效果如下:

原圖:

加文字浮水印:

加圖片浮水印:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.