Page 1 of 1

QR-code generator (OSX 10.9+)

Posted: Wed Jan 20, 2016 11:14 am
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

Re: QR-code generator (OSX 10.9+)

Posted: Sun Jan 24, 2016 5:42 pm
by mk-soft
Very nice :D

Re: QR-code generator (OSX 10.9+)

Posted: Sun Jan 24, 2016 10:03 pm
by davido
@wilbert,

Thank you for sharing.

Re: QR-code generator (OSX 10.9+)

Posted: Mon Jan 25, 2016 7:04 am
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).

Re: QR-code generator (OSX 10.9+)

Posted: Mon Jan 25, 2016 6:01 pm
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?

Re: QR-code generator (OSX 10.9+)

Posted: Mon Jan 25, 2016 6:07 pm
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

Re: QR-code generator (OSX 10.9+)

Posted: Mon Jan 25, 2016 6:51 pm
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. :)