|
@@ -26,6 +26,7 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
class ScanViwerViewModel : BindableBase, INavigationAware
|
|
|
{
|
|
|
private CPDFViewer PDFViewer;
|
|
|
+ private ViewContentViewModel viewContentViewModel;
|
|
|
|
|
|
private WriteableBitmap bgImage;
|
|
|
|
|
@@ -62,6 +63,18 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private bool isShowRect;
|
|
|
+
|
|
|
+ public bool IsShowRect
|
|
|
+ {
|
|
|
+ get { return isShowRect; }
|
|
|
+ set
|
|
|
+ {
|
|
|
+ SetProperty(ref isShowRect, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private COCRLanguage language = COCRLanguage.English;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 增强扫描结果路径
|
|
@@ -99,6 +112,12 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
|
|
|
public void OnNavigatedTo(NavigationContext navigationContext)
|
|
|
{
|
|
|
+ navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
|
|
|
+ if (viewContentViewModel == null)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
|
|
|
if (PDFViewer == null)
|
|
|
{
|
|
@@ -155,10 +174,18 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
TextRectList = null;
|
|
|
}
|
|
|
}
|
|
|
+ ClearDraw();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void ClearDraw()
|
|
|
+ {
|
|
|
+ //笨办法,有好方案随时能换,目前是以触发属性改变事件的方式激活清空
|
|
|
+ IsShowRect = !IsShowRect;
|
|
|
+ IsShowRect = !IsShowRect;
|
|
|
+ }
|
|
|
+
|
|
|
public ScanViwerViewModel(IEventAggregator eventAggregator)
|
|
|
{
|
|
|
CloseTooltipCommand = new DelegateCommand(CloseTooltip);
|
|
@@ -168,6 +195,148 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
cacahe = new Dictionary<int, List<KeyValuePair<Rect, string>>>();
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 区域识别
|
|
|
+ /// </summary>
|
|
|
+ public void AreaProcess(Rect AreaRect, Rect ImageRect)
|
|
|
+ {
|
|
|
+ COCREngine imEngine = new COCREngine(App.modelFolderPath);
|
|
|
+
|
|
|
+ string path = App.CachePath.MergeFilePath;
|
|
|
+ string name = Guid.NewGuid().ToString();
|
|
|
+ path = Path.Combine(path, name);
|
|
|
+
|
|
|
+ Directory.CreateDirectory(path);
|
|
|
+
|
|
|
+ CPDFDocument CurrentDoc = PDFViewer.Document;
|
|
|
+
|
|
|
+ CErrorCode error = imEngine.SetModel(language);
|
|
|
+
|
|
|
+ cacahe.Remove(PDFViewer.CurrentIndex);
|
|
|
+ string pageImagePath = Path.Combine(path, "Area");
|
|
|
+ Directory.CreateDirectory(pageImagePath);
|
|
|
+ pageImagePath = Path.Combine(pageImagePath, PDFViewer.CurrentIndex.ToString());
|
|
|
+
|
|
|
+ if (IsEnhanced)
|
|
|
+ {
|
|
|
+ BitmapImage image = new BitmapImage(new Uri(EnhancedFilePathList[PDFViewer.CurrentIndex]));
|
|
|
+ BgImage = new WriteableBitmap(image);
|
|
|
+ double zoomWidth = AreaRect.Width / ImageRect.Width;
|
|
|
+ double zoomHeight = AreaRect.Height / ImageRect.Height;
|
|
|
+ double zoomX = (AreaRect.X - ImageRect.X) / ImageRect.Width;
|
|
|
+ double zoomY = (AreaRect.Y - ImageRect.Y) / ImageRect.Height;
|
|
|
+ Int32Rect rect = new Int32Rect((int)(BgImage.Width*zoomX), (int)(BgImage.Height*zoomY), (int)(BgImage.Width*zoomWidth), (int)(BgImage.Height*zoomHeight));
|
|
|
+ byte[] renderData = new byte[(int)(BgImage.Width * BgImage.Height * 4)];
|
|
|
+
|
|
|
+ BgImage.CopyPixels(rect, renderData, BgImage.BackBufferStride, 0);
|
|
|
+
|
|
|
+ WriteableBitmap bitmap = new WriteableBitmap(rect.Width, rect.Height, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
|
|
|
+ bitmap.WritePixels(new Int32Rect(0, 0, rect.Width, rect.Height), renderData, BgImage.BackBufferStride, 0);
|
|
|
+ BitmapEncoder encoder = new PngBitmapEncoder();
|
|
|
+ encoder.Frames.Add(BitmapFrame.Create(bitmap));
|
|
|
+
|
|
|
+ using (FileStream imageStream = File.Create(pageImagePath))
|
|
|
+ {
|
|
|
+ encoder.Save(imageStream);
|
|
|
+ imageStream.Flush();
|
|
|
+ }
|
|
|
+ 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 + BgImage.Width * zoomX, top + BgImage.Height * zoomY, right - left, bottom - top), ocrResult.text));
|
|
|
+ }
|
|
|
+ cacahe.Add(PDFViewer.CurrentIndex, RectList);
|
|
|
+
|
|
|
+ if (cacahe.Count > 0)
|
|
|
+ {
|
|
|
+ if (cacahe.ContainsKey(PDFViewer.CurrentIndex))
|
|
|
+ {
|
|
|
+ TextRectList = cacahe[PDFViewer.CurrentIndex];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ TextRectList = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ CPDFPage pdfPage = CurrentDoc.PageAtIndex(PDFViewer.CurrentIndex);
|
|
|
+ double zoomWidth = AreaRect.Width / ImageRect.Width;
|
|
|
+ double zoomHeight = AreaRect.Height / ImageRect.Height;
|
|
|
+ double zoomX = (AreaRect.X- ImageRect.X) / ImageRect.Width;
|
|
|
+ double zoomY = (AreaRect.Y- ImageRect.Y) / ImageRect.Height;
|
|
|
+
|
|
|
+ float zoom = (float)(DpiHelpers.Dpi / 72D);
|
|
|
+ int renderWidth = (int)(pdfPage.PageSize.Width* zoomWidth * zoom);
|
|
|
+ int renderHeight = (int)(pdfPage.PageSize.Height* zoomHeight * zoom);
|
|
|
+ int renderX = (int)(pdfPage.PageSize.Width* zoomX * zoom);
|
|
|
+ int renderY = (int)(pdfPage.PageSize.Height* zoomY * zoom);
|
|
|
+ byte[] renderData = new byte[renderWidth * renderHeight * 4];
|
|
|
+ pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(renderX, renderY, 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();
|
|
|
+ }
|
|
|
+
|
|
|
+ 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+ renderX, top+ renderY, right - left, bottom - top), ocrResult.text));
|
|
|
+ }
|
|
|
+ cacahe.Add(PDFViewer.CurrentIndex, RectList);
|
|
|
+
|
|
|
+ if (cacahe.Count > 0)
|
|
|
+ {
|
|
|
+ if (cacahe.ContainsKey(PDFViewer.CurrentIndex))
|
|
|
+ {
|
|
|
+ TextRectList = cacahe[PDFViewer.CurrentIndex];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ TextRectList = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ClearDraw();
|
|
|
+ }
|
|
|
+
|
|
|
private void CancelEnhanced()
|
|
|
{
|
|
|
ShowTooltip = Visibility.Collapsed;
|
|
@@ -204,6 +373,7 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
/// <param name="e"></param>
|
|
|
private void ChangeScanMode(ScanEventArgs e)
|
|
|
{
|
|
|
+ IsShowRect = false;
|
|
|
switch (e.Mode)
|
|
|
{
|
|
|
case ScanMode.Unknown:
|
|
@@ -223,6 +393,8 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
}
|
|
|
break;
|
|
|
case ScanMode.Area:
|
|
|
+ language = (COCRLanguage)e.ScanLanguage;
|
|
|
+ IsShowRect = true;
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
@@ -247,8 +419,20 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
{
|
|
|
CIMEngine imEngine = new CIMEngine(App.modelFolderPath);
|
|
|
CErrorCode error = imEngine.SetModel();
|
|
|
+
|
|
|
+ //显示进度条
|
|
|
+ App.mainWindowViewModel.IsProcessVisible = Visibility.Visible;
|
|
|
+ App.mainWindowViewModel.MaxValue = CurrentDoc.PageCount;
|
|
|
+
|
|
|
for (int i = 0; i < CurrentDoc.PageCount; i++)
|
|
|
{
|
|
|
+ //刷新进度
|
|
|
+ App.Current.Dispatcher.Invoke(async () =>
|
|
|
+ {
|
|
|
+ App.mainWindowViewModel.Value = i;
|
|
|
+ await Task.Delay(5);
|
|
|
+ });
|
|
|
+
|
|
|
string pageImagePath = Path.Combine(path, i.ToString());
|
|
|
string pageEnhancePath = Path.Combine(EnhancePath, i.ToString() + ".png");
|
|
|
try
|
|
@@ -287,6 +471,11 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
});
|
|
|
imEngine.Release();
|
|
|
ShowTooltip = Visibility.Visible;
|
|
|
+
|
|
|
+ //隐藏进度条
|
|
|
+ App.mainWindowViewModel.IsProcessVisible = Visibility.Collapsed;
|
|
|
+ App.mainWindowViewModel.MaxValue = 0;
|
|
|
+ App.mainWindowViewModel.Value = 0;
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -312,8 +501,19 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
|
|
|
cacahe.Clear();
|
|
|
|
|
|
+ //显示进度条
|
|
|
+ App.mainWindowViewModel.IsProcessVisible = Visibility.Visible;
|
|
|
+ App.mainWindowViewModel.MaxValue = args.PageRange.Count;
|
|
|
+
|
|
|
for (int i = 0; i < args.PageRange.Count; i++)
|
|
|
{
|
|
|
+ //刷新进度
|
|
|
+ App.Current.Dispatcher.Invoke(async () =>
|
|
|
+ {
|
|
|
+ App.mainWindowViewModel.Value = i;
|
|
|
+ await Task.Delay(5);
|
|
|
+ });
|
|
|
+
|
|
|
string pageImagePath = Path.Combine(path, args.PageRange[i].ToString());
|
|
|
|
|
|
CPDFPage pdfPage = CurrentDoc.PageAtIndex(args.PageRange[i]);
|
|
@@ -326,7 +526,6 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
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);
|
|
@@ -367,6 +566,10 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
}
|
|
|
}
|
|
|
imEngine.Release();
|
|
|
+ //隐藏进度条
|
|
|
+ App.mainWindowViewModel.IsProcessVisible = Visibility.Collapsed;
|
|
|
+ App.mainWindowViewModel.MaxValue = 0;
|
|
|
+ App.mainWindowViewModel.Value = 0;
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -421,5 +624,6 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
imEngine.Release();
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
}
|