问题
Does anyone know of a QR code API that will help detect a QR barcode inside a .PDF. I would like to break up the .pdf when a QR code is found
回答1:
There are a lot of SDKs / APIs that are able to detect & decode a QR Barcode inside a PDF. You will also have to consider how you will input the PDF and split it.
You did not specify what platform / programming language you are developing in so I will list a few different ones.
If you are looking for an open-source solution for detecting QR Barcodes, then you can use ZXing:
- ZXing .NET port to .NET and C#, and related Windows platform
If you are looking for a more complete solution, you can look into using a Commercial (non-free) SDK. Just as a disclaimer, I work for LEADTOOLS and we have a Barcode SDK.
LEADTOOLS also has the ability to load and split a multipage PDF as a part of the Barcode SDK and so you can use 1 SDK to achieve what you want. Here is a small C#.NET method that will split a multipage PDF based on QR barcodes found on each of the pages:
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:
Use Spire.Pdf and Spire.Barcode from Nuget.
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;
}
来源:https://stackoverflow.com/questions/44335923/qr-code-api-needed-to-detect-qr-code-inside-pdf-using-c-sharp