Browse Source

Merge branch 'dev' of http://git.kdan.cc:8865/Windows/PDFOffice_Windows_exe into dev

ZhouJieSheng 2 years ago
parent
commit
b053ef4665

+ 60 - 0
PDF Office/CustomControl/ScanViewControl/CustomPanel.cs

@@ -26,6 +26,53 @@ namespace PDF_Office.CustomControl.ScanViewControl
         private int RawWidth = 0;
         private int RawHeight = 0;
 
+
+        /// <summary>
+        /// 展示用的背景图
+        /// </summary>
+        public WriteableBitmap BGImage
+        {
+            get { return (WriteableBitmap)GetValue(BGImageProperty); }
+            set 
+            { 
+                SetValue(BGImageProperty, value); 
+            }
+        }
+
+        // Using a DependencyProperty as the backing store for BGImage.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty BGImageProperty =
+            DependencyProperty.Register("BGImage", typeof(WriteableBitmap), typeof(CustomPanel), new FrameworkPropertyMetadata(null,new PropertyChangedCallback(OnImageChanged)));
+
+        private static void OnImageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+        {
+            if ((e.NewValue as  WriteableBitmap)!=null)
+            {
+                (d as CustomPanel).SetBackgroundImage(e.NewValue as WriteableBitmap);
+            }
+        }
+
+
+        /// <summary>
+        ///  当前页的OCR结果
+        /// </summary>
+        public List<KeyValuePair<Rect, string>> OCRTextRectList
+        {
+            get { return (List<KeyValuePair<Rect, string>>)GetValue(OCRTextRectListProperty); }
+            set { SetValue(OCRTextRectListProperty, value); }
+        }
+
+        // Using a DependencyProperty as the backing store for OCRTextRectList.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty OCRTextRectListProperty =
+            DependencyProperty.Register("OCRTextRectList", typeof(List<KeyValuePair<Rect, string>>), typeof(CustomPanel), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnOCRChanged)));
+
+        private static void OnOCRChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+        {
+            if ((e.NewValue as List<KeyValuePair<Rect, string>>) != null)
+            {
+                (d as CustomPanel).SetTextsRects(e.NewValue as List<KeyValuePair<Rect, string>>);
+            }
+        }
+
         private List<CustomDraw> VisualControlList = new List<CustomDraw>();
         protected override int VisualChildrenCount => VisualControlList.Count;
         protected override Visual GetVisualChild(int index)
@@ -113,6 +160,19 @@ namespace PDF_Office.CustomControl.ScanViewControl
                 RawHeight = BackgroundImage.PixelHeight;
                 BackgroundWidth = DpiHelpers.GetDpiUnrelatedNum(BackgroundImage.PixelWidth);
                 BackgroundHeight = DpiHelpers.GetDpiUnrelatedNum(BackgroundImage.PixelHeight);
+
+                double scale = Math.Min(ActualWidth / BackgroundWidth, ActualHeight / BackgroundHeight);
+                scale = Math.Min(scale, 1);
+                int drawWidth = (int)(scale * BackgroundWidth);
+                int drawHeight = (int)(scale * BackgroundHeight);
+                if (drawWidth < 10 || drawHeight < 10)
+                {
+                    return;
+                }
+                ScaleRate = scale;
+                OffsetPos.X = (int)((ActualWidth - drawWidth) / 2);
+                OffsetPos.Y = (int)((ActualHeight - drawHeight) / 2);
+
                 InvalidateVisual();
             }
         }

+ 4 - 5
PDF Office/EventAggregators/ScanEvent.cs

@@ -22,7 +22,6 @@ namespace PDF_Office.EventAggregators
 
     public enum ScanLanguageMode
     {
-        Unknown,
         /// <summary>
         /// 简体中文
         /// </summary>
@@ -45,11 +44,11 @@ namespace PDF_Office.EventAggregators
             }
             set
             {
-                Mode = value;
+                mode = value;
             }
         }
 
-        private ScanLanguageMode scanLanguage = ScanLanguageMode.Unknown;
+        private ScanLanguageMode scanLanguage = ScanLanguageMode.English;
         public ScanLanguageMode ScanLanguage 
         { 
             get
@@ -62,8 +61,8 @@ namespace PDF_Office.EventAggregators
             } 
         }
 
-        private string pageRange = "";
-        public string PageRange 
+        private List<int> pageRange ;
+        public List<int> PageRange 
         {
             get
             {

+ 15 - 8
PDF Office/ViewModels/PropertyPanel/Scan/ScanPropertyPanelViewModel.cs

@@ -26,9 +26,9 @@ namespace PDF_Office.ViewModels.PropertyPanel.Scan
             }
         }
 
-        private string setPageRange;
+        private List<int> setPageRange;
 
