Explorar el Código

OCR-框选与区域识别

zhuyi hace 2 años
padre
commit
c65651f257

+ 30 - 1
PDF Office/CustomControl/ScanViewControl/CustomPanel.cs

@@ -75,6 +75,27 @@ namespace PDF_Office.CustomControl.ScanViewControl
             }
         }
 
+
+
+        public bool IsShowRect
+        {
+            get { return (bool)GetValue(IsShowRectProperty); }
+            set { SetValue(IsShowRectProperty, value); }
+        }
+
+        // Using a DependencyProperty as the backing store for IsShowRect.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty IsShowRectProperty =
+            DependencyProperty.Register("IsShowRect", typeof(bool), typeof(CustomPanel), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnShowRectChanged)));
+
+        private static void OnShowRectChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+        {
+            if (Convert.ToBoolean(e.NewValue)==false)
+            {
+                (d as CustomPanel).SetRect(new Rect(0,0,0,0));
+                (d as CustomPanel).RectDraw();
+            }
+        }
+
         private List<CustomDraw> VisualControlList = new List<CustomDraw>();
 
         private DrawRect RectObject { get; set; } = new DrawRect();
@@ -292,7 +313,7 @@ namespace PDF_Office.CustomControl.ScanViewControl
         }
         public bool RectMouseLeftButtonDown(Point clickPoint)
         {
-            return RectObject.MouseLeftButtonDown(clickPoint,this);
+            return RectObject.MouseLeftButtonDown(clickPoint, this);
         }
         public void RectSetMaxRect(Rect rect)
         {
@@ -302,5 +323,13 @@ namespace PDF_Office.CustomControl.ScanViewControl
         {
             RectObject.Draw();
         }
+        public void ClearDraw()
+        {
+            RectObject.ClearDraw();
+        }
+        public Rect GetClientRect()
+        {
+            return RectObject.GetClientRect();
+        }
     }
 }

+ 21 - 11
PDF Office/CustomControl/ScanViewControl/DrawRect.cs

@@ -173,6 +173,11 @@ namespace PDF_Office.CustomControl.ScanViewControl
             return false;
         }
 
+        public void test(double x)
+        {
+            ClientRect = new Rect(ClientRect.X , ClientRect.Y , ClientRect.Width * x, ClientRect.Height * x);
+        }
+
         /// <summary>
         /// 鼠标松开时调用,存储鼠标状态
         /// </summary>
@@ -212,6 +217,11 @@ namespace PDF_Office.CustomControl.ScanViewControl
             ClientRect = newRect;
         }
 
+        public Rect GetClientRect()
+        {
+            return ClientRect;
+        }
+
         /// <summary>
         /// 获取当前命中状态
         /// </summary>
