Prechádzať zdrojové kódy

签名-手绘签名的点校准相关方法

zhuyi 1 rok pred
rodič
commit
a046d27686

+ 140 - 39
Demo/Examples/Compdfkit_Tools/Annotation/PDFAnnotationPanel/PDFAnnotationControl/CPDFAnnotationControl.xaml.cs

@@ -22,6 +22,8 @@ using ComPDFKitViewer.BaseObject;
 using ComPDFKitViewer.Widget;
 using ComPDFKit.PDFDocument;
 using System.Text;
+using ComPDFKitViewer.Helper;
+using ComPDFKit.Viewer.Helper;
 
 namespace Compdfkit_Tools.PDFControl
 {
@@ -457,50 +459,52 @@ namespace Compdfkit_Tools.PDFControl
                         switch (signatureParam.CurrentType)
                         {
                             case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
-                                StampParam stampParam = (StampParam)signatureParam;
-                                stampParam.ImageStream.Seek(0, SeekOrigin.Begin);
-                                decoder = BitmapDecoder.Create(stampParam.ImageStream, BitmapCreateOptions.None, BitmapCacheOption.Default);
+                                {
+                                    StampParam stampParam = (StampParam)signatureParam;
+                                    stampParam.ImageStream.Seek(0, SeekOrigin.Begin);
+                                    decoder = BitmapDecoder.Create(stampParam.ImageStream, BitmapCreateOptions.None, BitmapCacheOption.Default);
+                                    BitmapFrame frame = null;
+                                    if (decoder != null && decoder.Frames.Count > 0)
+                                    {
+                                        frame = decoder.Frames[0];
+                                    }
+                                    if (frame != null)
+                                    {
+                                        byte[] imageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
+                                        if (frame.Format != PixelFormats.Bgra32)
+                                        {
+                                            FormatConvertedBitmap covert = new FormatConvertedBitmap(
+                                                frame,
+                                                PixelFormats.Bgra32,
+                                                frame.Palette,
+                                                0);
+                                            covert.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
+                                        }
+                                        else
+                                        {
+                                            frame.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
+                                        }
+                                        pdfViewerControl.PDFViewTool.GetCPDFViewer().SetStampMouseImage(imageArray, frame.PixelWidth, frame.PixelHeight);
+                                        pdfViewerControl.PDFViewTool.GetCPDFViewer().SetIsVisibleCustomMouse(true);
+                                        pdfViewerControl.PDFViewTool.GetCPDFViewer().SetIsShowStampMouse(true);
+                                        annotHandlerEventArgs = signatureParam;
+                                    }
+                                }
                                 break;
                             case C_ANNOTATION_TYPE.C_ANNOTATION_INK:
-                                if (!string.IsNullOrEmpty(SignatureData.SourcePath))
                                 {
-                                    BitmapImage image = new BitmapImage(new Uri(SignatureData.SourcePath));
-                                    PngBitmapEncoder encoder = new PngBitmapEncoder();
-                                    encoder.Frames.Add(BitmapFrame.Create(image));
-                                    MemoryStream memoryStream = new MemoryStream();
-                                    encoder.Save(memoryStream);
-                                    decoder = BitmapDecoder.Create(memoryStream, BitmapCreateOptions.None, BitmapCacheOption.Default);
+                                    WriteableBitmap writeableBitmap = CreateInkImaget(signatureParam as InkParam);
+                                    byte[] imageArray = new byte[writeableBitmap.PixelWidth * writeableBitmap.PixelHeight * 4];
+                                    writeableBitmap.CopyPixels(imageArray, writeableBitmap.PixelWidth * 4, 0);
+                                    pdfViewerControl.PDFViewTool.GetCPDFViewer().SetStampMouseImage(imageArray, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight);
+                                    pdfViewerControl.PDFViewTool.GetCPDFViewer().SetIsVisibleCustomMouse(true);
+                                    pdfViewerControl.PDFViewTool.GetCPDFViewer().SetIsShowStampMouse(true);
+                                    annotHandlerEventArgs = signatureParam;
                                 }
                                 break;
                             default:
                                 return;
                         }
-                        BitmapFrame frame = null;
-                        if (decoder != null && decoder.Frames.Count > 0)
-                        {
-                            frame = decoder.Frames[0];
-                        }
-                        if (frame != null)
-                        {
-                            byte[] imageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
-                            if (frame.Format != PixelFormats.Bgra32)
-                            {
-                                FormatConvertedBitmap covert = new FormatConvertedBitmap(
-                                    frame,
-                                    PixelFormats.Bgra32,
-                                    frame.Palette,
-                                    0);
-                                covert.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
-                            }
-                            else
-                            {
-                                frame.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
-                            }
-                            pdfViewerControl.PDFViewTool.GetCPDFViewer().SetStampMouseImage(imageArray, frame.PixelWidth, frame.PixelHeight);
-                            pdfViewerControl.PDFViewTool.GetCPDFViewer().SetIsVisibleCustomMouse(true);
-                            pdfViewerControl.PDFViewTool.GetCPDFViewer().SetIsShowStampMouse(true);
-                            annotHandlerEventArgs = signatureParam;
-                        }
                     }
                     break;
                 case CPDFAnnotationType.Link:
@@ -526,6 +530,105 @@ namespace Compdfkit_Tools.PDFControl
 
         }
 