-        public string SetPageRange
+        public List<int> SetPageRange
         {
             get { return setPageRange; }
             set
@@ -67,19 +67,26 @@ namespace PDF_Office.ViewModels.PropertyPanel.Scan
         }
         public ScanPropertyPanelViewModel(IEventAggregator eventAggregator)
         {
+            setPageRange = new List<int>();
             events = eventAggregator;
             OCRCommand = new DelegateCommand(OCR);
         }
         private void OCR()
         {
-            events.GetEvent<ScanEvent>().Publish(new ScanEventArgs()
+            try
+            {
+                events.GetEvent<ScanEvent>().Publish(new ScanEventArgs()
+                {
+                    Unicode = App.mainWindowViewModel.SelectedItem.Unicode,
+                    PageRange = SetPageRange,
+                    Mode = ScanMode.OCR,
+                    ScanLanguage = ScanLanguageMode.ChineseS
+                }
+                );
+            }
+            catch (Exception e)
             {
-                Unicode = App.mainWindowViewModel.SelectedItem.Unicode,
-                PageRange = SetPageRange,
-                Mode = ScanMode.OCR,
-                ScanLanguage= ScanLanguageMode.ChineseS
             }
-            ); 
         }
     }
 }

+ 146 - 2
PDF Office/ViewModels/Scan/ScanViwerViewModel.cs

@@ -1,21 +1,55 @@
-using ComPDFKitViewer;
+using ComDocumentAIKit;
+using ComPDFKit.PDFDocument;
+using ComPDFKit.PDFPage;
+using ComPDFKitViewer;
 using ComPDFKitViewer.PdfViewer;
 using PDF_Office.EventAggregators;
+using PDF_Office.Helper;
 using PDF_Office.Model;
 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.Media;
+using System.Windows.Media.Imaging;
 
 namespace PDF_Office.ViewModels.Scan
 {
     class ScanViwerViewModel : BindableBase, INavigationAware
     {
         private CPDFViewer PDFViewer;
+
+        private WriteableBitmap bgImage;
+
+        public WriteableBitmap BgImage
+        {
+            get { return bgImage; }
+            set
+            {
+                SetProperty(ref bgImage, value);
+            }
+        }
+
+        private List<KeyValuePair<Rect, string>> textRectList;
+
+        public List<KeyValuePair<Rect, string>> TextRectList
+        {
+            get { return textRectList; }
+            set
+            {
+                SetProperty(ref textRectList, value);
+            }
+        }
+
+        private Dictionary<int, List<KeyValuePair<Rect, string>>> cacahe;
+
+
         public bool IsNavigationTarget(NavigationContext navigationContext)
         {
             return true;
@@ -36,6 +70,17 @@ namespace PDF_Office.ViewModels.Scan
             else
             {
                 PDFViewer.InfoChanged += PDFViewer_InfoChanged;
+
+                PDFViewer.ChangeViewMode(ViewMode.Single);
+                //第一次进入获取当前预览显示内容
+                CPDFPage pdfPage = PDFViewer.Document.PageAtIndex(PDFViewer.CurrentIndex);
+                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);
+                BgImage = new WriteableBitmap(renderWidth, renderHeight, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
+                BgImage.WritePixels(new Int32Rect(0, 0, renderWidth, renderHeight), renderData, BgImage.BackBufferStride, 0);
             }
         }
 
@@ -46,7 +91,26 @@ namespace PDF_Office.ViewModels.Scan
                 RenderData renderData = e.Value as RenderData;
                 if (renderData != null)
                 {
-                    var CurrentPage = renderData.PageIndex;
+                    CPDFPage pdfPage = PDFViewer.Document.PageAtIndex(renderData.PageIndex - 1);
+                    float zoom = (float)(DpiHelpers.Dpi / 72D);
+                    int renderWidth = (int)(pdfPage.PageSize.Width * zoom);
+                    int renderHeight = (int)(pdfPage.PageSize.Height * zoom);
+                    byte[] Data = new byte[renderWidth * renderHeight * 4];
+                    pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(0, 0, renderWidth, renderHeight), 0xFFFFFFFF, Data, 0);
+                    BgImage = new WriteableBitmap(renderWidth, renderHeight, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
+                    BgImage.WritePixels(new Int32Rect(0, 0, renderWidth, renderHeight), Data, BgImage.BackBufferStride, 0);
+
+                    if (cacahe.Count > 0)
+                    {
+                        if (cacahe.ContainsKey(renderData.PageIndex - 1))
+                        {
+                            TextRectList = cacahe[renderData.PageIndex - 1];
+                        }
+                        else
+                        {
+                            TextRectList = null;
+                        }
+                    }
                 }
             }
         }
