Skip to content

Create and Scan Barcodes and QR Codes on Mac, Windows and iOS

Xojo 2024r1 introduces the Barcode class for Windows, macOS and iOS. Now you can create barcodes and QR codes and scan up to twelve other types of codes with your Mac, Windows or iOS cameras. In addition, you can now identify codes from an image provided as a parameter.

Generate Codes

The ability to generate codes and identify them from images is available as a shared method for the indicated platforms, in desktop and console apps. The latter is interesting because it means that you can use this functionality in web apps deployed on Windows or Mac servers.

Creating QR or Bar128 codes couldn’t be simpler. Since it uses a shared method, it is not even necessary to have previously created an instance of the class, you only need to invoke the method:

Barcode.Image(value As String, width As Integer, height As Integer, type As Barcode.Types = Barcode.Types.QR) As Picture

Where the expected parameters are the following:

  • Value: Source text string that will be converted to the indicated code type
  • Width: Width of the resulting image with the generated code
  • Height: Height of the resulting image with the generated code
  • Type: The type of code you want to generate. It can be QR or Bar128; these values are available in the Barcode.Types enumerator

Once the method is invoked, you will obtain an image (Picture) that you can use in your app; or you will receive an InvalidArgumentException if some of the parameters provided are invalid, such as passing an empty text string as the data source.

For example, the following line of code generates a 120 x 120 image corresponding to a QR code with the text provided:

Var p As Picture = Barcode.Image("Xojo Rocks!", 120, 120, Barcode.Types.QR)

Recognize Codes in Images

In the same way it is possible to generate code images from text, it is also possible to recognize up to twelve different types of codes from an image. To do this we will use another shared method of the Barcode class:

Barcode.FromImage(image As Picture) As String()

As you can see, in this case the method will return an array of Strings with the values of the recognized codes. It is possible that the image contains more than one code; but it is also possible that no code could be recognized in the provided image, so it is advisable to check the limits of the received array before proceeding.

For example, this line of code will recognize the codes available in the MyPicture image and return the values in the RecognizedCodes() array:

Var RecognizedCodes() As String = Barcode.FromImage(MyPicture)

Scan Codes

Scanning QR codes (or any of the other supported codes) on desktop is only available for macOS and Windows platforms. This is due to desktop presenting a user interface where you can preview the image captured by the selected camera. You can select the camera to use to scan from those connected to the computer (including, for example, iOS devices in the case of Mac, through Continuity; or Android devices paired with your Windows computer). In the case of Mac, the ability to scan images is only available using macOS 10.15 or higher.

To scan codes directly using any of the cameras connected to the computer it is necessary to have previously created an instance of the Barcode class (for example, drag the class from the IDE Library onto the tray area in the Layout Editor window):

Doing so will add it to the Project Browser where we can select it and implement any of the three available Event Handlers:

  • ScanCancelled: The user has canceled the code scanning operation, such as by closing the presented UI window
  • ScanCompleted: Receive the value as String corresponding to the code detected by the camera
  • ScanFailed: Raised when some type of error has occurred, such as the fact that no camera is detected

To display the code scanning UI, invoke the StartScan method on the created Barcode instance. For example:

Barcode1.StartScan

The scan UI does not close automatically when a code is recognized to allow scanning a series of codes (such as in the case of inventory), so it is also possible to stop the operation by code by calling the StopScan method. For example, you can include this line of code in the ScanCompleted event of the Barcode1 instance after the received value has been processed:

me.StopScan

As expected, invoking the StopScan method from code will close the code scanning UI.

Conclusion

Now you can create all types of applications on macOS, Windows and iOS with the ability to generate and scan QR codes, Barcodes and other types of codes, opening up a wide field of possibilities for Xojo users.

Javier Menendez is an engineer at Xojo and has been using Xojo since 1998. He lives in Castellón, Spain and hosts regular Xojo hangouts en español. Ask Javier questions on Twitter at @XojoES or on the Xojo Forum.