Ver Fonte

OCR-文本矫正

zhuyi há 2 anos atrás
pai
commit
0c0a45e14e

+ 13 - 0
PDF Office/CustomControl/ScanViewControl/BoxSelection.cs

@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+
+namespace PDF_Office.CustomControl.ScanViewControl
+{
+    public class BoxSelection : FrameworkElement
+    {
+    }
+}

+ 2 - 2
PDF Office/CustomControl/ScanViewControl/CustomDraw.cs

@@ -9,7 +9,7 @@ using System.Windows.Media;
 
 namespace PDF_Office.CustomControl.ScanViewControl
 {
-    class CustomDraw : DrawingVisual
+    public class CustomDraw : DrawingVisual
     {
         public Rect PaintRect;
         public Rect RawRect;
@@ -34,7 +34,7 @@ namespace PDF_Office.CustomControl.ScanViewControl
                     FlowDirection.LeftToRight,
                     new Typeface(new FontFamily("Arial"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
                     fontSize,
-                    Brushes.Green,Helper.DpiHelpers.Dpi/96F);
+                    Brushes.Green, Helper.DpiHelpers.Dpi / 96F);
                 while (formatText.Width + fontSize < PaintRect.Width && formatText.Height < PaintRect.Height)
                 {
                     fontSize += 1;

+ 41 - 7
PDF Office/CustomControl/ScanViewControl/CustomPanel.cs

@@ -11,9 +11,9 @@ using System.Windows.Media.Imaging;
 
 namespace PDF_Office.CustomControl.ScanViewControl
 {
-    class CustomPanel: Panel
+    class CustomPanel : Panel
     {
-        public SolidColorBrush CoverBrush { get; set; } = new SolidColorBrush(Color.FromArgb(0xA0, 0xFF, 0xFF, 0xFF));
+        public SolidColorBrush CoverBrush { get; set; } = new SolidColorBrush(Color.FromArgb(0xCC, 0xFF, 0xFF, 0xFF));
         public bool DrawCover { get; set; } = false;
 
         private WriteableBitmap BackgroundImage = null;
@@ -33,19 +33,19 @@ namespace PDF_Office.CustomControl.ScanViewControl
         public WriteableBitmap BGImage
         {
             get { return (WriteableBitmap)GetValue(BGImageProperty); }
-            set 
-            { 
-                SetValue(BGImageProperty, value); 
+            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)));
+            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)
+            if ((e.NewValue as WriteableBitmap) != null)
             {
                 (d as CustomPanel).SetBackgroundImage(e.NewValue as WriteableBitmap);
             }
@@ -174,6 +174,40 @@ namespace PDF_Office.CustomControl.ScanViewControl
             }
         }
 
+        public void DisplayContent(CustomDraw custom)
+        {
+            if (custom != null)
+            {
+                if (HoverChild == null)
+                {
+                    HoverChild = custom;
+                    HoverChild.DrawBounds();
+                    if (ParentBlock != null)
+                    {
+                        ParentBlock.Text = HoverChild.PaintText;
+                    }
+                }
+                else
+                {
+                    if (HoverChild != custom)
+                    {
+                        HoverChild?.Draw();
+                        HoverChild = custom;
+                        HoverChild.DrawBounds();
+                    }
+                }
+            }
+            else
+            {
+                if (HoverChild != null)
+                {
+                    HoverChild.Draw();
+                    HoverChild = null;
+                }
+            }
+
+        }
+
         public int GetBackgroundImageWidth()
         {
             if (BackgroundImage != null)

+ 15 - 0
PDF Office/EventAggregators/ScanEvent.cs

@@ -95,4 +95,19 @@ namespace PDF_Office.EventAggregators
     {
 
     }
+    class a : ComPDFKitViewer.IHistory
+    {
+        List<int> x;
+        public bool Redo()
+        {
+            x.Add(1);
+            return true;
+        }
+
+        public bool Undo()
+        {
+            x.Remove(1);
+            return true;
+        }
+    }
 }

+ 15 - 18
PDF Office/Helper/CacheFilePath.cs

@@ -15,7 +15,7 @@ namespace PDF_Office.Helper
     /// 功能模块需要创建缓存文件夹时,统一在此类里处理
     /// 临时文件在app启动时进行删除
     /// </summary>
-    public class CacheFilePath:BindableBase
+    public class CacheFilePath : BindableBase
     {
         private static readonly CacheFilePath instance = new CacheFilePath();
 
@@ -23,7 +23,7 @@ namespace PDF_Office.Helper
         /// 图章缓存文件夹路径
         /// </summary>
         List<string> CustomStamp = new List<string> { "CustomStamp" };
-        List<string> SignatureFreeHand = new List<string> { "Signature" ,"FreeHand" };
+        List<string> SignatureFreeHand = new List<string> { "Signature", "FreeHand" };
         List<string> SignatureStamp = new List<string> { "Signature", "Stamp" };
         List<string> MergeFile = new List<string> { "Temp" };
 
@@ -48,9 +48,9 @@ namespace PDF_Office.Helper
 
         }
 
-        ~ CacheFilePath()
+        ~CacheFilePath()
         {
-            if(CopyDoc!=null)
+            if (CopyDoc != null)
             {
                 CopyDoc.Release();
             }
@@ -63,7 +63,7 @@ namespace PDF_Office.Helper
         {
             get
             {
-               return CreateCacheDirectory(CustomStamp);
+                return CreateCacheDirectory(CustomStamp);
             }
         }
 
@@ -74,7 +74,7 @@ namespace PDF_Office.Helper
         {
             get
             {
-               return CreateCacheDirectory(SignatureStamp);
+                return CreateCacheDirectory(SignatureStamp);
             }
         }
         /// <summary>
@@ -84,7 +84,7 @@ namespace PDF_Office.Helper
         {
             get
             {
-               return CreateCacheDirectory(SignatureFreeHand);
+                return CreateCacheDirectory(SignatureFreeHand);
             }
         }
 
@@ -95,10 +95,10 @@ namespace PDF_Office.Helper
         {
             get
             {
-               return CreateCacheDirectory(MergeFile);
+                return CreateCacheDirectory(MergeFile);
             }
         }
-        
+
         /// <summary>
         ///  在“文档”路径下创建缓存文件夹,传C:\Users\kdan\Documents\PDF Office 以后的文件夹名
         /// </summary>
@@ -113,7 +113,7 @@ namespace PDF_Office.Helper
                 {
                     Path = System.IO.Path.Combine(Path, directoryName[i]);
                 }
-           
+
                 System.IO.DirectoryInfo directoryInfo = System.IO.Directory.CreateDirectory(Path);
                 if (directoryInfo.Exists
                    && (directoryInfo.Attributes & System.IO.FileAttributes.ReadOnly) != System.IO.FileAttributes.ReadOnly
@@ -155,7 +155,7 @@ namespace PDF_Office.Helper
 
         public void AddToDeleteFiles(List<string> files)
         {
-            foreach(string file in files)
+            foreach (string file in files)
             {
                 AddToDeleteFiles(file);
             }
@@ -177,15 +177,12 @@ namespace PDF_Office.Helper
                 }
                 Settings.Default.AppProperties.NeedToDeletePath.Clear();
 
-                //清楚Temp 文件夹下的临时文件
-                DirectoryInfo tempfolder = new DirectoryInfo(Path.Combine(App.CurrentPath, "Temp"));
+                //清除Temp文件夹及该目录中的所有子内容
+                string TempPath= Path.Combine(App.CurrentPath, "Temp");
+                DirectoryInfo tempfolder = new DirectoryInfo(TempPath);
                 if (tempfolder.Exists)
                 {
-                    foreach (var file in tempfolder.GetFiles())
-                    {
-                        if (file.Exists)
-                            file.Delete();
-                    }
+                    Directory.Delete(TempPath, true);
                 }
             }
             catch { }

+ 7 - 0
PDF Office/PDF Office.csproj

@@ -1033,6 +1033,9 @@
     <Compile Include="Views\PropertyPanel\ViewModular\ViewModularContent.xaml.cs">
       <DependentUpon>ViewModularContent.xaml</DependentUpon>
     </Compile>
+    <Compile Include="Views\Scan\Redress.xaml.cs">
+      <DependentUpon>Redress.xaml</DependentUpon>
+    </Compile>
     <Compile Include="Views\Scan\ScanViwer.xaml.cs">
       <DependentUpon>ScanViwer.xaml</DependentUpon>
     </Compile>
@@ -1873,6 +1876,10 @@
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     </Page>
+    <Page Include="Views\Scan\Redress.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </Page>
     <Page Include="Views\Scan\ScanViwer.xaml">
       <SubType>Designer</SubType>
       <Generator>MSBuild:Compile</Generator>

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

@@ -6,6 +6,7 @@ using ComPDFKitViewer.PdfViewer;
 using PDF_Office.EventAggregators;
 using PDF_Office.Helper;
 using PDF_Office.Model;
+using Prism.Commands;
 using Prism.Events;
 using Prism.Mvvm;
 using Prism.Regions;
@@ -47,8 +48,11 @@ namespace PDF_Office.ViewModels.Scan
                 SetProperty(ref textRectList, value);
             }
         }
