123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- using ComDocumentAIKit;
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using ComPDFKit.PDFPage.Edit;
- using ComPDFKitViewer.PdfViewer;
- using PDF_Master.CustomControl;
- using PDF_Master.EventAggregators;
- using PDF_Master.Helper;
- using PDF_Master.Model;
- using Prism.Commands;
- 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.Controls;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- namespace PDF_Master.ViewModels.Tools
- {
- class ScanContentViewModel : BindableBase, INavigationAware
- {
- #region 模板与导航相关内容
- private CPDFViewer PDFViewer;
- public ViewContentViewModel viewContentViewModel;
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- return;
- }
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
- navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
- }
- #endregion
- private IRegionManager regions;
- public IEventAggregator events;
- public DelegateCommand NavigationCommand { get; set; }
- public DelegateCommand<object> EnhancedCommand { get; set; }
- public ScanContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
- {
- regions = regionManager;
- events = eventAggregator;
- NavigationCommand = new DelegateCommand(BtnNavigation);
- EnhancedCommand = new DelegateCommand<object>(EnhancedScan);
- }
- /// <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(object button)
- {
- ImageRadioButton imageRadioButton = button as ImageRadioButton;
- if (imageRadioButton==null)
- {
- return;
- }
- if ((bool)!imageRadioButton.IsChecked)
- {
- return;
- }
- try
- {
- List<int> SetPageRange = new List<int>();
- CommonHelper.GetPagesInRange(ref SetPageRange, "1-" + PDFViewer.Document.PageCount, PDFViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' });
- events.GetEvent<ScanEvent>().Publish(new ScanEventArgs()
- {
- Unicode = App.mainWindowViewModel.SelectedItem.Unicode,
- PageRange = SetPageRange,
- Mode = ScanMode.Enhanced,
- ScanLanguage = ScanLanguageMode.ChineseS
- }
- );
- }
- catch (Exception e)
- {
- }
- }
- 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);
- //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;
- }
- }
- imEngine.Release();
- return;
- }
- }
- }
|