QR-code generator (OSX 10.9+)

Mac OSX specific forum
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

QR-code generator (OSX 10.9+)

Post by wilbert »

A small QR-code generator for OSX 10.9 and above.
Tested with PB 5.41 on OSX 10.11 (El Capitan).

Code: Select all

; QRCode generator (OSX 10.9+)


Procedure QRImage(Message.s)
  
  ; Convert Message to NSData object
  Protected StringData = CocoaMessage(0, CocoaMessage(0, 0, "NSString stringWithString:$", @Message), "dataUsingEncoding:", #NSUTF8StringEncoding)
  
  ; Generate the QRCode image
  Protected QRCodeGenerator = CocoaMessage(0, 0, "CIFilter filterWithName:$", @"CIQRCodeGenerator")
  CocoaMessage(0, QRCodeGenerator, "setValue:", StringData, "forKey:$", @"inputMessage")
  CocoaMessage(0, QRCodeGenerator, "setValue:$", @"M", "forKey:$", @"inputCorrectionLevel")
  Protected OutputImage = CocoaMessage(0, QRCodeGenerator, "outputImage")
  
  ; Get the extent of the image
  Protected Extent.NSRect
  CocoaMessage(@Extent, OutputImage, "extent")
  
  ; Create a PB image and fill it with the QRCode image
  Protected ZeroPoint.NSPoint, Delta.CGFloat = 1
  Protected PBImage = CreateImage(#PB_Any, Extent\size\width, Extent\size\height) 
  StartDrawing(ImageOutput(PBImage))
  CocoaMessage(0, OutputImage, "drawAtPoint:@", @ZeroPoint, "fromRect:@", @Extent, "operation:", #NSCompositeCopy, "fraction:@", @Delta)
  StopDrawing()
  
  ; Return the image
  ProcedureReturn PBImage
  
EndProcedure


; Generate and show test image 

If OpenWindow(0, 0, 0, 128, 128, "QRCode example (OSX 10.9+)", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  QRImage = QRImage("http://www.purebasic.com")
  ResizeImage(QRImage, ImageWidth(QRImage) << 2, ImageHeight(QRImage) << 2, #PB_Image_Raw)
  ImageGadget(0, 10, 10, 108, 108, ImageID(QRImage))
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: QR-code generator (OSX 10.9+)

Post by mk-soft »

Very nice :D
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: QR-code generator (OSX 10.9+)

Post by davido »

@wilbert,

Thank you for sharing.
DE AA EB
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: QR-code generator (OSX 10.9+)

Post by wilbert »

davido wrote:Thank you for sharing.
You're welcome :)

Besides doing what it does (generating a QR-code image) it's also a way of demonstrating how to use one of the CIFilter generator filters since there are more of them (like generating a sun beam or checkerboard pattern).
Windows (x64)
Raspberry Pi OS (Arm64)
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: QR-code generator (OSX 10.9+)

Post by WilliamL »

Hey, that pretty cool!

I'm guessing the QR images take the place of bar codes. I suppose I will have to read up on the differences and advantages.

I was wondering if there was a bar code generator?
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: QR-code generator (OSX 10.9+)

Post by wilbert »

WilliamL wrote:I was wondering if there was a bar code generator?
Yes, but they require a higher OSX version.

CIAztecCodeGenerator => OSX 10.10
CICode128BarcodeGenerator => OSX 10.10
CIPDF417BarcodeGenerator => OSX 10.11

If one of those is what you are looking for, I can give it a try.

For a complete filter reference see
https://developer.apple.com/library/mac ... index.html
Windows (x64)
Raspberry Pi OS (Arm64)
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: QR-code generator (OSX 10.9+)

Post by WilliamL »

wilbert

I can't really think of a good use for it right now so it's not that important. I just wondered if it would be as easy as the QR Code.

It's interesting that there are so many versions depending on the operating system.

I'm using 10.11.3 now and Mail still doesn't make sounds. :)
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1
Post Reply