-        private bool isEnhanced;
 
+        private bool isEnhanced;
+        /// <summary>
+        /// 当前是否为增强扫描状态
+        /// </summary>
         public bool IsEnhanced
         {
             get { return isEnhanced; }
@@ -63,11 +67,25 @@ namespace PDF_Office.ViewModels.Scan
         /// 增强扫描结果路径
         /// </summary>
         public List<string> EnhancedFilePathList;
+
         /// <summary>
         /// OCR结果路径
         /// </summary>
         private Dictionary<int, List<KeyValuePair<Rect, string>>> cacahe;
 
+        private Visibility showTooltip = Visibility.Collapsed;
+
+        public Visibility ShowTooltip
+        {
+            get { return showTooltip; }
+            set
+            {
+                SetProperty(ref showTooltip, value);
+            }
+        }
+
+        public DelegateCommand CloseTooltipCommand { get; set; }
+        public DelegateCommand CancelEnhancedCommand { get; set; }
 
         public bool IsNavigationTarget(NavigationContext navigationContext)
         {
@@ -143,10 +161,43 @@ namespace PDF_Office.ViewModels.Scan
 
         public ScanViwerViewModel(IEventAggregator eventAggregator)
         {
+            CloseTooltipCommand = new DelegateCommand(CloseTooltip);
+            CancelEnhancedCommand = new DelegateCommand(CancelEnhanced);
             EnhancedFilePathList = new List<string>();
             eventAggregator.GetEvent<ScanEvent>().Subscribe(ChangeScanMode, e => e.Unicode == App.mainWindowViewModel.SelectedItem.Unicode);
             cacahe = new Dictionary<int, List<KeyValuePair<Rect, string>>>();
         }
+
+        private void CancelEnhanced()
+        {
+            ShowTooltip = Visibility.Collapsed;
+            IsEnhanced = false;
+            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[] 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(PDFViewer.CurrentIndex))
+                {
+                    TextRectList = cacahe[PDFViewer.CurrentIndex];
+                }
+                else
+                {
+                    TextRectList = null;
+                }
+            }
+        }
+        private void CloseTooltip()
+        {
+            ShowTooltip = Visibility.Collapsed;
+        }
+
         /// <summary>
         /// 根据Vm事件通知处理OCR与区域识别或者增强扫描事件
         /// </summary>
