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