@@ -398,11 +408,11 @@ namespace PDF_Office.CustomControl.ScanViewControl
                 case PointControlType.LeftTop:
                     {
                         double X = centerPoint.X - xMove;
-                        double Width =SnapClientRect.Width + SnapClientRect.X - centerPoint.X + xMove;
+                        double Width = SnapClientRect.Width + SnapClientRect.X - centerPoint.X + xMove;
                         if (Width < 10)
                         {
                             Width = 10;
-                            X = SnapClientRect.X + SnapClientRect.Width- 10;
+                            X = SnapClientRect.X + SnapClientRect.Width - 10;
                         }
                         double Y = centerPoint.Y - yMove;
                         double Height = SnapClientRect.Height + SnapClientRect.Y - centerPoint.Y + yMove;
@@ -444,7 +454,7 @@ namespace PDF_Office.CustomControl.ScanViewControl
                         if (Height < 10)
                         {
                             Height = 10;
-                            Y = SnapClientRect.Y ;
+                            Y = SnapClientRect.Y;
                         }
                         ClientRect = new Rect(new Point(X, Y),
                             new Size(Width, Height));
@@ -468,7 +478,7 @@ namespace PDF_Office.CustomControl.ScanViewControl
                 case PointControlType.RightBottom:
                     {
                         double X = SnapClientRect.Left;
-                        double Width = SnapClientRect.Width + SnapClientRect.X - centerPoint.X -xMove;
+                        double Width = SnapClientRect.Width + SnapClientRect.X - centerPoint.X - xMove;
                         if (Width < 10)
                         {
                             Width = 10;
@@ -537,7 +547,7 @@ namespace PDF_Office.CustomControl.ScanViewControl
                 case PointControlType.Body:
                     {
                         moveVector = mousePoint - MouseDownPoint;
-                        ClientRect = new Rect(SnapClientRect.Left - xMove, SnapClientRect.Top  -yMove, SnapClientRect.Width , SnapClientRect.Height);
+                        ClientRect = new Rect(SnapClientRect.Left - xMove, SnapClientRect.Top - yMove, SnapClientRect.Width, SnapClientRect.Height);
                     }
                     break;
                 default:
@@ -550,7 +560,7 @@ namespace PDF_Office.CustomControl.ScanViewControl
                 ClientRect = new Rect
                (MaxRect.X,
                ClientRect.Y,
-               ClientRect.Width + MaxRect.X - ClientRect.X,
+               ClientRect.Width,
                ClientRect.Height);
             }
             if (ClientRect.Y < MaxRect.Y)
@@ -559,23 +569,23 @@ namespace PDF_Office.CustomControl.ScanViewControl
                 (ClientRect.X,
                 MaxRect.Y,
                 ClientRect.Width,
-                ClientRect.Height - ClientRect.Y + MaxRect.Y);
+                ClientRect.Height);
             }
             if (ClientRect.Right > MaxRect.Right)
             {
                 ClientRect = new Rect
-                (ClientRect.X,
+                (MaxRect.Right - ClientRect.Width,
                 ClientRect.Y,
-                MaxRect.Right - ClientRect.X,
+                ClientRect.Width,
                 ClientRect.Height);
             }
             if (ClientRect.Bottom > MaxRect.Bottom)
             {
                 ClientRect = new Rect
                 (ClientRect.X,
-                ClientRect.Y,
+                MaxRect.Bottom - ClientRect.Height,
                 ClientRect.Width,
-                MaxRect.Bottom - ClientRect.Y);
+                ClientRect.Height);
             }
 
             Console.WriteLine(ClientRect.ToString());

+ 39 - 0
PDF Office/ViewModels/PropertyPanel/Scan/ScanPropertyPanelViewModel.cs

@@ -44,6 +44,8 @@ namespace PDF_Office.ViewModels.PropertyPanel.Scan
         public IEventAggregator events;
 
         public DelegateCommand OCRCommand { get; set; }
+        public DelegateCommand AreaCommand { get; set; }
+        public DelegateCommand UnknownCommand { get; set; }
         public bool IsNavigationTarget(NavigationContext navigationContext)
         {
             return true;
@@ -71,6 +73,8 @@ namespace PDF_Office.ViewModels.PropertyPanel.Scan
             setPageRange = new List<int>();
             events = eventAggregator;
             OCRCommand = new DelegateCommand(OCR);
+            AreaCommand = new DelegateCommand(Area);
+            UnknownCommand = new DelegateCommand(Unknown);
         }
         private void OCR()
         {
@@ -89,5 +93,40 @@ namespace PDF_Office.ViewModels.PropertyPanel.Scan
             {
             }
         }
+
+        private void Area()
+        {
+            try
+            {
+                events.GetEvent<ScanEvent>().Publish(new ScanEventArgs()
+                {
+                    Unicode = App.mainWindowViewModel.SelectedItem.Unicode,
+                    PageRange = SetPageRange,
+                    Mode = ScanMode.Area,
+                    ScanLanguage = ScanLanguageMode.ChineseS
+                }
+                );
+            }
+            catch (Exception e)
+            {
+            }
+        }
+        private void Unknown()
+        {
+            try
+            {
+                events.GetEvent<ScanEvent>().Publish(new ScanEventArgs()
+                {
+                    Unicode = App.mainWindowViewModel.SelectedItem.Unicode,
+                    PageRange = SetPageRange,
+                    Mode = ScanMode.Unknown,
+                    ScanLanguage = ScanLanguageMode.ChineseS
+                }
+                );
+            }
+            catch (Exception e)
+            {
+            }
+        }
     }
 }

