搜尋
首頁JavaJava入門java實作為圖片添加圖片浮水印

java實作為圖片添加圖片浮水印

May 08, 2020 pm 04:42 PM
java圖片圖片浮水印

java實作為圖片添加圖片浮水印

具體程式碼:

 // 水印透明度
    private static float alpha = 0.5f;
    
       /**
     * 给图片添加水印图片、可设置水印图片旋转角度
     *
     * @param iconPath   水印图片路径
     * @param srcImgPath 源图片路径
     * @param location   水印图片位置
     * @param degree     水印图片旋转角度
     */
    public static void markImageByIcon(HttpServletRequest request, HttpServletResponse response,
                                       String iconPath, String srcImgPath, String location, Integer degree) {
        OutputStream os = null;
        try {

            Image srcImg = ImageIO.read(new File(srcImgPath));

            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

            // 得到画笔对象
            Graphics2D g = buffImg.createGraphics();

            // 设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);

            g.drawImage(
                    srcImg.getScaledInstance(srcImg.getWidth(null),
                            srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,
                    null);

            // 设置水印旋转角度(默认对角线角度)
            if (null != degree) {
                g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
            } else {
                //根据三角形相关定理,计算对角线角度
                double lengthOfDiagonal = Math.sqrt(buffImg.getWidth() * buffImg.getWidth() + buffImg.getHeight() * buffImg.getHeight());
                double v = (Math.pow(buffImg.getWidth(), 2) + Math.pow(lengthOfDiagonal, 2) - Math.pow(buffImg.getHeight(), 2)) / (2 * buffImg.getWidth() * lengthOfDiagonal);
                double acos = Math.acos(v);
                double myDegree = Math.toDegrees(acos);
                g.rotate(-Math.toRadians(myDegree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
            }

            // 水印图片的路径 水印图片一般为gif或者png的,这样可设置透明度
            ImageIcon imgIcon = new ImageIcon(iconPath);
            Image img = imgIcon.getImage();
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                    alpha));

            // 水印图片的位置
            int x = 0, y = 0;
            if (StringUtils.equals(location, "left-top")) {
                x = 30;
                y = 30;
            } else if (StringUtils.equals(location, "right-top")) {
                x = buffImg.getWidth() - img.getWidth(null) - 30;
                y = 30;
            } else if (StringUtils.equals(location, "left-bottom")) {
                x += 30;
                y = buffImg.getHeight() - img.getHeight(null) - 30;
            } else if (StringUtils.equals(location, "right-bottom")) {
                x = buffImg.getWidth() - img.getWidth(null) - 30;
                y = buffImg.getHeight() - img.getHeight(null) - 30;
            } else {
                x = (buffImg.getWidth() - img.getWidth(null)) / 2;
                y = (buffImg.getHeight() - img.getHeight(null)) / 2;
            }
            g.drawImage(img, x, y, null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            g.dispose();

            // os = new FileOutputStream(targerPath);
            os = response.getOutputStream();
            ImageIO.write(buffImg, "JPG", os);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

(影片教學推薦:java影片

smbfile時也可以寫成這樣:

// 展示时 添加水印
	public void showRemarkImage(String filePath, String iconPath, HttpServletRequest request, HttpServletResponse response) {
		InputStream is = null;
		OutputStream os = null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();  
		try {
			SmbFile smbFile = new SmbFile(filePath);
			is = smbFile.getInputStream();
			os = response.getOutputStream();
			
			Image srcImg = ImageIO.read(is);
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
            // 得到画笔对象
            Graphics2D g = buffImg.createGraphics();
            // 设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);

            g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
                     
            // 旋转角度处理(根据三角形相关定理,计算对角线角度)
//            double lengthOfDiagonal = Math.sqrt(buffImg.getWidth() * buffImg.getWidth() + buffImg.getHeight() * buffImg.getHeight());
//            double v = (Math.pow(buffImg.getWidth(), 2) + Math.pow(lengthOfDiagonal, 2) - Math.pow(buffImg.getHeight(), 2)) / (2 * buffImg.getWidth() * lengthOfDiagonal);
//            double acos = Math.acos(v);
//            double myDegree = Math.toDegrees(acos);
//            g.rotate(-Math.toRadians(myDegree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);            
          
            // 水印图片的路径 水印图片一般为gif或者png的,这样可设置透明度
            ImageIcon imgIcon = new ImageIcon(iconPath);
            Image img = imgIcon.getImage();
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            // 水印图片居中
            int x = 0, y = 0;       
            x = (buffImg.getWidth() - img.getWidth(null)) / 2;
            y = (buffImg.getHeight() - img.getHeight(null)) / 2;          
            g.drawImage(img, x, y, null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            g.dispose();
                       
	        ImageIO.write(buffImg, "png", out); 
	        byte[] b = out.toByteArray();
			response.addHeader("Content-Length", String.valueOf(b.length));	      	
			os.write(b, 0, b.length);
			os.flush();	      	    
         
		} catch (Exception e) {
			LogUtil.error("附件下载失败,时间:" + DateUtil.formatToDateTimeText(new Date()) + "原因:" + e.getMessage());
//			throw new ApplicationException("文件读取失败:" + e.getMessage(), e);
		} finally {
			IOUtils.closeQuietly(is);
			IOUtils.closeQuietly(os);
			IOUtils.closeQuietly(out);
		}
	}

推薦教學:java入門程式

#

以上是java實作為圖片添加圖片浮水印的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:csdn。如有侵權,請聯絡admin@php.cn刪除

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器