Browse Source

综合-增强扫描和OCR部分实现

zhuyi 2 years ago
parent
commit
783f6afa14

+ 4 - 7
PDF Office/CustomControl/WritableComboBox.xaml.cs

@@ -109,9 +109,7 @@ namespace PDF_Office.CustomControl
         public List<int> PageIndexList
         {
             get { return (List<int>)GetValue(PageIndexListProperty); }
-            set { 
-                SetValue(PageIndexListProperty, value);
-            }
+            set { SetValue(PageIndexListProperty, value); }
         }
 
         // Using a DependencyProperty as the backing store for PageIndexList.  This enables animation, styling, binding, etc...
@@ -135,11 +133,11 @@ namespace PDF_Office.CustomControl
             int value = Convert.ToInt32(e.NewValue);
             if (value>0)
             {
-                (d as WritableComboBox).t();
+                (d as WritableComboBox).UpDataPagesInRange();
             }
         }
 
-        private void t()
+        private void UpDataPagesInRange()
         {
 
             if ((writableComboBox.SelectedItem as ComboBoxItem).Tag != null)
@@ -210,7 +208,6 @@ namespace PDF_Office.CustomControl
                 }
 
             }
-            SetCurrentValue(PageIndexListProperty, PageIndexList);
         }
         public ItemCollection Items
         {
@@ -382,7 +379,7 @@ namespace PDF_Office.CustomControl
 
         private void UserControl_Loaded(object sender, RoutedEventArgs e)
         {
-            var x = PageIndexList;
+            UpDataPagesInRange();
         }
     }
 }

+ 2 - 2
PDF Office/SDKLisence.xml

@@ -1,6 +1,6 @@
 <Lisences
-	devKey = "sTHSivIW4YnZQavIYDLVw4vaFcN5DQTUtRGrVyv5p9aeaHX3GirG/MBxl6Wy9TJbNOwR4CU6LPjHmGe3Po3OtBCPqIRheam9+LomScw3AvoUTHFlG1134e2J4enF43WJQ6PyOnjwZqsG1kUdlBPNztkoPzDAE2pQKgZWf6V2i34="
-    devSecret = "mG0c3O3Mzeu5dkZJW3gpqq9uA7o7EGQveSC38Q8TK4gQurxTxGuBlGAhs0P1mD3X3bHT+AHfcLiymaqE4DY7kTFHoPs9I3tl5ErS+BHdzHRhrp9sGpqfp0B228KI+IMTu4aGVjtYuk+Uxs/kosIBw1367/WkJ00tM7U7tttD6cfaiHpW8pgW2aur3AxuQIBBLuJD5gcmNxTYW3KA32JLG8alfyXjKFVS3gka9bngvwfZ7GuNXybjKlmz1tnN6iVHpnbSj+xOHhWEperY4HHrSg=="
+	devKey = "BK8BcXWm+YbGRpEONe43pEJXhshAiAwWoBQgwlEUnxFBkCVal4AYW01RnbuwHCdkktvy9sAYs/SUflLR+KOMayg6z2TE2G3OhIXf95sN8k1sk947zrPGeHPtHx7gMaurYrscLDAumvtymhg4rMTg7XFKfBtchlAi0bE+wPTsqa8="
+  devSecret = "mG0c3O3Mzeu5dkZJW3gpqq9uA7o7EGQveSC38Q8TK4iYSXB8erGstkUyaynN7k8ocNCYAAzc9CPxiym2TlqeSUWpLOaOv47bs7x+UqfQzn9hrp9sGpqfp0B228KI+IMTu4aGVjtYuk+Uxs/kosIBwyVLBY93oKLBB8UJg4t07605BGbaBKUlDopA0iGq1HSnRAJhqHxGgIZ+m3BxAFwgabp2OdkT7I9klGmA/4KertEmsKlSJ3WFqmDWhwfT73yCYDNQpuarUAQxiS1PJu8kUw=="
 	userKey = "iBPRM/Tz8b6Z1G2GQt52X7hiNCGfVYXztnPjalgrgARvqfKV6lFNH8QeScTzBRYI8GGFpwelfgh790Kd9JmL7V4adI1jCiFHUT2DLT7QucxY5Nkgys2aJItQS482Ck2G2Xf8gNgojxYxRt65o/MEzkj93foj8qIdfHagXsSorSs="
 	userSecret="mG0c3O3Mzeu5dkZJW3gpqq9uA7o7EGQveSC38Q8TK4ivEHOmPIqbfhpDnKKj+7Ymj2rXQvfZRmke06HMV+3tt064G64WjPW8+EbGCNZaAh1hrp9sGpqfp0B228KI+IMTu4aGVjtYuk+Uxs/kosIBw1FKJi6HYB+DuugQyaqI2prfej861QnJrU4s2T/npZK/"
 	ConverterDevKey="ErjZKxcU3SPvUohGZo0CBgy0XdkAsZqUYqCTbfn1AYsCQ6mxCucx61UPqLT7MDQ0n+5/oKnWY9SSk8Xv5pJtYYHD87HRdYNG+QNCaoPGHulyfVxGllmzU71U7WjflfZBFvhg/UQOFiz3OsAVKz4k2YkXPzQgNaS3glym42PVpj4="

+ 130 - 116
PDF Office/ViewModels/Scan/ScanViwerViewModel.cs

@@ -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();
+            });
         }
     }
 }

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

@@ -28,7 +28,7 @@
                 <TextBlock Text="Chinese Traditional"/>
                 <TextBlock Text="French"/>
             </ComboBox>
-            <customcontrol:WritableComboBox x:Name="tt" SelectedIndex="0" MaxPageRange="{Binding PageCount}" PageIndexList="{Binding SetPageRange,Mode=TwoWay}" Loaded="WritableComboBox_Loaded"/>
+            <customcontrol:WritableComboBox SelectedIndex="0" MaxPageRange="{Binding PageCount}" PageIndexList="{Binding SetPageRange,Mode=TwoWay}"/>
             <Button Content="OCR" Command="{Binding OCRCommand}"/>
         </StackPanel>
     </Grid>

+ 1 - 8
PDF Office/Views/PropertyPanel/Scan/ScanPropertyPanel.xaml.cs

@@ -1,5 +1,4 @@
-using PDF_Office.CustomControl;
-using System;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -25,11 +24,5 @@ namespace PDF_Office.Views.PropertyPanel.Scan
         {
             InitializeComponent();
         }
-
-        private void WritableComboBox_Loaded(object sender, RoutedEventArgs e)
-        {
-            var x=tt.PageIndexList;
-            tt.GetBindingExpression(WritableComboBox.PageIndexListProperty).UpdateSource();
-        }
     }
 }