|
@@ -1,18 +1,33 @@
|
|
-using ComPDFKitViewer.PdfViewer;
|
|
|
|
|
|
+using ComDocumentAIKit;
|
|
|
|
+using ComPDFKit.PDFDocument;
|
|
|
|
+using ComPDFKit.PDFPage;
|
|
|
|
+using ComPDFKit.PDFPage.Edit;
|
|
|
|
+using ComPDFKitViewer.PdfViewer;
|
|
|
|
+using PDF_Office.Helper;
|
|
using PDF_Office.Model;
|
|
using PDF_Office.Model;
|
|
|
|
+using Prism.Commands;
|
|
using Prism.Mvvm;
|
|
using Prism.Mvvm;
|
|
using Prism.Regions;
|
|
using Prism.Regions;
|
|
using System;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
|
|
+using System.IO;
|
|
using System.Linq;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
|
|
+using System.Windows;
|
|
|
|
+using System.Windows.Controls;
|
|
|
|
+using System.Windows.Media;
|
|
|
|
+using System.Windows.Media.Imaging;
|
|
|
|
|
|
namespace PDF_Office.ViewModels.Tools
|
|
namespace PDF_Office.ViewModels.Tools
|
|
{
|
|
{
|
|
class ScanContentViewModel : BindableBase, INavigationAware
|
|
class ScanContentViewModel : BindableBase, INavigationAware
|
|
{
|
|
{
|
|
|
|
+ #region 模板与导航相关内容
|
|
|
|
+
|
|
private CPDFViewer PDFViewer;
|
|
private CPDFViewer PDFViewer;
|
|
|
|
+
|
|
|
|
+ public ViewContentViewModel viewContentViewModel;
|
|
public bool IsNavigationTarget(NavigationContext navigationContext)
|
|
public bool IsNavigationTarget(NavigationContext navigationContext)
|
|
{
|
|
{
|
|
return true;
|
|
return true;
|
|
@@ -20,12 +35,198 @@ namespace PDF_Office.ViewModels.Tools
|
|
|
|
|
|
public void OnNavigatedFrom(NavigationContext navigationContext)
|
|
public void OnNavigatedFrom(NavigationContext navigationContext)
|
|
{
|
|
{
|
|
- return ;
|
|
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
|
|
|
|
public void OnNavigatedTo(NavigationContext navigationContext)
|
|
public void OnNavigatedTo(NavigationContext navigationContext)
|
|
{
|
|
{
|
|
|
|
+ navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
|
|
navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
|
|
navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ #endregion
|
|
|
|
+
|
|
|
|
+ private IRegionManager regions;
|
|
|
|
+ public DelegateCommand NavigationCommand { get; set; }
|
|
|
|
+ public DelegateCommand EnhancedCommand { get; set; }
|
|
|
|
+ public List<string> EnhancedFilePathList;
|
|
|
|
+ public ScanContentViewModel(IRegionManager regionManager)
|
|
|
|
+ {
|
|
|
|
+ regions = regionManager;
|
|
|
|
+ NavigationCommand = new DelegateCommand(BtnNavigation);
|
|
|
|
+ EnhancedCommand = new DelegateCommand(EnhancedScan);
|
|
|
|
+ EnhancedFilePathList = new List<string>();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ ///导航到OCR的侧边栏
|
|
|
|
+ /// </summary>
|
|
|
|
+ private void BtnNavigation()
|
|
|
|
+ {
|
|
|
|
+ //替换属性面板的内容
|
|
|
|
+ NavigationParameters parameters = new NavigationParameters();
|
|
|
|
+ parameters.Add(ParameterNames.PDFViewer, PDFViewer);
|
|
|
|
+ parameters.Add(ParameterNames.PropertyPanelContentViewModel, null);
|
|
|
|
+ regions.RequestNavigate(RegionNames.PropertyRegionName, "ScanPropertyPanel", parameters);
|
|
|
|
+ //显示属性面板
|
|
|
|
+ viewContentViewModel.IsPropertyOpen = true;
|
|
|
|
+ }
|
|
|
|
+ private void EnhancedScan()
|
|
|
|
+ {
|
|
|
|
+ CPDFDocument CurrentDoc = PDFViewer.Document;
|
|
|
|
+ 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);
|
|
|
|
+
|
|
|
|
+ CIMEngine imEngine = new CIMEngine(App.modelFolderPath);
|
|
|
|
+ CErrorCode error = imEngine.SetModel();
|
|
|
|
+ for (int i = 0; i < CurrentDoc.PageCount; i++)
|
|
|
|
+ {
|
|
|
|
+ string pageImagePath = Path.Combine(path, i.ToString());
|
|
|
|
+ string pageEnhancePath = Path.Combine(EnhancePath, i.ToString() + ".png");
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ 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
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void OCREnhancedScan()
|
|
|
|
+ {
|
|
|
|
+ CPDFDocument EnhanceDoc = CPDFDocument.CreateDocument();
|
|
|
|
+ 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;
|
|
|
|
+ //CurrentDoc.FilePath;
|
|
|
|
+
|
|
|
|
+ CErrorCode error = imEngine.SetModel(COCRLanguage.Chinese_S);
|
|
|
|
+ //CErrorCode error = imEngine.SetModel();
|
|
|
|
+ for (int i = 0; i < 1; i++)
|
|
|
|
+ {
|
|
|
|
+ string pageImagePath = Path.Combine(path, i.ToString());
|
|
|
|
+ string pageEnhancePath = Path.Combine(EnhancePath, i.ToString() + ".png");
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ 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);
|
|
|
|
+ error = imEngine.Process(pageImagePath);
|
|
|
|
+
|
|
|
|
+ if (imEngine.OCRResultList == null)
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ List<KeyValuePair<Rect, string>> textRectList = 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);
|
|
|
|
+
|
|
|
|
+ textRectList.Add(new KeyValuePair<Rect, string>(new Rect(left, top, right - left, bottom - top), ocrResult.text));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ CPDFEditPage editPage = pdfPage.GetEditPage();
|
|
|
|
+
|
|
|
|
+ editPage.BeginEdit(CPDFEditType.EditText);
|
|
|
|
+ for (int y = textRectList.Count - 1; y >= 0; y--)
|
|
|
|
+ {
|
|
|
|
+ KeyValuePair<Rect, string> textBlock = textRectList[y];
|
|
|
|
+ int fontSize = 1;
|
|
|
|
+ Rect pdfRect = DpiHelpers.GetRawRect(textBlock.Key);
|
|
|
|
+
|
|
|
|
+ for (; fontSize < 100; fontSize++)
|
|
|
|
+ {
|
|
|
|
+ Rect cmpRect = pdfPage.GetTextContentRect(fontSize, "Helvetica", textBlock.Value);
|
|
|
|
+ if (cmpRect.Width > pdfRect.Width || cmpRect.Height > pdfRect.Height)
|
|
|
|
+ {
|
|
|
|
+ fontSize -= 1;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ CPDFEditTextArea editArea = editPage.CreateNewTextArea(pdfRect, "Helvetica", fontSize, new byte[] { 0, 0, 0 });
|
|
|
|
+
|
|
|
|
+ bool x = editArea.SetTextAreaAlign(TextAlignType.AlignChar);
|
|
|
|
+ editArea.InsertText(textBlock.Value);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ editPage.EndEdit();
|
|
|
|
+ }
|
|
|
|
+ catch (Exception e)
|
|
|
|
+ {
|
|
|
|
+ throw;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //error = imEngine.Process("C:/test/9.png", "C:/test/9IM.png");
|
|
|
|
+ imEngine.Release();
|
|
|
|
+ PDFViewer.InvalidChildVisual(true);
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ //更新预览
|
|
|
|
+ //int PageIndex = PDFViewer.CurrentIndex;
|
|
|
|
+ //PDFViewer.InitDocument(EnhanceDoc);
|
|
|
|
+ //PDFViewer.GoToPage(PageIndex);
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|