+ 205 - 1
PDF Office/ViewModels/Scan/ScanViwerViewModel.cs

@@ -26,6 +26,7 @@ namespace PDF_Office.ViewModels.Scan
     class ScanViwerViewModel : BindableBase, INavigationAware
     {
         private CPDFViewer PDFViewer;
+        private ViewContentViewModel viewContentViewModel;
 
         private WriteableBitmap bgImage;
 
@@ -62,6 +63,18 @@ namespace PDF_Office.ViewModels.Scan
             }
         }
 
+        private bool isShowRect;
+
+        public bool IsShowRect
+        {
+            get { return isShowRect; }
+            set
+            {
+                SetProperty(ref isShowRect, value);
+            }
+        }
+
+        private COCRLanguage language = COCRLanguage.English;
 
         /// <summary>
         /// 增强扫描结果路径
@@ -99,6 +112,12 @@ namespace PDF_Office.ViewModels.Scan
 
         public void OnNavigatedTo(NavigationContext navigationContext)
         {
+            navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
+            if (viewContentViewModel == null)
+            {
+                return;
+            }
+
             navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
             if (PDFViewer == null)
             {
@@ -155,10 +174,18 @@ namespace PDF_Office.ViewModels.Scan
                             TextRectList = null;
                         }
                     }
+                    ClearDraw();
                 }
             }
         }
 
+        private void ClearDraw()
+        {
+            //笨办法,有好方案随时能换,目前是以触发属性改变事件的方式激活清空
+            IsShowRect = !IsShowRect;
+            IsShowRect = !IsShowRect;
+        }
+
         public ScanViwerViewModel(IEventAggregator eventAggregator)
         {
             CloseTooltipCommand = new DelegateCommand(CloseTooltip);
@@ -168,6 +195,148 @@ namespace PDF_Office.ViewModels.Scan
             cacahe = new Dictionary<int, List<KeyValuePair<Rect, string>>>();
         }
 