@@ -178,6 +229,9 @@ namespace PDF_Office.ViewModels.Scan
             }
         }
 
+        /// <summary>
+        /// 增强扫描
+        /// </summary>
         private void EnhancedProcess(ScanEventArgs args)
         {
             CPDFDocument CurrentDoc = PDFViewer.Document;
@@ -232,6 +286,7 @@ namespace PDF_Office.ViewModels.Scan
                     BgImage = new WriteableBitmap(image);
                 });
                 imEngine.Release();
+                ShowTooltip = Visibility.Visible;
             });
         }
 
@@ -244,7 +299,6 @@ namespace PDF_Office.ViewModels.Scan
             Task.Run(() =>
             {
                 COCREngine imEngine = new COCREngine(App.modelFolderPath);
-                //CIMEngine imEngine = new CIMEngine(App.modelFolderPath);
 
                 string path = App.CachePath.MergeFilePath;
                 string name = Guid.NewGuid().ToString();

+ 32 - 0
PDF Office/Views/Scan/Redress.xaml

@@ -0,0 +1,32 @@
+<UserControl x:Class="PDF_Office.Views.Scan.Redress"
+             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+             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"
+             mc:Ignorable="d" 
+             d:DesignHeight="450" d:DesignWidth="800">
+    <Border Background="{StaticResource color.sys.layout.anti}" Width="320" Height="144" CornerRadius="8" >
+        <Grid>
+            <Grid.ColumnDefinitions>
+                <ColumnDefinition Width="16"/>
+                <ColumnDefinition Width="*"/>
+                <ColumnDefinition Width="16"/>
+            </Grid.ColumnDefinitions>
+            <Grid.RowDefinitions>
+                <RowDefinition Height="16"/>
+                <RowDefinition Height="*"/>
+                <RowDefinition Height="16"/>
+                <RowDefinition Height="auto"/>
+                <RowDefinition Height="16"/>
+            </Grid.RowDefinitions>
+            <TextBox x:Name="TextInput" 
+                     Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:Redress}, Path=TextBoxContext}"
+                     Grid.Row="1" Grid.Column="1" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto"/>
+            <StackPanel Grid.Row="3" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Bottom" Orientation="Horizontal">
+                <Button x:Name="BtnApply" Content="Apply" Style="{StaticResource Btn.cta}" Width="80" Height="32" Click="BtnApply_Click" />
+                <Button x:Name="BtnCancel" Content="Cancel" Style="{StaticResource btn.sec}" Width="80" Height="32" Margin="16,0,0,0" Click="BtnCancel_Click"/>
+            </StackPanel>
+        </Grid>
+    </Border>
+</UserControl>

