首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用C#检测.pdf内部的二维码所需的二维码API

使用C#检测.pdf内部的二维码所需的二维码API
EN

Stack Overflow用户
提问于 2017-06-03 03:12:29
回答 2查看 6.5K关注 0票数 3

有人知道二维码接口可以帮助检测.PDF内部的二维码吗?当发现二维码时,我想拆开.pdf

EN

回答 2

Stack Overflow用户

发布于 2017-07-01 03:21:47

有很多SDK/API能够检测和解码PDF中的QR条形码。您还必须考虑如何输入PDF并将其拆分。

您没有指定您正在开发的平台/编程语言,所以我将列出几个不同的平台/编程语言。

如果您正在寻找用于检测二维码的开源解决方案,那么您可以使用ZXing:

  • ZXing .NET端口连接到.NET和C#,以及相关的Windows platform

如果您正在寻找更完整的解决方案,您可以考虑使用商业(非免费) SDK。作为免责声明,我为LEADTOOLS工作,我们有一个Barcode SDK

LEADTOOLS还具有加载和拆分多页PDF的能力,作为条形码SDK的一部分,因此您可以使用1个SDK来实现您想要的功能。这是一个小的C#.NET方法,它将根据在每个页面上找到的QR条形码来拆分多页PDF:

代码语言:javascript
复制
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");
}
票数 2
EN

Stack Overflow用户

发布于 2018-04-20 07:18:57

使用Nuget中的Spire.Pdf和Spire.Barcode。

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44335923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档