+        /// <summary>
+        /// 区域识别
+        /// </summary>
+        public void AreaProcess(Rect AreaRect, Rect ImageRect)
+        {
+            COCREngine imEngine = new COCREngine(App.modelFolderPath);
+
+            string path = App.CachePath.MergeFilePath;
+            string name = Guid.NewGuid().ToString();
+            path = Path.Combine(path, name);
+
+            Directory.CreateDirectory(path);
+
+            CPDFDocument CurrentDoc = PDFViewer.Document;
+
+            CErrorCode error = imEngine.SetModel(language);
+
+            cacahe.Remove(PDFViewer.CurrentIndex);
+            string pageImagePath = Path.Combine(path, "Area");
+            Directory.CreateDirectory(pageImagePath);
+            pageImagePath = Path.Combine(pageImagePath, PDFViewer.CurrentIndex.ToString());
+
+            if (IsEnhanced)
+            {
+                BitmapImage image = new BitmapImage(new Uri(EnhancedFilePathList[PDFViewer.CurrentIndex]));
+                BgImage = new WriteableBitmap(image);
+                double zoomWidth = AreaRect.Width / ImageRect.Width;
+                double zoomHeight = AreaRect.Height / ImageRect.Height;
+                double zoomX = (AreaRect.X - ImageRect.X) / ImageRect.Width;
+                double zoomY = (AreaRect.Y - ImageRect.Y) / ImageRect.Height;
+                Int32Rect rect = new Int32Rect((int)(BgImage.Width*zoomX), (int)(BgImage.Height*zoomY), (int)(BgImage.Width*zoomWidth), (int)(BgImage.Height*zoomHeight));
+                byte[] renderData = new byte[(int)(BgImage.Width * BgImage.Height * 4)];
+
+                BgImage.CopyPixels(rect, renderData, BgImage.BackBufferStride, 0);
+
+                WriteableBitmap bitmap = new WriteableBitmap(rect.Width, rect.Height, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
+                bitmap.WritePixels(new Int32Rect(0, 0, rect.Width, rect.Height), renderData, BgImage.BackBufferStride, 0);
+                BitmapEncoder encoder = new PngBitmapEncoder();
+                encoder.Frames.Add(BitmapFrame.Create(bitmap));
+
+                using (FileStream imageStream = File.Create(pageImagePath))
+                {
+                    encoder.Save(imageStream);
+                    imageStream.Flush();
+                }
+                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 + BgImage.Width * zoomX, top + BgImage.Height * zoomY, right - left, bottom - top), ocrResult.text));
+                }
+                cacahe.Add(PDFViewer.CurrentIndex, RectList);
+
+                if (cacahe.Count > 0)
+                {
+                    if (cacahe.ContainsKey(PDFViewer.CurrentIndex))
+                    {
+                        TextRectList = cacahe[PDFViewer.CurrentIndex];
+                    }
+                    else
+                    {
+                        TextRectList = null;
+                    }
+                }
+            }
+            else
+            {
+                CPDFPage pdfPage = CurrentDoc.PageAtIndex(PDFViewer.CurrentIndex);
+                double zoomWidth = AreaRect.Width / ImageRect.Width;
+                double zoomHeight = AreaRect.Height / ImageRect.Height;
+                double zoomX = (AreaRect.X- ImageRect.X) / ImageRect.Width;
+                double zoomY = (AreaRect.Y- ImageRect.Y) / ImageRect.Height;
+
+                float zoom = (float)(DpiHelpers.Dpi / 72D);
+                int renderWidth = (int)(pdfPage.PageSize.Width* zoomWidth * zoom);
+                int renderHeight = (int)(pdfPage.PageSize.Height* zoomHeight * zoom);
+                int renderX = (int)(pdfPage.PageSize.Width* zoomX * zoom);
+                int renderY = (int)(pdfPage.PageSize.Height* zoomY * zoom);
+                byte[] renderData = new byte[renderWidth * renderHeight * 4];
+                pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(renderX, renderY, 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();
+                }
+
+                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+ renderX, top+ renderY, right - left, bottom - top), ocrResult.text));
+                }
+                cacahe.Add(PDFViewer.CurrentIndex, RectList);
+
+                if (cacahe.Count > 0)
+                {
+                    if (cacahe.ContainsKey(PDFViewer.CurrentIndex))
+                    {
+                        TextRectList = cacahe[PDFViewer.CurrentIndex];
+                    }
+                    else
+                    {
+                        TextRectList = null;
+                    }
+                }
+            }
+            ClearDraw();
+        }
+
         private void CancelEnhanced()
         {
             ShowTooltip = Visibility.Collapsed;
@@ -204,6 +373,7 @@ namespace PDF_Office.ViewModels.Scan
         /// <param name="e"></param>
         private void ChangeScanMode(ScanEventArgs e)
         {
+            IsShowRect = false;
             switch (e.Mode)
             {
                 case ScanMode.Unknown:
@@ -223,6 +393,8 @@ namespace PDF_Office.ViewModels.Scan
                     }
                     break;
                 case ScanMode.Area:
+                    language = (COCRLanguage)e.ScanLanguage;
+                    IsShowRect = true;
                     break;
                 default:
                     break;
@@ -247,8 +419,20 @@ namespace PDF_Office.ViewModels.Scan
             {
                 CIMEngine imEngine = new CIMEngine(App.modelFolderPath);
                 CErrorCode error = imEngine.SetModel();
+
+                //显示进度条
+                App.mainWindowViewModel.IsProcessVisible = Visibility.Visible;
+                App.mainWindowViewModel.MaxValue = CurrentDoc.PageCount;
+
                 for (int i = 0; i < CurrentDoc.PageCount; i++)
                 {
+                    //刷新进度
+                    App.Current.Dispatcher.Invoke(async () =>
+                    {
+                        App.mainWindowViewModel.Value = i;
+                        await Task.Delay(5);
+                    });
+
                     string pageImagePath = Path.Combine(path, i.ToString());
                     string pageEnhancePath = Path.Combine(EnhancePath, i.ToString() + ".png");
                     try
@@ -287,6 +471,11 @@ namespace PDF_Office.ViewModels.Scan
                 });
                 imEngine.Release();
                 ShowTooltip = Visibility.Visible;
+
+                //隐藏进度条
+                App.mainWindowViewModel.IsProcessVisible = Visibility.Collapsed;
+                App.mainWindowViewModel.MaxValue = 0;
+                App.mainWindowViewModel.Value = 0;
             });
         }
 
