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(ParameterNames.ViewContentViewModel, out viewContentViewModel); navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); } #endregion private IRegionManager regions; public IEventAggregator events; public DelegateCommand NavigationCommand { get; set; } public DelegateCommand EnhancedCommand { get; set; } public ScanContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator) { regions = regionManager; events = eventAggregator; NavigationCommand = new DelegateCommand(BtnNavigation); EnhancedCommand = new DelegateCommand(EnhancedScan); } /// ///导航到OCR的侧边栏 /// 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 SetPageRange = new List(); CommonHelper.GetPagesInRange(ref SetPageRange, "1-" + PDFViewer.Document.PageCount, PDFViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }); events.GetEvent().Publish(new ScanEventArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, PageRange = SetPageRange, Mode = ScanMode.Enhanced, ScanLanguage = ScanLanguageMode.ChineseS } ); } 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.COCRLanguageChinese); //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> textRectList = new List>(); foreach (COCRResult ocrResult in imEngine.OCRResultList) { List rectPoints = new List(); 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(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 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 { throw; } } imEngine.Release(); return; } } }