|
@@ -1,21 +1,55 @@
|
|
|
-using ComPDFKitViewer;
|
|
|
+using ComDocumentAIKit;
|
|
|
+using ComPDFKit.PDFDocument;
|
|
|
+using ComPDFKit.PDFPage;
|
|
|
+using ComPDFKitViewer;
|
|
|
using ComPDFKitViewer.PdfViewer;
|
|
|
using PDF_Office.EventAggregators;
|
|
|
+using PDF_Office.Helper;
|
|
|
using PDF_Office.Model;
|
|
|
using Prism.Events;
|
|
|
using Prism.Mvvm;
|
|
|
using Prism.Regions;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
+using System.Windows;
|
|
|
+using System.Windows.Media;
|
|
|
+using System.Windows.Media.Imaging;
|
|
|
|
|
|
namespace PDF_Office.ViewModels.Scan
|
|
|
{
|
|
|
class ScanViwerViewModel : BindableBase, INavigationAware
|
|
|
{
|
|
|
private CPDFViewer PDFViewer;
|
|
|
+
|
|
|
+ private WriteableBitmap bgImage;
|
|
|
+
|
|
|
+ public WriteableBitmap BgImage
|
|
|
+ {
|
|
|
+ get { return bgImage; }
|
|
|
+ set
|
|
|
+ {
|
|
|
+ SetProperty(ref bgImage, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<KeyValuePair<Rect, string>> textRectList;
|
|
|
+
|
|
|
+ public List<KeyValuePair<Rect, string>> TextRectList
|
|
|
+ {
|
|
|
+ get { return textRectList; }
|
|
|
+ set
|
|
|
+ {
|
|
|
+ SetProperty(ref textRectList, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Dictionary<int, List<KeyValuePair<Rect, string>>> cacahe;
|
|
|
+
|
|
|
+
|
|
|
public bool IsNavigationTarget(NavigationContext navigationContext)
|
|
|
{
|
|
|
return true;
|
|
@@ -36,6 +70,17 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
else
|
|
|
{
|
|
|
PDFViewer.InfoChanged += PDFViewer_InfoChanged;
|
|
|
+
|
|
|
+ PDFViewer.ChangeViewMode(ViewMode.Single);
|
|
|
+ //第一次进入获取当前预览显示内容
|
|
|
+ CPDFPage pdfPage = PDFViewer.Document.PageAtIndex(PDFViewer.CurrentIndex);
|
|
|
+ float zoom = (float)(DpiHelpers.Dpi / 72D);
|
|
|
+ int renderWidth = (int)(pdfPage.PageSize.Width * zoom);
|
|
|
+ int renderHeight = (int)(pdfPage.PageSize.Height * zoom);
|
|
|
+ byte[] renderData = new byte[renderWidth * renderHeight * 4];
|
|
|
+ pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(0, 0, renderWidth, renderHeight), 0xFFFFFFFF, renderData, 0);
|
|
|
+ BgImage = new WriteableBitmap(renderWidth, renderHeight, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
|
|
|
+ BgImage.WritePixels(new Int32Rect(0, 0, renderWidth, renderHeight), renderData, BgImage.BackBufferStride, 0);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -46,7 +91,26 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
RenderData renderData = e.Value as RenderData;
|
|
|
if (renderData != null)
|
|
|
{
|
|
|
- var CurrentPage = renderData.PageIndex;
|
|
|
+ CPDFPage pdfPage = PDFViewer.Document.PageAtIndex(renderData.PageIndex - 1);
|
|
|
+ float zoom = (float)(DpiHelpers.Dpi / 72D);
|
|
|
+ int renderWidth = (int)(pdfPage.PageSize.Width * zoom);
|
|
|
+ int renderHeight = (int)(pdfPage.PageSize.Height * zoom);
|
|
|
+ byte[] Data = new byte[renderWidth * renderHeight * 4];
|
|
|
+ pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(0, 0, renderWidth, renderHeight), 0xFFFFFFFF, Data, 0);
|
|
|
+ BgImage = new WriteableBitmap(renderWidth, renderHeight, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
|
|
|
+ BgImage.WritePixels(new Int32Rect(0, 0, renderWidth, renderHeight), Data, BgImage.BackBufferStride, 0);
|
|
|
+
|
|
|
+ if (cacahe.Count > 0)
|
|
|
+ {
|
|
|
+ if (cacahe.ContainsKey(renderData.PageIndex - 1))
|
|
|
+ {
|
|
|
+ TextRectList = cacahe[renderData.PageIndex - 1];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ TextRectList = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -54,6 +118,7 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
public ScanViwerViewModel(IEventAggregator eventAggregator)
|
|
|
{
|
|
|
eventAggregator.GetEvent<ScanEvent>().Subscribe(ChangeScanMode, e => e.Unicode == App.mainWindowViewModel.SelectedItem.Unicode);
|
|
|
+ cacahe = new Dictionary<int, List<KeyValuePair<Rect, string>>>();
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 根据Vm事件通知处理OCR与区域识别事件
|
|
@@ -66,6 +131,7 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
case ScanMode.Unknown:
|
|
|
break;
|
|
|
case ScanMode.OCR:
|
|
|
+ OCRProcess(e);
|
|
|
break;
|
|
|
case ScanMode.Area:
|
|
|
break;
|
|
@@ -74,5 +140,83 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void OCRProcess(ScanEventArgs args)
|
|
|
+ {
|
|
|
+ COCREngine imEngine = new COCREngine(App.modelFolderPath);
|
|
|
+ //CIMEngine imEngine = new CIMEngine(App.modelFolderPath);
|
|
|
+
|
|
|
+ string path = App.CachePath.MergeFilePath;
|
|
|
+ string name = Guid.NewGuid().ToString();
|
|
|
+ path = Path.Combine(path, name);
|
|
|
+ string EnhancePath = Path.Combine(path, "Enhance");
|
|
|
+
|
|
|
+ Directory.CreateDirectory(path);
|
|
|
+ Directory.CreateDirectory(EnhancePath);
|
|
|
+
|
|
|
+ CPDFDocument CurrentDoc = PDFViewer.Document;
|
|
|
+
|
|
|
+ CErrorCode error = imEngine.SetModel((COCRLanguage)args.ScanLanguage);
|
|
|
+
|
|
|
+ cacahe.Clear();
|
|
|
+
|
|
|
+ for (int i = 0; i < args.PageRange.Count; i++)
|
|
|
+ {
|
|
|
+ string pageImagePath = Path.Combine(path, args.PageRange[i].ToString());
|
|
|
+ string pageEnhancePath = Path.Combine(EnhancePath, args.PageRange[i].ToString() + ".png");
|
|
|
+
|
|
|
+ CPDFPage pdfPage = CurrentDoc.PageAtIndex(args.PageRange[i]);
|
|
|
+ float zoom = (float)(DpiHelpers.Dpi / 72D);
|
|
|
+ int renderWidth = (int)(pdfPage.PageSize.Width * zoom);
|
|
|
+ int renderHeight = (int)(pdfPage.PageSize.Height * zoom);
|
|
|
+ byte[] renderData = new byte[renderWidth * renderHeight * 4];
|
|
|
+ pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(0, 0, renderWidth, renderHeight), 0xFFFFFFFF, renderData, 0);
|
|
|
+ WriteableBitmap bitmap = new WriteableBitmap(renderWidth, renderHeight, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
|
|
|
+ bitmap.WritePixels(new Int32Rect(0, 0, renderWidth, renderHeight), renderData, bitmap.BackBufferStride, 0);
|
|
|
+ BitmapEncoder encoder = new PngBitmapEncoder();
|
|
|
+ encoder.Frames.Add(BitmapFrame.Create(bitmap));
|
|
|
+
|
|
|
+ using (FileStream imageStream = File.Create(pageImagePath))
|
|
|
+ {
|
|
|
+ encoder.Save(imageStream);
|
|
|
+ imageStream.Flush();
|
|
|
+ }
|
|
|
+
|
|
|
+ //File.Create(pageEnhancePath);
|
|
|
+ //error = imEngine.Process(pageImagePath, pageEnhancePath);
|
|
|
+ error = imEngine.Process(pageImagePath);
|
|
|
+
|
|
|
+ if (imEngine.OCRResultList == null)
|
|
|
+ return;
|
|
|
+
|
|
|
+ List<KeyValuePair<Rect, string>> RectList = new List<KeyValuePair<Rect, string>>();
|
|
|
+ foreach (COCRResult ocrResult in imEngine.OCRResultList)
|
|
|
+ {
|
|
|
+ List<Point> rectPoints = new List<Point>();
|
|
|
+ for (int j = 0; j < ocrResult.position.Length; j += 2)
|
|
|
+ {
|
|
|
+ rectPoints.Add(new Point(ocrResult.position[j], ocrResult.position[j + 1]));
|
|
|
+ }
|
|
|
+ int left = (int)rectPoints.AsEnumerable().Min(x => x.X);
|
|
|
+ int right = (int)rectPoints.AsEnumerable().Max(x => x.X);
|
|
|
+ int top = (int)rectPoints.AsEnumerable().Min(x => x.Y);
|
|
|
+ int bottom = (int)rectPoints.AsEnumerable().Max(x => x.Y);
|
|
|
+
|
|
|
+ RectList.Add(new KeyValuePair<Rect, string>(new Rect(left, top, right - left, bottom - top), ocrResult.text));
|
|
|
+ }
|
|
|
+ cacahe.Add(args.PageRange[i], RectList);
|
|
|
+ }
|
|
|
+ if (cacahe.Count > 0)
|
|
|
+ {
|
|
|
+ if (cacahe.ContainsKey(PDFViewer.CurrentIndex))
|
|
|
+ {
|
|
|
+ TextRectList = cacahe[PDFViewer.CurrentIndex];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ TextRectList = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ imEngine.Release();
|
|
|
+ }
|
|
|
}
|
|
|
}
|