@@ -312,8 +501,19 @@ namespace PDF_Office.ViewModels.Scan
 
                 cacahe.Clear();
 
+                //显示进度条
+                App.mainWindowViewModel.IsProcessVisible = Visibility.Visible;
+                App.mainWindowViewModel.MaxValue = args.PageRange.Count;
+
                 for (int i = 0; i < args.PageRange.Count; i++)
                 {
+                    //刷新进度
+                    App.Current.Dispatcher.Invoke(async () =>
+                    {
+                        App.mainWindowViewModel.Value = i;
+                        await Task.Delay(5);
+                    });
+
                     string pageImagePath = Path.Combine(path, args.PageRange[i].ToString());
 
                     CPDFPage pdfPage = CurrentDoc.PageAtIndex(args.PageRange[i]);
@@ -326,7 +526,6 @@ namespace PDF_Office.ViewModels.Scan
                     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);
@@ -367,6 +566,10 @@ namespace PDF_Office.ViewModels.Scan
                     }
                 }
                 imEngine.Release();
+                //隐藏进度条
+                App.mainWindowViewModel.IsProcessVisible = Visibility.Collapsed;
+                App.mainWindowViewModel.MaxValue = 0;
+                App.mainWindowViewModel.Value = 0;
             });
         }
 
@@ -421,5 +624,6 @@ namespace PDF_Office.ViewModels.Scan
                 imEngine.Release();
             });
         }
+
     }
 }

+ 13 - 3
PDF Office/ViewModels/Tools/ScanContentViewModel.cs

@@ -3,6 +3,7 @@ using ComPDFKit.PDFDocument;
 using ComPDFKit.PDFPage;
 using ComPDFKit.PDFPage.Edit;
 using ComPDFKitViewer.PdfViewer;
+using PDF_Office.CustomControl;
 using PDF_Office.EventAggregators;
 using PDF_Office.Helper;
 using PDF_Office.Model;
@@ -52,14 +53,14 @@ namespace PDF_Office.ViewModels.Tools
         private IRegionManager regions;
         public IEventAggregator events;
         public DelegateCommand NavigationCommand { get; set; }
-        public DelegateCommand EnhancedCommand { 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(EnhancedScan);
+            EnhancedCommand = new DelegateCommand<object>(EnhancedScan);
         }
 
         /// <summary>
@@ -75,8 +76,17 @@ namespace PDF_Office.ViewModels.Tools
             //显示属性面板
             viewContentViewModel.IsPropertyOpen = true;
         }
-        private void EnhancedScan()
+        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>();

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

@@ -3,25 +3,29 @@
              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:scan="clr-namespace:PDF_Office.ViewModels.PropertyPanel.Scan" d:DataContext="{d:DesignInstance Type=scan:ScanPropertyPanelViewModel}"
+             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" xmlns:dataconvert="clr-namespace:PDF_Office.DataConvert" d:DataContext="{d:DesignInstance Type=scan:ScanPropertyPanelViewModel}"
              mc:Ignorable="d" >
+    <UserControl.Resources>
+        <dataconvert:BoolToVisible x:Key="BoolToVisible"/>
+    </UserControl.Resources>
     <Grid>
         <StackPanel>
         <TextBlock Text="识别文本"/>
-            <customcontrol:PathRadioButton
+            <customcontrol:PathRadioButton x:Name="BtnOCR" Command="{Binding UnknownCommand}"
                 MouseOverBackground="#EDEEF0" MouseDownBackground="#CED0D4" MouseDownBackgroundOpacity="0.6" 
                 HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
                 <customcontrol:PathRadioButton.Content >
                     <TextBlock Text="页面识别"/>
                 </customcontrol:PathRadioButton.Content>
             </customcontrol:PathRadioButton>