+ 58 - 0
PDF Office/Views/Scan/Redress.xaml.cs

@@ -0,0 +1,58 @@
+using PDF_Office.CustomControl.ScanViewControl;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+
+namespace PDF_Office.Views.Scan
+{
+    /// <summary>
+    /// Redress.xaml 的交互逻辑
+    /// </summary>
+    public partial class Redress : UserControl
+    {
+        public Redress()
+        {
+            InitializeComponent();
+        }
+        public string TextBoxContext
+        {
+            get { return (string)GetValue(TextBoxContextProperty); }
+            set
+            {
+                SetValue(TextBoxContextProperty, value);
+            }
+        }
+        public CustomDraw HitChild { get; set; }
+
+        // Using a DependencyProperty as the backing store for TextBoxContext.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty TextBoxContextProperty =
+            DependencyProperty.Register("TextBoxContext", typeof(string), typeof(Redress), new PropertyMetadata(""));
+
+        private void BtnCancel_Click(object sender, RoutedEventArgs e)
+        {
+            Visibility = Visibility.Collapsed;
+        }
+        public void SelectAll()
+        {
+           TextInput.SelectAll();
+           TextInput.Focus();
+        }
+
+        public event EventHandler ApplyCommandHandler;
+        private void BtnApply_Click(object sender, RoutedEventArgs e)
+        {
+            ApplyCommandHandler.Invoke(sender, e);
+        }
+    }
+}

