Browse Source

OCR-ocr自定义控件

zhuyi 2 years ago
parent
commit
6fc06052dd

+ 2 - 0
PDF Office/App.xaml.cs

@@ -54,6 +54,7 @@ using PDF_Office.Views.Dialog.HomePageToolsDialogs.HomePageBatchProcessing.HomeP
 using ComDocumentAIKit;
 using PDF_Office.Views.PropertyPanel.Scan;
 using PDF_Office.Views.Dialog.Redaction;
+using PDF_Office.Views.Scan;
 
 namespace PDF_Office
 {
@@ -154,6 +155,7 @@ namespace PDF_Office
 
             containerRegistry.RegisterForNavigation<HomeContent>();
             containerRegistry.RegisterForNavigation<ViewContent>();
+            containerRegistry.RegisterForNavigation<ScanViwer>();
             containerRegistry.RegisterForNavigation<MainContent>();
             containerRegistry.RegisterForNavigation<HomeCloudContent>("Cloud");
             containerRegistry.RegisterForNavigation<HomeToolsContent>("Tools");

+ 87 - 0
PDF Office/CustomControl/ScanViewControl/CustomDraw.cs

@@ -0,0 +1,87 @@
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Media;
+
+namespace PDF_Office.CustomControl.ScanViewControl
+{
+    class CustomDraw : DrawingVisual
+    {
+        public Rect PaintRect;
+        public Rect RawRect;
+        public string PaintText;
+        public double FontSize = 0;
+        private Pen DrawPen;
+        private SolidColorBrush CoverBrush = Brushes.Transparent;// new SolidColorBrush(Color.FromArgb(0x01, 0xFF, 0xFF, 0xFF));
+        public CustomDraw()
+        {
+            DrawPen = new Pen(Brushes.Red, 1);
+        }
+
+        public void Draw()
+        {
+            DrawingContext dc = RenderOpen();
+            dc.DrawRectangle(null, DrawPen, PaintRect);
+            if (string.IsNullOrEmpty(PaintText) == false)
+            {
+                double fontSize = 1;
+                FormattedText formatText = new FormattedText(PaintText,
+                    CultureInfo.CurrentCulture,
+                    FlowDirection.LeftToRight,
+                    new Typeface(new FontFamily("Arial"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
+                    fontSize,
+                    Brushes.Green,Helper.DpiHelpers.Dpi/96F);
+                while (formatText.Width + fontSize < PaintRect.Width && formatText.Height < PaintRect.Height)
+                {
+                    fontSize += 1;
+                    formatText.SetFontSize(fontSize);
+                }
+                if (fontSize > 1)
+                {
+                    formatText.SetFontSize(fontSize - 1);
+                }
+                FontSize = fontSize;
+                dc.DrawRectangle(Brushes.White, null, PaintRect);
+                Array x = PaintText.ToCharArray();
+                double w = 0;
+
+                List<FormattedText> drawFormatList = new List<FormattedText>();
+                for (int i = 0; i < x.Length; i++)
+                {
+                    FormattedText formatText1 = new FormattedText(x.GetValue(i).ToString(),
+                    CultureInfo.CurrentCulture,
+                    FlowDirection.LeftToRight,
+                    new Typeface(new FontFamily("Arial"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
+                    fontSize,
+                    Brushes.Green, Helper.DpiHelpers.Dpi / 96F);
+                    formatText1.SetFontSize(fontSize);
+                    w += formatText1.Width;
+                    drawFormatList.Add(formatText1);
+                }
+
+                Point startPos = new Point(PaintRect.Left, (int)(PaintRect.Top));
+                double subLength = PaintRect.Width - w;
+                double offset = subLength / x.Length;
+
+                foreach (FormattedText subFormat in drawFormatList)
+                {
+                    dc.DrawText(subFormat, startPos);
+                    startPos.X += subFormat.Width + offset;
+                }
+
+            }
+            dc.Close();
+        }
+
+        public void DrawBounds()
+        {
+            DrawingContext dc = RenderOpen();
+            dc.DrawRectangle(CoverBrush, DrawPen, PaintRect);
+            dc.Close();
+        }
+    }
+}

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

@@ -0,0 +1,163 @@
+using PDF_Office.Helper;
+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.Media;
+using System.Windows.Media.Imaging;
+
+namespace PDF_Office.CustomControl.ScanViewControl
+{
+    class CustomPanel: Panel
+    {
+        public SolidColorBrush CoverBrush { get; set; } = new SolidColorBrush(Color.FromArgb(0xA0, 0xFF, 0xFF, 0xFF));
+        public bool DrawCover { get; set; } = false;
+
+        private WriteableBitmap BackgroundImage = null;
+        private int BackgroundWidth = 0;
+        private int BackgroundHeight = 0;
+        private Point OffsetPos = new Point(0, 0);
+        private double ScaleRate = 1;
+        private CustomDraw HoverChild = null;
+        public TextBlock ParentBlock;
+        private int RawWidth = 0;
+        private int RawHeight = 0;
+
+        private List<CustomDraw> VisualControlList = new List<CustomDraw>();
+        protected override int VisualChildrenCount => VisualControlList.Count;
+        protected override Visual GetVisualChild(int index)
+        {
+            return VisualControlList[index];
+        }
+        protected override void OnRender(DrawingContext dc)
+        {
+            Rect drawRect = new Rect(0, 0, ActualWidth, ActualHeight);
+            dc.DrawRectangle(Brushes.LightGray, null, drawRect);
+            DrawBackground(dc);
+        }
+
+        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
+        {
+            double scale = Math.Min(sizeInfo.NewSize.Width / BackgroundWidth, sizeInfo.NewSize.Height / BackgroundHeight);
+            scale = Math.Min(scale, 1);
+            int drawWidth = (int)(scale * BackgroundWidth);
+            int drawHeight = (int)(scale * BackgroundHeight);
+            if (drawWidth < 10 || drawHeight < 10)
+            {
+                return;
+            }
+
+            int offsetX = (int)((sizeInfo.NewSize.Width - drawWidth) / 2);
+            int offsetY = (int)((sizeInfo.NewSize.Height - drawHeight) / 2);
+            foreach (CustomDraw drawVisual in VisualControlList)
+            {
+                Rect paintRect = drawVisual.RawRect;
+                drawVisual.PaintRect = new Rect((int)(paintRect.Left * scale + offsetX),
+                   (int)(paintRect.Top * scale + offsetY),
+                   (int)(paintRect.Width * scale),
+                   (int)(paintRect.Height * scale));
+                drawVisual.Draw();
+            }
+        }
+
+        private void DrawBackground(DrawingContext dc)
+        {
+            if (BackgroundImage != null && BackgroundWidth > 0 && BackgroundHeight > 0)
+            {
+                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);
+                Rect drawRect = new Rect(OffsetPos.X, OffsetPos.Y, drawWidth, drawHeight);
+                dc.DrawImage(BackgroundImage, drawRect);
+                if (DrawCover)
+                {
+                    dc.DrawRectangle(CoverBrush, null, drawRect);
+                }
+            }
+        }
+
+        private void CleanChild()
+        {
+            foreach (DrawingVisual child in VisualControlList)
+            {
+                RemoveLogicalChild(child);
+                RemoveVisualChild(child);
+            }
+            VisualControlList.Clear();
+        }
+
+        public void SetBackgroundImage(WriteableBitmap bgImage)
+        {
+            DrawCover = false;
+            BackgroundImage = bgImage;
+            BackgroundWidth = 0;
+            BackgroundHeight = 0;
+            RawWidth = 0;
+            RawHeight = 0;
+            OffsetPos = new Point(0, 0);
+            CleanChild();
+            if (BackgroundImage != null && BackgroundImage.PixelHeight > 0 && BackgroundImage.PixelWidth > 0)
+            {
+                RawWidth = BackgroundImage.PixelWidth;
+                RawHeight = BackgroundImage.PixelHeight;
+                BackgroundWidth = DpiHelpers.GetDpiUnrelatedNum(BackgroundImage.PixelWidth);
+                BackgroundHeight = DpiHelpers.GetDpiUnrelatedNum(BackgroundImage.PixelHeight);
+                InvalidateVisual();
+            }
+        }
+
+        public int GetBackgroundImageWidth()
+        {
+            if (BackgroundImage != null)
+            {
+                return RawWidth;
+            }
+            return 0;
+        }
+
+        public int GetBackgroundImageHeight()
+        {
+            if (BackgroundImage != null)
+            {
+                return RawHeight;
+            }
+            return 0;
+        }
+
+        public void SetTextsRects(List<KeyValuePair<Rect, string>> textsRect)
+        {
+            CleanChild();
+            foreach (KeyValuePair<Rect, string> textRect in textsRect)
+            {
+                CustomDraw drawVisual = new CustomDraw();
+                drawVisual.PaintText = textRect.Value;
+                Rect paintRect = DpiHelpers.GetDpiUnrelatedRect(textRect.Key);
+                drawVisual.RawRect = paintRect;
+                drawVisual.PaintRect = new Rect((int)(paintRect.Left * ScaleRate + OffsetPos.X),
+                    (int)(paintRect.Top * ScaleRate + OffsetPos.Y),
+                    (int)(paintRect.Width * ScaleRate),
+                    (int)(paintRect.Height * ScaleRate));
+                drawVisual.Draw();
+                VisualControlList.Add(drawVisual);
+                AddLogicalChild(drawVisual);
+                AddVisualChild(drawVisual);
+            }
+        }
+
+        public void RePaint()
+        {
+            InvalidateVisual();
+        }
+    }
+}

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

@@ -0,0 +1,95 @@
+using Prism.Events;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PDF_Office.EventAggregators
+{
+    public enum ScanMode
+    {
+        Unknown,
+        /// <summary>
+        /// 点击OCR按钮
+        /// </summary>
+        OCR,
+        /// <summary>
+        /// 区域识别
+        /// </summary>
+        Area
+    }
+
+    public enum ScanLanguageMode
+    {
+        Unknown,
+        /// <summary>
+        /// 简体中文
+        /// </summary>
+        ChineseS,
+        /// <summary>
+        /// 繁体中文
+        /// </summary>
+        ChineseT,
+        English,
+        French,
+    }
+    public class ScanEventArgs
+    {
+        private ScanMode mode = ScanMode.Unknown;
+        public ScanMode Mode 
+        {
+            get
+            {
+                return mode;
+            }
+            set
+            {
+                Mode = value;
+            }
+        }
+
+        private ScanLanguageMode scanLanguage = ScanLanguageMode.Unknown;
+        public ScanLanguageMode ScanLanguage 
+        { 
+            get
+            {
+                return scanLanguage;
+            }
+            set
+            {
+                scanLanguage = value;
+            } 
+        }
+
+        private string pageRange = "";
+        public string PageRange 
+        {
+            get
+            {
+                return pageRange;
+            }
+            set
+            {
+                pageRange = value;
+            }
+        }
+
+        private string unicode = "";
+        public string Unicode 
+        {
+            get
+            {
+                return unicode;
+            }
+            set 
+            {
+                unicode = value;
+            }
+        }
+    }
+    class ScanEvent : PubSubEvent<ScanEventArgs>
+    {
+
+    }
+}

+ 11 - 0
PDF Office/PDF Office.csproj

@@ -265,6 +265,8 @@
     </Compile>
     <Compile Include="CustomControl\PathButton.cs" />
     <Compile Include="CustomControl\PathRadioButton.cs" />
+    <Compile Include="CustomControl\ScanViewControl\CustomDraw.cs" />
+    <Compile Include="CustomControl\ScanViewControl\CustomPanel.cs" />
     <Compile Include="CustomControl\SystemControl\CustomCommandAction .cs" />
     <Compile Include="CustomControl\SystemControl\RoutedEventTrigger.cs" />
     <Compile Include="CustomControl\TextBoxEx.cs" />
@@ -300,6 +302,7 @@
     <Compile Include="EventAggregators\EditToolsEvent.cs" />
     <Compile Include="EventAggregators\PageEditNotifyEvent.cs" />
     <Compile Include="EventAggregators\PageEditRefreshEvent.cs" />
+    <Compile Include="EventAggregators\ScanEvent.cs" />
     <Compile Include="EventAggregators\SendPrintInfoEvent.cs" />
     <Compile Include="EventAggregators\SplitEvent.cs" />
     <Compile Include="Helper\CacheFilePath.cs" />
@@ -483,6 +486,7 @@
     <Compile Include="ViewModels\HomePanel\RecentFiles\RecentFilesContentViewModel.cs" />
     <Compile Include="ViewModels\Dialog\CustomCreateDialogViewModel.cs" />
     <Compile Include="ViewModels\Dialog\DynamicPropertyDialogViewModel.cs" />
+    <Compile Include="ViewModels\Scan\ScanViwerViewModel.cs" />
     <Compile Include="ViewModels\Tools\ConverterBarContentViewModel.cs" />
     <Compile Include="ViewModels\Dialog\ExtractDialogViewModel.cs" />
     <Compile Include="ViewModels\Dialog\FullScreenWindowViewModel.cs" />
@@ -1007,6 +1011,9 @@
     <Compile Include="Views\PropertyPanel\ViewModular\ViewModularContent.xaml.cs">
       <DependentUpon>ViewModularContent.xaml</DependentUpon>
     </Compile>
+    <Compile Include="Views\Scan\ScanViwer.xaml.cs">
+      <DependentUpon>ScanViwer.xaml</DependentUpon>
+    </Compile>
     <Compile Include="Views\Tools\AnnotToolContent.xaml.cs">
       <DependentUpon>AnnotToolContent.xaml</DependentUpon>
     </Compile>
@@ -1820,6 +1827,10 @@
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     </Page>
+    <Page Include="Views\Scan\ScanViwer.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </Page>
     <Page Include="Views\Tools\AnnotToolContent.xaml">
       <SubType>Designer</SubType>
       <Generator>MSBuild:Compile</Generator>

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

@@ -1,5 +1,8 @@
 using ComPDFKitViewer.PdfViewer;
+using PDF_Office.EventAggregators;
 using PDF_Office.Model;
+using Prism.Commands;
+using Prism.Events;
 using Prism.Mvvm;
 using Prism.Regions;
 using System;
@@ -36,6 +39,10 @@ namespace PDF_Office.ViewModels.PropertyPanel.Scan
 
 
         private CPDFViewer PDFViewer;
+
+        public IEventAggregator events;
+
+        public DelegateCommand OCRCommand { get; set; }
         public bool IsNavigationTarget(NavigationContext navigationContext)
         {
             return true;
@@ -58,5 +65,21 @@ namespace PDF_Office.ViewModels.PropertyPanel.Scan
                 PageCount = PDFViewer.Document.PageCount;
             }
         }
+        public ScanPropertyPanelViewModel(IEventAggregator eventAggregator)
+        {
+            events = eventAggregator;
+            OCRCommand = new DelegateCommand(OCR);
+        }
+        private void OCR()
+        {
+            events.GetEvent<ScanEvent>().Publish(new ScanEventArgs()
+            {
+                Unicode = App.mainWindowViewModel.SelectedItem.Unicode,
+                PageRange = SetPageRange,
+                Mode = ScanMode.OCR,
+                ScanLanguage= ScanLanguageMode.ChineseS
+            }
+            ); 
+        }
     }
 }

+ 78 - 0
PDF Office/ViewModels/Scan/ScanViwerViewModel.cs

@@ -0,0 +1,78 @@
+using ComPDFKitViewer;
+using ComPDFKitViewer.PdfViewer;
+using PDF_Office.EventAggregators;
+using PDF_Office.Model;
+using Prism.Events;
+using Prism.Mvvm;
+using Prism.Regions;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PDF_Office.ViewModels.Scan
+{
+    class ScanViwerViewModel : BindableBase, INavigationAware
+    {
+        private CPDFViewer PDFViewer;
+        public bool IsNavigationTarget(NavigationContext navigationContext)
+        {
+            return true;
+        }
+
+        public void OnNavigatedFrom(NavigationContext navigationContext)
+        {
+            return;
+        }
+
+        public void OnNavigatedTo(NavigationContext navigationContext)
+        {
+            navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
+            if (PDFViewer == null)
+            {
+                return;
+            }
+            else
+            {
+                PDFViewer.InfoChanged += PDFViewer_InfoChanged;
+            }
+        }
+
+        private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
+        {
+            if (e.Key == "PageNum")
+            {
+                RenderData renderData = e.Value as RenderData;
+                if (renderData != null)
+                {
+                    var CurrentPage = renderData.PageIndex;
+                }
+            }
+        }
+
+        public ScanViwerViewModel(IEventAggregator eventAggregator)
+        {
+            eventAggregator.GetEvent<ScanEvent>().Subscribe(ChangeScanMode, e => e.Unicode == App.mainWindowViewModel.SelectedItem.Unicode);
+        }
+        /// <summary>
+        /// 根据Vm事件通知处理OCR与区域识别事件
+        /// </summary>
+        /// <param name="e"></param>
+        private void ChangeScanMode(ScanEventArgs e)
+        {
+            switch (e.Mode)
+            {
+                case ScanMode.Unknown:
+                    break;
+                case ScanMode.OCR:
+                    break;
+                case ScanMode.Area:
+                    break;
+                default:
+                    break;
+            }
+        }
+
+    }
+}

+ 4 - 0
PDF Office/ViewModels/ViewContentViewModel.cs

@@ -1146,6 +1146,10 @@ namespace PDF_Office.ViewModels
             param.Add(ParameterNames.PDFViewer, PDFViewer);
             param.Add(ParameterNames.ViewContentViewModel, this);
             region.RequestNavigate(regionNameByTabItem[currentBar], barContentByTabItem[currentBar], param);
+            if (currentBar=="TabItemScan")
+            {
+                region.RequestNavigate(RegionNames.ViwerRegionName, "ScanViwer", param);
+            }
             ShowContent(currentBar);
         }
 

+ 11 - 0
PDF Office/Views/Scan/ScanViwer.xaml

@@ -0,0 +1,11 @@
+<UserControl x:Class="PDF_Office.Views.Scan.ScanViwer"
+             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" xmlns:scanviewcontrol="clr-namespace:PDF_Office.CustomControl.ScanViewControl"
+             mc:Ignorable="d" >
+    <Grid >
+        <scanviewcontrol:CustomPanel/>
+    </Grid>
+</UserControl>

+ 28 - 0
PDF Office/Views/Scan/ScanViwer.xaml.cs

@@ -0,0 +1,28 @@
+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>
+    /// ScanViwer.xaml 的交互逻辑
+    /// </summary>
+    public partial class ScanViwer : UserControl
+    {
+        public ScanViwer()
+        {
+            InitializeComponent();
+        }
+    }
+}