-            <customcontrol:PathRadioButton
+            <customcontrol:PathRadioButton x:Name="BtnArea" Command="{Binding AreaCommand}"
                 MouseOverBackground="#EDEEF0" MouseDownBackground="#CED0D4" MouseDownBackgroundOpacity="0.6" 
                 HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
                 <customcontrol:PathRadioButton.Content >
                     <TextBlock Text="区域识别"/>
                 </customcontrol:PathRadioButton.Content>
             </customcontrol:PathRadioButton>
+            <TextBlock Text="一个页面只会显示最后一次OCR的结果,不会显示多次结果。" TextWrapping="Wrap"  Visibility="{Binding ElementName=BtnArea,Path=IsChecked,Converter={StaticResource BoolToVisible}}" />
             <ComboBox SelectedIndex="0">
                 <TextBlock Text="English"/>
                 <TextBlock Text="Chinese Simplified"/>
@@ -29,7 +33,7 @@
                 <TextBlock Text="French"/>
             </ComboBox>
             <customcontrol:WritableComboBox SelectedIndex="0" MaxPageRange="{Binding PageCount}" PageIndexList="{Binding SetPageRange,Mode=TwoWay}"/>
-            <Button Content="OCR" Command="{Binding OCRCommand}"/>
+            <Button Content="OCR" Command="{Binding OCRCommand}" IsEnabled="{Binding ElementName=BtnOCR,Path=IsChecked}"/>
         </StackPanel>
     </Grid>
 </UserControl>

+ 7 - 3
PDF Office/Views/Scan/ScanViwer.xaml

@@ -3,20 +3,24 @@
              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:scan="clr-namespace:PDF_Office.ViewModels.Scan" d:DataContext="{d:DesignInstance Type=scan:ScanViwerViewModel}"
+             xmlns:local="clr-namespace:PDF_Office.Views.Scan" xmlns:scanviewcontrol="clr-namespace:PDF_Office.CustomControl.ScanViewControl" xmlns:scan="clr-namespace:PDF_Office.ViewModels.Scan" xmlns:customcontrol="clr-namespace:PDF_Office.CustomControl" d:DataContext="{d:DesignInstance Type=scan:ScanViwerViewModel}"
              mc:Ignorable="d"
+             Loaded="UserControl_Loaded"
+             Unloaded="UserControl_Unloaded"
              >
 
     <Grid PreviewMouseWheel="Grid_MouseWheel" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" Background="LightGray" MouseLeftButtonUp="BitmapPanel_MouseLeftButtonUp">
         <ScrollViewer HorizontalScrollBarVisibility="Auto" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" VerticalScrollBarVisibility="Auto" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
             <Grid x:Name="ImageGrid" HorizontalAlignment="Center"  VerticalAlignment="Center" Width="500" Height="500">
-                <scanviewcontrol:CustomPanel x:Name="BitmapPanel" BGImage="{Binding BgImage}" OCRTextRectList="{Binding TextRectList}" MouseDown="CustomPanel_MouseDown" MouseLeftButtonDown="BitmapPanel_MouseLeftButtonDown" MouseLeftButtonUp="BitmapPanel_MouseLeftButtonUp" MouseMove="BitmapPanel_MouseMove"/>
+                <scanviewcontrol:CustomPanel x:Name="BitmapPanel" 
+                                             BGImage="{Binding BgImage}" OCRTextRectList="{Binding TextRectList}" IsShowRect="{Binding IsShowRect}"
+                                             MouseDown="CustomPanel_MouseDown" MouseLeftButtonDown="BitmapPanel_MouseLeftButtonDown" MouseLeftButtonUp="BitmapPanel_MouseLeftButtonUp" MouseMove="BitmapPanel_MouseMove" MouseRightButtonDown="BitmapPanel_MouseRightButtonDown"/>
                 <Canvas>
                     <local:Redress x:Name="RedressControl" Visibility="Collapsed" IsVisibleChanged="RedressControl_IsVisibleChanged"  ApplyCommandHandler="RedressControl_ApplyCommandHandler"/>
                 </Canvas>
             </Grid>
         </ScrollViewer>
