Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Lazarus Pascal - macOS - Fast Scan QR code from image with macOS CoreImage

1 Posts
1 Users
0 Reactions
335 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

While tinkering with some of the newer Apple frameworks, I ran into this very fast function in CoreImage to quickly find and scan QR codes.
Works great in Lazarus Pascal /FPC, so I though I'd share this here.

The CoreImage QR code scanner even picks up multiple QR code's in an image. And FAST.

Note that you may need to set the compiler option "-WM11.0".

Code:

I just made a simple form with a TButton, TImage, TLabel, TOpenDialog.
It allows you to select a picture and scans for QR codes right away.
The text of the QR code will be placed in the caption of Label1.

I've tested this with the latest Lazarus Pascal (trunk) but it looks like this will work in a release version as well.

unit Unit1;

{$mode objfpc}{$H+}
{$modeswitch objectivec1}
{$modeswitch objectivec2}
{$modeswitch cvar}
{$modeswitch cblocks}
{$packrecords c}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls
  , CocoaAll, CocoaUtils;

type
  { TForm1 }

  TForm1 = class(TForm)
    btnDetectQR: TButton;
    Image1: TImage;
    Label1: TLabel;
    OpenDialog1: TOpenDialog;
    procedure btnDetectQRClick(Sender: TObject);
  private

  public

  end;


var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.btnDetectQRClick(Sender: TObject);
var
  QRCodeImage : NSImage;
  MyCIImage   : CIImage;
  Detector    : CIDetector;
  Features    : NSArray;
  Feature     : CIFeature;
  Filename    : string;
begin
  if OpenDialog1.Execute then
    begin
      Filename := OpenDialog1.FileName;
      Image1.Picture.LoadFromFile(Filename);
      QRCodeImage := NSImage.alloc.initWithContentsOfFile(NSSTR(Filename));

      MyCIImage := CIImage.imageWithData(QRCodeImage.TIFFRepresentation);
      Detector  := CIDetector.detectorOfType_context_options(CIDetectorTypeQRCode, nil, nil);
      Features  := Detector.featuresInImage(MyCIImage);

      label1.caption:='';
      for Feature in Features do
      begin
        if Feature.isKindOfClass(CIQRCodeFeature) then
          label1.Caption:=label1.Caption+NSStringToString(CIQRCodeFeature(Feature).messageString_)+LineEnding;
      end;
    end;
end;

end.

Hopefully this is useful to someone, and at the least its a nice reference for myself in case I need this in the future 😉 


   
ReplyQuote
Share: