LibGDX-QR provides you users with both QR Code generation and QR Code scanning by wrapping several external libraries and native OS APIs.
A demo application is available here
It is also available on the Google Play Store here
This library uses the QR generation functionality of the Zebra Crossing Barcode library https://github.com/zxing/zxing to generate a bit matrix representing a code then uses https://github.com/earlygrey/shapedrawer to render the QR code to a framebuffer.
Finally, the texture is extracted from the frame buffer and returned.
Currently, Android and IOS support QR Code scanning (there is no intention of supporting GWT and Desktop but feel free to do so yourself).
QR scanning on Android is provided by Google's play-services-vision. LibGDX-QR creates a new Activity which implements all QR scanning functionality from this library and returns the first code scanned.
QR scanning on IOS is provided by Apple's native AVCapture libraries. LibGDX-QR creates a new View Controller which implements all QR scanning functionality and returns the first code scanned.
Currently, Binary QR codes are supported on Android but not IOS. As far as I can tell, IOS doesn't expose an API for accessing the data returned by non String based QR codes.
Add Jitpack if you don't already have it
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
Add the dependencies for which ever platforms you are targeting.
NOTE: The Android and IOS native projects are only required if you want to scan QR codes, if you just want to generate them then only the core project is required.
project(":core") {
dependencies {
implementation 'com.github.zeroed-tech.LibGDX-QR:core:master-SNAPSHOT'
}
}
project(":android") {
dependencies {
implementation 'com.github.zeroed-tech.LibGDX-QR:AndroidNative:master-SNAPSHOT'
}
}
project(":ios") {
dependencies {
implementation 'com.github.zeroed-tech.LibGDX-QR:IOSNative:master-SNAPSHOT'
}
}
Each platform has some extra things that need to be configured get things up and running
- Change the value of
minSdkVersion
to 16 or higher inandroid/build.gradle
- Add
QRCode.init(new AndroidQRCodeNativeInterface(this));
toAndroidLauncher.java
- Add the following to your
AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
- Add
QRCode.init(new IOSQRCodeNativeInterface());
toIOSLauncher.java
- Add the following value to
info.plist
. Feel free to change the text between<string></string>
<key>NSCameraUsageDescription</key>
<string>Needed to support QR code scanning</string>
Do you want to support a platform that I haven't? Do you think my Android Activity or IOS View Controller looks horrible (I don't blame you)? Feel free to make your own!
To do so simply implement NativeInterface
, perform your QR scanning login and be sure to call QRCode.QRCodeScanned(code);
when you're done.
I haven't read the QR spec so I've almost certainly gotten the terminology wrong in the code. Here is what I've referred to everything as:
- Block - a single square of the QR code
- Eye - the large (7x7) squares in the top left, top right and bottom left corners
Start by creating a QRGenerator and specifying a block size (the number of pixels each square should take up:
QRGenerator generator = new QRGenerator(15)
Configure your generator
// Change you block size after you forgot to set it in the constructor
generator.blockSize(10)
// Change the border size. The border is the number of block of white space should be placed around your QR code (0-1 is usually enough)
generator.borderSize(1)
// Set the primary (block) and secondary (background) colours
generator.primaryColor(Color.Black)
generator.secondaryColor(Color.White)
// Set the eye border shape (ARC, CIRCLE or SQUARE)
generator.setEyeBorderShape(QRGenerator.Shape.ARC)
// Set the blocks inside the eyes shape (CIRCLE or SQUARE)
generator.setEyeInnerShape(QRGenerator.Shape.CIRCLE)
// Set the blocks shape (CIRCLE or SQUARE)
generator.setInnerShape(QRGenerator.Shape.SQUARE)
Generate your QR code
// Generate your QR code
TextureRegion code = generator.generate("Zeroed.tech")
// Do things with your code, don't forget to dispose of it when you're done
String input = "Zeroed.tech"
// Generate a basic QR code
new QRGenerator(12).generate(input)
// Generate a QR code with arcs on the eye borders
new QRGenerator(12).setEyeBorderShape(QRGenerator.Shape.ARC).generate(input)
// Generate a QR code with arcs on the eye borders and circular inner bits
new QRGenerator(12).setEyeBorderShape(QRGenerator.Shape.ARC).setEyeInnerShape(QRGenerator.Shape.CIRCLE).generate(input)
// Generate a QR code with arcs on the eye borders and circular everything else
new QRGenerator(12).setEyeBorderShape(QRGenerator.Shape.ARC).setEyeInnerShape(QRGenerator.Shape.CIRCLE).setInnerShape(QRGenerator.Shape.CIRCLE).generate(input)
// Generate a QR code where everything is a circle
new QRGenerator(12).setEyeBorderShape(QRGenerator.Shape.CIRCLE).setEyeInnerShape(QRGenerator.Shape.CIRCLE).setInnerShape(QRGenerator.Shape.CIRCLE).generate(input)
// Change up the colors
new QRGenerator(12).primaryColor(Color.WHITE).secondaryColor(Color.BLACK).generate(input)
new QRGenerator(12).primaryColor(Color.GREEN).secondaryColor(Color.BLACK).generate(input)
// Generate a QR code with a larger border
new QRGenerator(12).borderSize(3).generate(input)
// Generate a larger QR code
new QRGenerator(20).generate(input)
After performing any necessary platform specific setup simply call QRCode.scanQRCode(CodeScanned)
with a CodeScanned callback object and you're good to go