-        <Grid Width="318" Height="128" Background="{StaticResource color.sys.layout.state.note}" VerticalAlignment="Top" HorizontalAlignment="Right" Visibility="{Binding ShowTooltip}"> 
+        <Grid Width="318" Height="128" Background="{StaticResource color.sys.layout.state.note}" VerticalAlignment="Top" HorizontalAlignment="Right" Visibility="{Binding ShowTooltip}">
             <Grid.ColumnDefinitions>
                 <ColumnDefinition Width="8"/>
                 <ColumnDefinition Width="*"/>

+ 72 - 13
PDF Office/Views/Scan/ScanViwer.xaml.cs

@@ -1,4 +1,5 @@
 using PDF_Office.CustomControl.ScanViewControl;
+using PDF_Office.ViewModels.Scan;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -21,6 +22,9 @@ namespace PDF_Office.Views.Scan
     /// </summary>
     public partial class ScanViwer : UserControl
     {
+        ScanViwerViewModel scanViwerViewModel;
+        MenuItem item;
+        ContextMenu contextMenu;
         public ScanViwer()
         {
             InitializeComponent();
@@ -34,6 +38,12 @@ namespace PDF_Office.Views.Scan
                 if (e.Delta > 0)
                 {
                     scale += 0.1;
+                    if (scanViwerViewModel.IsShowRect)
+                    {
+                        Rect rect = new Rect(drawRect.X * scale, drawRect.Y * scale, drawRect.Width * scale, drawRect.Height * scale);
+                        BitmapPanel.SetRect(rect);
+                        BitmapPanel.RectDraw();
+                    }
                     ImageGrid.Width = 500 * scale;
                     ImageGrid.Height = 500 * scale;
                 }
@@ -43,6 +53,12 @@ namespace PDF_Office.Views.Scan
                     {
                         scale -= 0.2;
                     }
+                    if (scanViwerViewModel.IsShowRect)
+                    {
+                        Rect rect = new Rect(drawRect.X * scale, drawRect.Y * scale, drawRect.Width * scale, drawRect.Height * scale);
+                        BitmapPanel.SetRect(rect);
+                        BitmapPanel.RectDraw();
+                    }
                     ImageGrid.Width = 500 * scale;
                     ImageGrid.Height = 500 * scale;
 
@@ -98,12 +114,15 @@ namespace PDF_Office.Views.Scan
 
         private void BitmapPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
         {
-            Point clickPoint = e.GetPosition(BitmapPanel);
-            dowm = true;
-            if (!BitmapPanel.RectMouseLeftButtonDown(clickPoint) && BitmapPanel.DrawRect.Contains(clickPoint))
+            if (scanViwerViewModel.IsShowRect)
             {
+                Point clickPoint = e.GetPosition(BitmapPanel);
+                dowm = true;
                 BitmapPanel.RectSetMaxRect(BitmapPanel.DrawRect);
-                CreateNewRect = true;
+                if (!BitmapPanel.RectMouseLeftButtonDown(clickPoint) && BitmapPanel.DrawRect.Contains(clickPoint))
+                {
+                    CreateNewRect = true;
+                }
             }
         }
 
@@ -112,24 +131,64 @@ namespace PDF_Office.Views.Scan
             dowm = false;
             CreateNewRect = false;
         }
-
+        private Rect drawRect;
         private void BitmapPanel_MouseMove(object sender, MouseEventArgs e)
         {
-            Point currentPos = e.GetPosition(BitmapPanel);
-            if (dowm && BitmapPanel.DrawRect.Contains(currentPos))
+            if (scanViwerViewModel.IsShowRect)
+            {
+                Point currentPos = e.GetPosition(BitmapPanel);
+                if (dowm && BitmapPanel.DrawRect.Contains(currentPos))
+                {
+                    if (CreateNewRect)
+                    {
+                        drawRect = new Rect(MouseDownPos, currentPos);
+                        BitmapPanel.SetRect(drawRect);
+                        BitmapPanel.RectDraw();
+                    }
+                    else
+                    {
+                        BitmapPanel.RectMouseMove(currentPos);
+                        drawRect = BitmapPanel.GetClientRect();
+                    }
+                    drawRect = new Rect(drawRect.X / scale, drawRect.Y / scale, drawRect.Width / scale, drawRect.Height / scale);
+                }
+            }
+        }
+
+        private void BitmapPanel_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
+        {
+            if (scanViwerViewModel.IsShowRect)
             {
-                if (CreateNewRect)
+                Point clickPoint = e.GetPosition(BitmapPanel);
+                if (BitmapPanel.RectMouseLeftButtonDown(clickPoint))
                 {
-                    Rect drawRect = new Rect(MouseDownPos, currentPos);
-                    BitmapPanel.SetRect(drawRect);
-                    BitmapPanel.RectDraw();
+                    BitmapPanel.ContextMenu = contextMenu;
                 }
                 else
                 {
-                    BitmapPanel.RectMouseMove(currentPos);
-
+                    BitmapPanel.ContextMenu = null;
                 }
+                e.Handled = true;
             }
         }
+
+        private void Item_Click(object sender, RoutedEventArgs e)
+        {
+            scanViwerViewModel.AreaProcess(BitmapPanel.GetClientRect(), BitmapPanel.DrawRect);
+        }
+
+        private void UserControl_Loaded(object sender, RoutedEventArgs e)
+        {
+            scanViwerViewModel = DataContext as ScanViwerViewModel;
+            contextMenu = new ContextMenu();
+            item = new MenuItem() { Header = "开始识别" };
+            item.Click += Item_Click;
+            contextMenu.Items.Add(item);
+        }
+
+        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
+        {
+            item.Click -= Item_Click;
+        }
     }
 }

