Create a QR Code from OCR Results

Posted on 2021-03-08 Zac Ferraresi

We take great pride in the fact that LEADTOOLS is truly a comprehensive imaging SDK. We developed LEADTOOLS with the mindset that we want it to be a one-stop-shop for imaging. There are many applications, especially in the document imaging world, where technologies intersect and you may find the need to piecemeal multiple SDKs together. For example, Scanning + Document Cleanup + OCR, OMR + Forms Recognition + OCR, or OCR + PDF. How about something a little less common, like OCR + Barcode?

Huh? Those don't really go together do they?

Most of the time you probably think of OCR and Barcodes as the last step in a document imaging process:

1. Scan an image
  --> 2. Clean the image
        --> 3.a. Use OCR to get the text
        --> 3.b. Use Barcode to get encoded data

That may be true, but you're only thinking about reading. Creating barcodes, especially QR codes, is still an important and popular task. Plus, they can pack in a ton of information so you can encode and squeeze all the text from a document into a QR code. Or use zonal OCR to convert just a block of text into a QR code. Or even better yet, use your mobile phone to capture a picture of the text you want to convert into a QR code.

The Code

It doesn't take much to do either of these advanced imaging processes with LEADTOOLS. Check out the following code snippet that loads an image, OCRs the text, then creates a QR Code on a blank image.


// Create and use OCR to get text from the image
using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, false))
{
   ocrEngine.Startup(null, null, null, @"LEADTOOLS_INSTALL_DIRECTORY\Bin\Common\OcrLEADRuntime");

   using (IOcrPage ocrPage = ocrEngine.CreatePage(ocrEngine.RasterCodecsInstance.Load(imagePath, 1), OcrImageSharingMode.AutoDispose))
   {
      ocrPage.AutoZone(null);
      ocrPage.Recognize(null);
      string recognizedCharacters = ocrPage.GetText(-1);

      // Create a QR code using the text
      BarcodeEngine engine = new BarcodeEngine();
      int resolution = 300;
      using (RasterImage image = RasterImage.Create((int)(8.5 * resolution), (int)(11.0 * resolution), 1, resolution, RasterColor.FromKnownColor(RasterKnownColor.White)))
      {
         BarcodeWriter writer = engine.Writer;

         QRBarcodeData data = BarcodeData.CreateDefaultBarcodeData(BarcodeSymbology.QR) as QRBarcodeData;

         data.Bounds = new LeadRect(0, 0, image.ImageWidth, image.ImageHeight);
         QRBarcodeWriteOptions writeOptions = writer.GetDefaultOptions(data.Symbology) as QRBarcodeWriteOptions;
         writeOptions.XModule = 30;
         writeOptions.HorizontalAlignment = BarcodeAlignment.Near;
         writeOptions.VerticalAlignment = BarcodeAlignment.Near;
         data.Value = recognizedCharacters;

         writer.CalculateBarcodeDataBounds(new LeadRect(0, 0, image.ImageWidth, image.ImageHeight), image.XResolution, image.YResolution, data, writeOptions);

         writer.WriteBarcode(image, data, writeOptions);
      }
   }
}

Download the Full Example

To download the full example and see more information on the classes and methods used in this code tip, check out the original forum post.

LEADTOOLS Blog

LEADTOOLS Powered by Apryse,the Market Leading PDF SDK,All Rights Reserved