翻譯|使用教程|編輯:吉煒煒|2025-01-16 13:29:38.140|閱讀 110 次
概述:雖然基于文本的比較方法很常見,但在某些情況下,基于圖像的逐像素方法具有獨特的優勢。本文探討了這種方法的適用情況和原因,并提供了突出其實用性和速度的示例和應用程序。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
文檔比較是許多行業中的關鍵功能,可幫助團隊高效識別文檔版本之間的差異。雖然基于文本的比較方法很常見,但在某些情況下,基于圖像的逐像素方法具有獨特的優勢。本文提供了示例和應用程序來展示此方法的實用性和速度,并探討了該方法何時以及為何有用。
使用 TX Text Control API,可以遍歷所有段落和字符以檢查位置和格式。雖然這在技術上是可行的,而且 TX Text Control 已經很快了,但對于較長的文檔來說,它還是太慢了。
TX Text Control 是一款功能類似于 MS Word 的文字處理控件,包括文檔創建、編輯、打印、郵件合并、格式轉換、拆分合并、導入導出、批量生成等功能。廣泛應用于企業文檔管理,網站內容發布,電子病歷中病案模板創建、病歷書寫、修改歷史、連續打印、病案歸檔等功能的實現。
基于圖像的文檔比較將文檔的頁面渲染為圖像,并逐個像素進行比較。這種方法不是通過編程分析文本內容、格式或定位,而是直接識別視覺差異。傳統的基于文本的比較方法解析文檔結構、提取文本、分析格式并檢測位置差異。這個過程可能需要大量計算,尤其是對于布局復雜或格式繁雜的復雜文檔。基于圖像的比較跳過這些步驟并直接比較渲染的圖像,這可以大大縮短處理時間。
基于文本的方法可能會忽略某些視覺差異,例如輕微的字體變化、對齊偏移或顏色變化。逐像素比較可以準確捕捉這些差異,使其成為視覺關鍵應用的理想選擇。
為了演示目的,我們將使用 TX Text Control 安裝附帶的演示文檔。該文檔有六頁,包含 TX Text Control 的大部分功能。
在第一遍中,我們將獲取文檔的兩個精確副本,并使用以下代碼對它們進行比較。
using static DocumentComparer; | |
string document1 = "demo1.tx"; | |
string document2 = "demo2.tx"; | |
// Get the comparison results | |
List<PageComparisonResult> comparisonResults = DocumentComparer.CompareDocuments(document1, document2); | |
// Generate and display the results | |
foreach (var result in comparisonResults) | |
{ | |
if (result.PageIndex == -1) | |
{ | |
// Special case for differing page counts | |
Console.WriteLine(result.Message); | |
} | |
else | |
{ | |
string message = result.AreEqual | |
? $"The document images of page {result.PageIndex + 1} are equal." | |
: $"The document images of page {result.PageIndex + 1} are different."; | |
Console.WriteLine(message); | |
} | |
} |
運行此代碼時,結果將如下所示,這意味著文檔相同:
The document images of page 1 are equal. The document images of page 2 are equal. The document images of page 3 are equal. The document images of page 4 are equal. The document images of page 5 are equal. The document images of page 6 are equal.
現在讓我們改變第 1 頁第一段的字體,并縮小第 4 頁圖像的尺寸。
當再次運行相同的代碼時,結果將如下所示:
The document images of page 1 are different. The document images of page 2 are equal. The document images of page 3 are equal. The document images of page 4 are different. The document images of page 5 are equal. The document images of page 6 are equal.
該類DocumentComparer是一個靜態實用程序,用于逐頁比較兩個文檔。它可幫助您了解文檔在視覺上是否相同或存在差異。該CompareDocuments方法提供了比較兩個文檔的入口點。它使用服務器文本控件 實例加載兩個文檔,并將每個文檔轉換為位圖對象列表。
public static List<PageComparisonResult> CompareDocuments(string documentPath1, string documentPath2) | |
{ | |
var comparisonResults = new List<PageComparisonResult>(); | |
using (var serverTextControl = new ServerTextControl()) | |
{ | |
serverTextControl.Create(); | |
// Load and render the first document | |
serverTextControl.Load(documentPath1, StreamType.InternalUnicodeFormat); | |
var bitmapsDocument1 = GetDocumentImages(serverTextControl); | |
// Load and render the second document | |
serverTextControl.Load(documentPath2, StreamType.InternalUnicodeFormat); | |
var bitmapsDocument2 = GetDocumentImages(serverTextControl); | |
// Compare pages | |
if (bitmapsDocument1.Count != bitmapsDocument2.Count) | |
{ | |
comparisonResults.Add(new PageComparisonResult | |
{ | |
PageIndex = -1, | |
AreEqual = false, | |
Message = "The documents have different page counts." | |
}); | |
return comparisonResults; // Return early if page counts differ | |
} | |
for (int i = 0; i < bitmapsDocument1.Count; i++) | |
{ | |
using (var bitmap1 = bitmapsDocument1[i]) | |
using (var bitmap2 = bitmapsDocument2[i]) | |
{ | |
comparisonResults.Add(new PageComparisonResult | |
{ | |
PageIndex = i, | |
AreEqual = !DocumentComparer.IsDifferent(bitmap1, bitmap2), | |
Message = null | |
}); | |
} | |
} | |
} | |
return comparisonResults; | |
} |
每個位圖代表一個渲染頁面。該方法首先檢查文檔的頁數是否相同。如果頁數不同,它會立即返回一個結果,突出顯示此差異。對于頁數匹配的文檔,該方法使用 IsDifferent 函數比較每個頁面的渲染位圖對象,識別任何視覺差異。
該GetDocumentImages方法從加載到 ServerTextControl 的文檔中提取所有頁面的高分辨率圖像。每頁都以 300 DPI 呈現,以保持高保真度并確保準確的基于像素的比較。
private static List<Bitmap> GetDocumentImages(ServerTextControl serverTextControl) | |
{ | |
var bitmaps = new List<Bitmap>(); | |
var pages = serverTextControl.GetPages(); | |
for (int i = 1; i <= pages.Count; i++) | |
{ | |
// Get image for each page | |
bitmaps.Add(pages[i].GetImage(300, Page.PageContent.All)); | |
} | |
return bitmaps; | |
} |
該IsDifferent方法通過逐字節比較像素數據來確定兩個位圖對象是否不同。如果圖像的尺寸不同,則立即將其標記為不同。該方法鎖定像素數據以實現高效訪問,逐字節比較原始像素數據以查找不匹配,然后在比較完成后解鎖數據。這種方法可確保準確檢測出細微的視覺差異。
public static bool IsDifferent(Bitmap bitmap1, Bitmap bitmap2) | |
{ | |
if (bitmap1 == null || bitmap2 == null) | |
{ | |
throw new ArgumentNullException("Bitmaps cannot be null."); | |
} | |
if (bitmap1.Width != bitmap2.Width || bitmap1.Height != bitmap2.Height) | |
{ | |
// Consider images different if dimensions are not the same. | |
return true; | |
} | |
// Lock the bits for both images for efficient pixel access. | |
var rect = new Rectangle(0, 0, bitmap1.Width, bitmap1.Height); | |
BitmapData data1 = bitmap1.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); | |
BitmapData data2 = bitmap2.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); | |
try | |
{ | |
// Compare pixel data byte by byte. | |
int bytes = data1.Stride * data1.Height; | |
byte[] buffer1 = new byte[bytes]; | |
byte[] buffer2 = new byte[bytes]; | |
System.Runtime.InteropServices.Marshal.Copy(data1.Scan0, buffer1, 0, bytes); | |
System.Runtime.InteropServices.Marshal.Copy(data2.Scan0, buffer2, 0, bytes); | |
for (int i = 0; i < bytes; i++) | |
{ | |
if (buffer1[i] != buffer2[i]) | |
{ | |
return true; | |
} | |
} | |
} | |
finally | |
{ | |
// Unlock the bits. | |
bitmap1.UnlockBits(data1); | |
bitmap2.UnlockBits(data2); | |
} | |
return false; | |
} |
基于 IImage 的文檔比較提供了一種獨特、非常快速且有效的方法來識別文檔之間的視覺差異。通過將文檔呈現為圖像并逐個像素進行比較,此方法提供了一種快速而準確的方法來檢測變化。這種方法對于視覺關鍵應用程序特別有用,因為基于文本的方法可能會忽略細微的差異。該DocumentComparer實用程序演示了如何使用 TX Text Control 實現基于圖像的文檔比較,從而為比較文檔提供了一種實用且有效的解決方案。
產品試用下載、價格咨詢、優惠獲取,或其他任何問題,請聯系。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網