|
@@ -18,6 +18,7 @@ using System.Threading.Tasks;
|
|
|
using System.Windows;
|
|
|
using System.Windows.Media;
|
|
|
using System.Windows.Media.Imaging;
|
|
|
+using System.Windows.Threading;
|
|
|
|
|
|
namespace PDF_Office.ViewModels.Scan
|
|
|
{
|
|
@@ -188,43 +189,50 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
Directory.CreateDirectory(path);
|
|
|
Directory.CreateDirectory(EnhancePath);
|
|
|
|
|
|
- CIMEngine imEngine = new CIMEngine(App.modelFolderPath);
|
|
|
- CErrorCode error = imEngine.SetModel();
|
|
|
- for (int i = 0; i < CurrentDoc.PageCount; i++)
|
|
|
+ Task.Run(() =>
|
|
|
{
|
|
|
- string pageImagePath = Path.Combine(path, i.ToString());
|
|
|
- string pageEnhancePath = Path.Combine(EnhancePath, i.ToString() + ".png");
|
|
|
- try
|
|
|
+ CIMEngine imEngine = new CIMEngine(App.modelFolderPath);
|
|
|
+ CErrorCode error = imEngine.SetModel();
|
|
|
+ for (int i = 0; i < CurrentDoc.PageCount; i++)
|
|
|
{
|
|
|
- CPDFPage pdfPage = CurrentDoc.PageAtIndex(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))
|
|
|
+ string pageImagePath = Path.Combine(path, i.ToString());
|
|
|
+ string pageEnhancePath = Path.Combine(EnhancePath, i.ToString() + ".png");
|
|
|
+ try
|
|
|
{
|
|
|
- encoder.Save(imageStream);
|
|
|
- imageStream.Flush();
|
|
|
- }
|
|
|
+ CPDFPage pdfPage = CurrentDoc.PageAtIndex(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);
|
|
|
- EnhancedFilePathList.Add(pageEnhancePath);
|
|
|
- }
|
|
|
- catch
|
|
|
- {
|
|
|
+ //File.Create(pageEnhancePath);
|
|
|
+ error = imEngine.Process(pageImagePath, pageEnhancePath);
|
|
|
+ EnhancedFilePathList.Add(pageEnhancePath);
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- BitmapImage image = new BitmapImage(new Uri(EnhancedFilePathList[PDFViewer.CurrentIndex]));
|
|
|
- BgImage = new WriteableBitmap(image);
|
|
|
+ Application.Current.Dispatcher.Invoke(() =>
|
|
|
+ {
|
|
|
+ BitmapImage image = new BitmapImage(new Uri(EnhancedFilePathList[PDFViewer.CurrentIndex]));
|
|
|
+ BgImage = new WriteableBitmap(image);
|
|
|
+ });
|
|
|
+ imEngine.Release();
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -233,76 +241,79 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
/// <param name="args"></param>
|
|
|
private void OCRProcess(ScanEventArgs args)
|
|
|
{
|
|
|
- COCREngine imEngine = new COCREngine(App.modelFolderPath);
|
|
|
- //CIMEngine imEngine = new CIMEngine(App.modelFolderPath);
|
|
|
+ Task.Run(() =>
|
|
|
+ {
|
|
|
+ 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 path = App.CachePath.MergeFilePath;
|
|
|
+ string name = Guid.NewGuid().ToString();
|
|
|
+ path = Path.Combine(path, name);
|
|
|
|
|
|
- Directory.CreateDirectory(path);
|
|
|
+ Directory.CreateDirectory(path);
|
|
|
|
|
|
- CPDFDocument CurrentDoc = PDFViewer.Document;
|
|
|
+ CPDFDocument CurrentDoc = PDFViewer.Document;
|
|
|
|
|
|
- CErrorCode error = imEngine.SetModel((COCRLanguage)args.ScanLanguage);
|
|
|
+ CErrorCode error = imEngine.SetModel((COCRLanguage)args.ScanLanguage);
|
|
|
|
|
|
- cacahe.Clear();
|
|
|
+ cacahe.Clear();
|
|
|
|
|
|
- for (int i = 0; i < args.PageRange.Count; i++)
|
|
|
- {
|
|
|
- string pageImagePath = Path.Combine(path, args.PageRange[i].ToString());
|
|
|
+ for (int i = 0; i < args.PageRange.Count; i++)
|
|
|
+ {
|
|
|
+ string pageImagePath = Path.Combine(path, args.PageRange[i].ToString());
|
|
|
|
|
|
- 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));
|
|
|
+ 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();
|
|
|
- }
|
|
|
+ using (FileStream imageStream = File.Create(pageImagePath))
|
|
|
+ {
|
|
|
+ encoder.Save(imageStream);
|
|
|
+ imageStream.Flush();
|
|
|
+ }
|
|
|
|
|
|
- error = imEngine.Process(pageImagePath);
|
|
|
+ error = imEngine.Process(pageImagePath);
|
|
|
|
|
|
- if (imEngine.OCRResultList == null)
|
|
|
- return;
|
|
|
+ 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)
|
|
|
+ List<KeyValuePair<Rect, string>> RectList = new List<KeyValuePair<Rect, string>>();
|
|
|
+ foreach (COCRResult ocrResult in imEngine.OCRResultList)
|
|
|
{
|
|
|
- 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);
|
|
|
+ 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];
|
|
|
+ RectList.Add(new KeyValuePair<Rect, string>(new Rect(left, top, right - left, bottom - top), ocrResult.text));
|
|
|
+ }
|
|
|
+ cacahe.Add(args.PageRange[i], RectList);
|
|
|
}
|
|
|
- else
|
|
|
+ if (cacahe.Count > 0)
|
|
|
{
|
|
|
- TextRectList = null;
|
|
|
+ if (cacahe.ContainsKey(PDFViewer.CurrentIndex))
|
|
|
+ {
|
|
|
+ TextRectList = cacahe[PDFViewer.CurrentIndex];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ TextRectList = null;
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- imEngine.Release();
|
|
|
+ imEngine.Release();
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -311,47 +322,50 @@ namespace PDF_Office.ViewModels.Scan
|
|
|
/// <param name="args"></param>
|
|
|
private void EnhancedOCRProcess(ScanEventArgs args)
|
|
|
{
|
|
|
- COCREngine imEngine = new COCREngine(App.modelFolderPath);
|
|
|
-
|
|
|
- CErrorCode error = imEngine.SetModel((COCRLanguage)args.ScanLanguage);
|
|
|
-
|
|
|
- cacahe.Clear();
|
|
|
- for (int i = 0; i < args.PageRange.Count; i++)
|
|
|
+ Task.Run(() =>
|
|
|
{
|
|
|
- error = imEngine.Process(EnhancedFilePathList[i]);
|
|
|
+ COCREngine imEngine = new COCREngine(App.modelFolderPath);
|
|
|
|
|
|
- if (imEngine.OCRResultList == null)
|
|
|
- return;
|
|
|
+ CErrorCode error = imEngine.SetModel((COCRLanguage)args.ScanLanguage);
|
|
|
|
|
|
- List<KeyValuePair<Rect, string>> RectList = new List<KeyValuePair<Rect, string>>();
|
|
|
- foreach (COCRResult ocrResult in imEngine.OCRResultList)
|
|
|
+ cacahe.Clear();
|
|
|
+ for (int i = 0; i < args.PageRange.Count; i++)
|
|
|
{
|
|
|
- List<Point> rectPoints = new List<Point>();
|
|
|
- for (int j = 0; j < ocrResult.position.Length; j += 2)
|
|
|
+ error = imEngine.Process(EnhancedFilePathList[args.PageRange[i]]);
|
|
|
+
|
|
|
+ if (imEngine.OCRResultList == null)
|
|
|
+ return;
|
|
|
+
|
|
|
+ List<KeyValuePair<Rect, string>> RectList = new List<KeyValuePair<Rect, string>>();
|
|
|
+ foreach (COCRResult ocrResult in imEngine.OCRResultList)
|
|
|
{
|
|
|
- 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);
|
|
|
+ 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];
|
|
|
+ RectList.Add(new KeyValuePair<Rect, string>(new Rect(left, top, right - left, bottom - top), ocrResult.text));
|
|
|
+ }
|
|
|
+ cacahe.Add(args.PageRange[i], RectList);
|
|
|
}
|
|
|
- else
|
|
|
+ if (cacahe.Count > 0)
|
|
|
{
|
|
|
- TextRectList = null;
|
|
|
+ if (cacahe.ContainsKey(PDFViewer.CurrentIndex))
|
|
|
+ {
|
|
|
+ TextRectList = cacahe[PDFViewer.CurrentIndex];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ TextRectList = null;
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- imEngine.Release();
|
|
|
+ imEngine.Release();
|
|
|
+ });
|
|
|
}
|
|
|
}
|
|
|
}
|