在保持品質的情況下大規模縮小影像
過度縮小影像可能會帶來品質挑戰。為了解決這個問題,分而治之是一種有效的方法。方法如下:
1.原圖:
2.增量縮小:
不是一步從300x300 調整到60x60,而是逐漸縮放:
- 調整到150x150(原始尺寸的1/ 2)
- 調整大小至75x75(1/2 150x150)
- 調整大小為37x38 (75x75 的1/2)
- 調整大小為60x60 (37x38 的1/2) 調整大小為60x60 (37x38 的1/2)
每一步都會減少影像尺寸一半。
3。結果:
4。比較(一步與分治之):
5. Java 實作:
public class DivideAndConquerImageResizer { public static main(String[] args) { // Load the original image BufferedImage original = ImageIO.read(...); // Create a scaled image BufferedImage scaled = null; Dimension currentSize = new Dimension(original.getWidth(), original.getHeight()); Dimension desiredSize = new Dimension(60, 60); while (currentSize.getWidth() > desiredSize.getWidth() || currentSize.getHeight() > desiredSize.getHeight()) { // Halve the current size currentSize.width = (int) Math.ceil(currentSize.width / 2.0); currentSize.height = (int) Math.ceil(currentSize.height / 2.0); // Resize the image to the current size scaled = getScaledInstanceToFit(original, currentSize); } // Save the scaled image ImageIO.write(scaled, "jpg", ...); } public static BufferedImage getScaledInstanceToFit(BufferedImage img, Dimension size) { float scaleFactor = getScaleFactorToFit(img, size); return getScaledInstance(img, scaleFactor); } public static float getScaleFactorToFit(BufferedImage img, Dimension size) { float scale = 1f; if (img != null) { int imageWidth = img.getWidth(); int imageHeight = img.getHeight(); scale = getScaleFactorToFit(new Dimension(imageWidth, imageHeight), size); } return scale; } public static float getScaleFactorToFit(Dimension original, Dimension toFit) { float scale = 1f; if (original != null && toFit != null) { float dScaleWidth = getScaleFactor(original.width, toFit.width); float dScaleHeight = getScaleFactor(original.height, toFit.height); scale = Math.min(dScaleHeight, dScaleWidth); } return scale; } public static float getScaleFactor(int iMasterSize, int iTargetSize) { float scale = 1; if (iMasterSize > iTargetSize) { scale = (float) iTargetSize / (float) iMasterSize; } else { scale = (float) iTargetSize / (float) iMasterSize; } return scale; } public static BufferedImage getScaledInstance(BufferedImage img, double dScaleFactor) { BufferedImage imgBuffer = null; imgBuffer = getScaledInstance(img, dScaleFactor, RenderingHints.VALUE_INTERPOLATION_BILINEAR, true); return imgBuffer; } }
這種方法可以顯著提高影像質量,尤其是在大幅縮小尺寸時。
以上是大幅縮小大影像時如何保持影像品質?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

在使用IntelliJIDEAUltimate版本啟動Spring...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

Redis緩存方案如何實現產品排行榜列表的需求?在開發過程中,我們常常需要處理排行榜的需求,例如展示一個�...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...

在Idea中如何設置SpringBoot項目默認運行配置列表在使用IntelliJ...


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

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

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。