使用C#检测PDF内的QR码需要QR码API。

3
有没有一个QR码API可以帮助检测PDF内的QR条形码?发现QR码后,我想拆分PDF。
2个回答

2

请从Nuget使用Spire.Pdf和Spire.Barcode。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

using System.Web.UI;
using System.Web.UI.WebControls;
using Spire.Pdf;
using Path = System.IO.Path;

namespace MyTask
{
    public partial class Index : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            GetImages(@"C:\Users\prave\Desktop\my.pdf", @"C:\Users\prave\Desktop");
        }

        private static void GetImages(string sourcePdf, string outputPath)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C:\Users\prave\Desktop\my.pdf");

            for (int i = 0; i < doc.Pages.Count; i++)
            {
                PdfPageBase page = doc.Pages[i];

                System.Drawing.Image[] images = page.ExtractImages();
                outputPath = Path.Combine(outputPath, String.Format(@"{0}.jpg", i));
                foreach (System.Drawing.Image image in images)
                {
                    string QRCodeString = GetQRCodeString(image,outputPath);
                    if (QRCodeString == "")
                    {
                        continue;
                    }

                    break;

                }



            }
        }

        private static string GetQRCodeString(System.Drawing.Image img, string outPutPath)
        {

            img.Save(outPutPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            string scaningResult = Spire.Barcode.BarcodeScanner.ScanOne(outPutPath);
            File.Delete(outPutPath);

            return scaningResult;
        }

2
有许多SDK / API能够在PDF中检测和解码QR条形码。您还需要考虑如何输入PDF并将其拆分。
您没有说明正在使用的平台/编程语言,因此我将列出几个不同的选择。
如果您正在寻找用于检测QR条形码的开源解决方案,则可以使用ZXing:
  • ZXing .NET 适用于.NET和C#的端口以及相关Windows平台
如果您正在寻找更完整的解决方案,可以考虑使用商业(非免费)SDK。作为免责声明,我为LEADTOOLS工作,并且我们有一个Barcode SDK
LEADTOOLS还具有将多页PDF加载和拆分为Barcode SDK的一部分的功能,因此您可以使用1个SDK来实现所需的功能。以下是一个小的C#.NET方法,它将基于每个页面上找到的QR条形码拆分多页PDF:
static void SplitPDF(string filename)
{
   BarcodeEngine barcodeEngine = new BarcodeEngine();
   List<int> PageNumbers = new List<int>();
   using (RasterCodecs codecs = new RasterCodecs())
   {
      int totalPages = codecs.GetTotalPages(filename);
      for (int page = 1; page <= totalPages; page++)
         using (RasterImage image = codecs.Load(filename, page))
         {
            BarcodeData barcodeData = barcodeEngine.Reader.ReadBarcode(image, LogicalRectangle.Empty, BarcodeSymbology.QR);
            if (barcodeData != null) // QR Barcode found on this image
               PageNumbers.Add(page);
         }
   }

   int firstPage = 1;
   PDFFile pdfFile = new PDFFile(filename);
   //Loop through and split the PDF according to the barcodes
   for(int i= 0; i < PageNumbers.Count; i++)
   {
      string outputFile = $"{Path.GetDirectoryName(filename)}\\{Path.GetFileNameWithoutExtension(filename)}_{i}.pdf";
      pdfFile.ExtractPages(firstPage, PageNumbers[i] - 1, outputFile);
      firstPage = PageNumbers[i]; //set the first page to the next page
   }
   //split the rest of the PDF based on the last barcode
   if (firstPage != 1)
      pdfFile.ExtractPages(firstPage, -1, $"{Path.GetDirectoryName(filename)}\\{Path.GetFileNameWithoutExtension(filename)}_rest.pdf");
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接