+        private WriteableBitmap CreateInkImaget(InkParam inkParam)
+        {
+            if (inkParam == null)
+            {
+                return null;
+            }
+            if (inkParam.InkPath != null && inkParam.InkPath.Count > 0)
+            {
+                GeometryGroup PaintGeomtry = new GeometryGroup();
+                int minLeft = -1;
+                int minTop = -1;
+                int maxLeft = -1;
+                int maxTop = -1;
+
+                foreach (List<CPoint> Item in inkParam.InkPath)
+                {
+                    for (int i = 0; i < Item.Count; i++)
+                    {
+                        CPoint paintPoint = Item[i];
+
+                        if (minLeft == -1)
+                        {
+                            minLeft = (int)paintPoint.x;
+                            maxLeft = (int)paintPoint.x;
+                            minTop = (int)paintPoint.y;
+                            maxTop = (int)paintPoint.y;
+                        }
+                        else
+                        {
+                            minLeft = (int)Math.Min(minLeft, paintPoint.x);
+                            maxLeft = (int)Math.Max(maxLeft, paintPoint.x);
+                            minTop = (int)Math.Min(minTop, paintPoint.y);
+                            maxTop = (int)Math.Max(maxTop, paintPoint.y);
+                        }
+                    }
+                }
+                if (minLeft >= 0 && maxLeft > minLeft && minTop >= 0 && maxTop > minTop)
+                {
+                    List<List<CPoint>> points = new List<List<CPoint>>();
+
+                    foreach (List<CPoint> Item in inkParam.InkPath)
+                    {
+                        PathGeometry PaintPath = new PathGeometry();
+                        PathFigureCollection Figures = new PathFigureCollection();
+                        PathFigure AddFigure = new PathFigure();
+                        Figures.Add(AddFigure);
+                        PaintPath.Figures = Figures;
+                        PaintGeomtry.Children.Add(PaintPath);
+
+                        List<CPoint> changeList = new List<CPoint>();
+                        for (int i = 0; i < Item.Count; i++)
+                        {
+                            Point paintPoint = new Point(DpiHelper.PDFNumToStandardNum(Item[i].x - minLeft), DpiHelper.PDFNumToStandardNum(Item[i].y - minTop));
+                            changeList.Add(DataConversionForWPF.PointConversionForCPoint(DpiHelper.StandardPointToPDFPoint(paintPoint)));
+                            if (i == 0)
+                            {
+                                AddFigure.StartPoint = paintPoint;
+                            }
+                            else
+                            {
+                                LineSegment AddSegment = new LineSegment();
+                                AddSegment.Point = paintPoint;
+                                AddFigure.Segments.Add(AddSegment);
+                            }
+                        }
+                        if (changeList.Count > 0)
+                        {
+                            points.Add(changeList);
+                        }
+                    }
+                    int drawWidth = (int)DpiHelper.PDFNumToStandardNum(maxLeft - minLeft);
+                    int drawHeight = (int)DpiHelper.PDFNumToStandardNum(maxTop - minTop);
+
+                    inkParam.InkPath = points;
+                    DefaultSettingParam defaultSettingParam = pdfViewerControl.PDFViewTool.GetDefaultSettingParam();
+                    defaultSettingParam.SetAnnotParam(inkParam);
+
+                    DrawingVisual copyVisual = new DrawingVisual();
+                    DrawingContext copyContext = copyVisual.RenderOpen();
+
+                   Color color= ParamConverter.ConverterByteForColor(inkParam.InkColor);
+                    color.A = inkParam.Transparency;
+                    Pen drawPen = new Pen(new SolidColorBrush(color), inkParam.Thickness);
+                    copyContext?.DrawGeometry(null, drawPen, PaintGeomtry);
+                    copyContext.Close();
+                    RenderTargetBitmap targetBitmap = new RenderTargetBitmap(drawWidth, drawHeight, 96, 96, PixelFormats.Pbgra32);
+                    targetBitmap.Render(copyVisual);
+                    byte[] ImageArray = new byte[targetBitmap.PixelWidth * targetBitmap.PixelHeight * 4];
+                    targetBitmap.CopyPixels(new Int32Rect(0, 0, (int)targetBitmap.PixelWidth, (int)targetBitmap.PixelHeight), ImageArray, targetBitmap.PixelWidth * 4, 0);
+
+                    WriteableBitmap writeBitmap = new WriteableBitmap(targetBitmap.PixelWidth, targetBitmap.PixelHeight, 96, 96, PixelFormats.Bgra32, null);
+                    writeBitmap.WritePixels(new Int32Rect(0, 0, targetBitmap.PixelWidth, targetBitmap.PixelHeight), ImageArray, targetBitmap.PixelWidth * 4, 0);
+                    return writeBitmap;
+                }
+
+            }
+            return null;
+        }
+
         public void SetSignature(ref AnnotParam annotParam, CPDFSignatureData stamp)
         {
             switch (stamp.Type)
@@ -557,7 +660,6 @@ namespace Compdfkit_Tools.PDFControl
                         (annotParam as InkParam).Transparency = stamp.inkColor.A;
                         annotParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_INK;
                         List<List<Point>> RawPointList = GetPoints(stamp.DrawingPath);
-                        List<CPoint> linqList = new List<CPoint>();
 
                         if (RawPointList != null && RawPointList.Count > 0)
                         {
@@ -567,8 +669,7 @@ namespace Compdfkit_Tools.PDFControl
                                 List<CPoint> ink = new List<CPoint>();
                                 foreach (Point point in inkPoints)
                                 {
-                                    ink.Add(new CPoint((float)point.X, (float)point.Y));
-                                    linqList.Add(new CPoint((float)point.X, (float)point.Y));
+                                    ink.Add(new CPoint((float)DpiHelper.StandardNumToPDFNum(point.X), (float)DpiHelper.StandardNumToPDFNum(point.Y)));
                                 }
 
                                 if (ink.Count > 0)