+ 10 - 8
PDF Office/Views/Tools/ScanContent.xaml

@@ -7,22 +7,24 @@
              mc:Ignorable="d" >
     <Grid>
         <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
-            <customcontrol:ImageButton HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
-            Padding="3" Command="{Binding EnhancedCommand}"
-            MouseOverBackground="#000000" MouseOverBackgroundOpacity="0.25"
-            Icon="pack://application:,,,/PDF Office;component/Resources/ToolBarIcon/Scan/enhance.png"  
+            <customcontrol:ImageRadioButton HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
+            Padding="3" Command="{Binding EnhancedCommand}"  GroupName="enhance" CommandParameter="{Binding RelativeSource={ RelativeSource Mode=Self}}"
+            MouseOverBackground="{StaticResource color.item-state.sel.bg.lv2}" 
+            Icon="pack://application:,,,/PDF Office;component/Resources/ToolBarIcon/Scan/enhance.png"
+            IconChecked="pack://application:,,,/PDF Office;component/Resources/ToolBarIcon/Scan/enhance.png"                                   
             IconPress="pack://application:,,,/PDF Office;component/Resources/ToolBarIcon/Scan/enhance.png"  
             IconMouseOver="pack://application:,,,/PDF Office;component/Resources/ToolBarIcon/Scan/enhance.png"
             Background="Transparent" Content="增强扫描"/>
-            <customcontrol:ImageButton HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
-            Padding="3" Command="{Binding NavigationCommand}"
-            MouseOverBackground="#000000" MouseOverBackgroundOpacity="0.25"                         
+            <customcontrol:ImageRadioButton HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
+            Padding="3" Command="{Binding NavigationCommand}" GroupName="ocr"
+            MouseOverBackground="{StaticResource color.item-state.sel.bg.lv2}"                         
             Icon="pack://application:,,,/PDF Office;component/Resources/ToolBarIcon/Scan/ocr.png"  
+            IconChecked="pack://application:,,,/PDF Office;component/Resources/ToolBarIcon/Scan/ocr.png"                      
             IconPress="pack://application:,,,/PDF Office;component/Resources/ToolBarIcon/Scan/ocr.png"  
             IconMouseOver="pack://application:,,,/PDF Office;component/Resources/ToolBarIcon/Scan/ocr.png"
             Background="Transparent" Content="识别文本"/>
             <customcontrol:ImageButton HorizontalContentAlignment="Center" VerticalContentAlignment="Center" 
-            Padding="3"
+            Padding="3" 
             MouseOverBackground="#000000" MouseOverBackgroundOpacity="0.25"
             Icon="pack://application:,,,/PDF Office;component/Resources/ToolBarIcon/Scan/batch.png"  
             IconPress="pack://application:,,,/PDF Office;component/Resources/ToolBarIcon/Scan/batch.png"