@@ -54,6 +118,7 @@ namespace PDF_Office.ViewModels.Scan
         public ScanViwerViewModel(IEventAggregator eventAggregator)
         {
             eventAggregator.GetEvent<ScanEvent>().Subscribe(ChangeScanMode, e => e.Unicode == App.mainWindowViewModel.SelectedItem.Unicode);
+            cacahe = new Dictionary<int, List<KeyValuePair<Rect, string>>>();
         }
         /// <summary>
         /// 根据Vm事件通知处理OCR与区域识别事件
@@ -66,6 +131,7 @@ namespace PDF_Office.ViewModels.Scan
                 case ScanMode.Unknown:
                     break;
                 case ScanMode.OCR:
+                    OCRProcess(e);
                     break;
                 case ScanMode.Area:
                     break;
@@ -74,5 +140,83 @@ namespace PDF_Office.ViewModels.Scan
             }
         }
 
+        private void OCRProcess(ScanEventArgs args)
+        {
+            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;
+
+            CErrorCode error = imEngine.SetModel((COCRLanguage)args.ScanLanguage);
+
+            cacahe.Clear();
+
+            for (int i = 0; i < args.PageRange.Count; i++)
+            {
+                string pageImagePath = Path.Combine(path, args.PageRange[i].ToString());
+                string pageEnhancePath = Path.Combine(EnhancePath, args.PageRange[i].ToString() + ".png");
+
+                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();
+                }
+
+                //File.Create(pageEnhancePath);
+                //error = imEngine.Process(pageImagePath, pageEnhancePath);
+                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, 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];
+                }
+                else
+                {
+                    TextRectList = null;
+                }
+            }
+            imEngine.Release();
+        }
     }
 }

+ 1 - 8
PDF Office/ViewModels/Tools/ScanContentViewModel.cs

@@ -184,6 +184,7 @@ namespace PDF_Office.ViewModels.Tools
                         textRectList.Add(new KeyValuePair<Rect, string>(new Rect(left, top, right - left, bottom - top), ocrResult.text));
                     }
 
+                    //写入数据
                     CPDFEditPage editPage = pdfPage.GetEditPage();
 
                     editPage.BeginEdit(CPDFEditType.EditText);
@@ -216,16 +217,8 @@ namespace PDF_Office.ViewModels.Tools
                     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);
-
         }
 
     }

+ 3 - 3
PDF Office/Views/PropertyPanel/Scan/ScanPropertyPanel.xaml

@@ -3,7 +3,7 @@
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
-             xmlns:local="clr-namespace:PDF_Office.Views.PropertyPanel.Scan" xmlns:customcontrol="clr-namespace:PDF_Office.CustomControl"
+             xmlns:local="clr-namespace:PDF_Office.Views.PropertyPanel.Scan" xmlns:customcontrol="clr-namespace:PDF_Office.CustomControl" xmlns:scan="clr-namespace:PDF_Office.ViewModels.PropertyPanel.Scan" d:DataContext="{d:DesignInstance Type=scan:ScanPropertyPanelViewModel}"
              mc:Ignorable="d" >
     <Grid>
         <StackPanel>
@@ -28,8 +28,8 @@
                 <TextBlock Text="Chinese Traditional"/>
                 <TextBlock Text="French"/>
             </ComboBox>
-            <customcontrol:WritableComboBox SelectedIndex="0" MaxPageRange="{Binding PageCount}" Text="{Binding SetPageRange,Mode=TwoWay}"/>
-            <Button Content="OCR"/>
+            <customcontrol:WritableComboBox SelectedIndex="0" MaxPageRange="{Binding PageCount}" PageIndexList="{Binding SetPageRange,Mode=TwoWay}"/>
+            <Button Content="OCR" Command="{Binding OCRCommand}"/>
         </StackPanel>
     </Grid>
 </UserControl>

+ 2 - 2
PDF Office/Views/Scan/ScanViwer.xaml

@@ -3,9 +3,9 @@
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
-             xmlns:local="clr-namespace:PDF_Office.Views.Scan" xmlns:scanviewcontrol="clr-namespace:PDF_Office.CustomControl.ScanViewControl"
+             xmlns:local="clr-namespace:PDF_Office.Views.Scan" xmlns:scanviewcontrol="clr-namespace:PDF_Office.CustomControl.ScanViewControl" xmlns:scan="clr-namespace:PDF_Office.ViewModels.Scan" d:DataContext="{d:DesignInstance Type=scan:ScanViwerViewModel}"
              mc:Ignorable="d" >
     <Grid >
-        <scanviewcontrol:CustomPanel/>
+        <scanviewcontrol:CustomPanel BGImage="{Binding BgImage}" OCRTextRectList="{Binding TextRectList}"/>
     </Grid>
 </UserControl>