+ 19 - 1
PDF Office/Views/Scan/ScanViwer.xaml

@@ -10,8 +10,26 @@
     <Grid PreviewMouseWheel="Grid_MouseWheel" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" Background="LightGray">
         <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 BGImage="{Binding BgImage}" OCRTextRectList="{Binding TextRectList}"/>
+                <scanviewcontrol:CustomPanel x:Name="BitmapPanel" BGImage="{Binding BgImage}" OCRTextRectList="{Binding TextRectList}" MouseDown="CustomPanel_MouseDown"/>
+                <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.ColumnDefinitions>
+                <ColumnDefinition Width="8"/>
+                <ColumnDefinition Width="*"/>
+                <ColumnDefinition Width="8"/>
+            </Grid.ColumnDefinitions>
+            <Grid.RowDefinitions>
+                <RowDefinition Height="8"/>
+                <RowDefinition Height="*"/>
+                <RowDefinition Height="8"/>
+            </Grid.RowDefinitions>
+            <TextBlock Grid.Row="1" Grid.Column="1" Text="增强扫描已完成"/>
+            <Button Grid.Row="1" Grid.Column="1" Content="取消增强效果" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="50" Height="50" Command="{Binding CancelEnhancedCommand}"/>
+            <Button Grid.Row="1" Grid.Column="1" Content="取消" HorizontalAlignment="Right" VerticalAlignment="Top" Width="20" Height="20" Command="{Binding CloseTooltipCommand}"/>
+        </Grid>
     </Grid>
 </UserControl>

+ 45 - 1
PDF Office/Views/Scan/ScanViwer.xaml.cs

@@ -1,4 +1,5 @@
-using System;
+using PDF_Office.CustomControl.ScanViewControl;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -47,7 +48,50 @@ namespace PDF_Office.Views.Scan
 
                 }
             }
+            RedressControl.Visibility = Visibility.Collapsed;
+        }
 
+        private Point MouseDownPos;
+        private async void CustomPanel_MouseDown(object sender, MouseButtonEventArgs e)
+        {
+            RedressControl.Visibility = Visibility.Visible;
+            MouseDownPos = e.GetPosition(BitmapPanel);
+            HitTestResult hitResult = VisualTreeHelper.HitTest(BitmapPanel, MouseDownPos);
+            if (hitResult != null && hitResult.VisualHit is CustomDraw)
+            {
+                CustomDraw hitChild = (CustomDraw)hitResult.VisualHit;
+                BitmapPanel.DisplayContent(hitChild);
+                RedressControl.SetValue(Canvas.LeftProperty, hitChild.PaintRect.Left + (hitChild.PaintRect.Width / 2 - RedressControl.ActualWidth / 2));
+                if (hitChild.PaintRect.Top - RedressControl.ActualHeight > 0)
+                {
+                    RedressControl.SetValue(Canvas.TopProperty, hitChild.PaintRect.Top - RedressControl.ActualHeight);
+                }
+                else
+                {
+                    RedressControl.SetValue(Canvas.TopProperty, hitChild.PaintRect.Bottom);
+                }
+                RedressControl.HitChild = hitChild;
+                RedressControl.TextBoxContext = hitChild.PaintText;
+                await Task.Delay(5);
+                RedressControl.SelectAll();
+                return;
+            }
+            RedressControl.Visibility = Visibility.Collapsed;
+        }
+
+        private void RedressControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
+        {
+            if (!(bool)e.NewValue)
+            {
+                BitmapPanel.DisplayContent(null);
+            }
+        }
+
+        private void RedressControl_ApplyCommandHandler(object sender, EventArgs e)
+        {
+            RedressControl.HitChild.PaintText = RedressControl.TextBoxContext;
+            RedressControl.HitChild.Draw(); 
+            RedressControl.Visibility = Visibility.Collapsed;
         }
     }
 }