This library uses the zxing library, which provides a simple API to create QR codes.
The entry point for this library is the QrCodeFactory
,
which will give you an instance of the QrCodeApi
to create a QR code as BufferedImage
.
This BufferedImage
can be configured by passing an instance of QrCodeConfig
and use for further image processing.
Examples for Java or Kotlin can be found in the java-sample
or kotlin-sample
module.
The qr-code-app project makes it even easier to create QR codes with this library.
Just run the qr-code-app
project and create your QR code with a nice UI.
./gradlew :qr-code-app:run
Or download the appropriate distribution from the releases section.
More details about the application can be found here
The app you can also generate the code for you, so you can just copy and paste it into your project.
Getting a released version:
dependencies {
implementation("io.github.simonscholz:qr-code-with-logo:0.3.0")
}
Using the snapshot version:
repositories {
mavenCentral()
maven(
url = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
)
}
dependencies {
implementation("io.github.simonscholz:qr-code-with-logo:0.4.0-SNAPSHOT")
}
import io.github.simonscholz.qrcode.QrCodeFactory.createQrCodeApi
import io.github.simonscholz.qrcode.DEFAULT_IMG_SIZE
import io.github.simonscholz.qrcode.QrCodeConfig
import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO
// extension function to write a BufferedImage to a file
fun BufferedImage.toFile(file: File) {
ImageIO.write(this, "png", file)
}
val qrCodeApi = createQrCodeApi()
qrCodeApi.createQrImage(QrCodeConfig("https://simonscholz.github.io/", DEFAULT_IMG_SIZE))
.toFile(File("qr-with-defaults-kotlin.png"))
For more advanced examples, please have a look at the kotlin-sample
module.
import io.github.simonscholz.qrcode.QrCodeApi;
import io.github.simonscholz.qrcode.QrCodeConfig;
import io.github.simonscholz.qrcode.QrCodeFactory;
import javax.imageio.ImageIO;
import java.io.File;
import static io.github.simonscholz.qrcode.QrCodeConfigKt.DEFAULT_IMG_SIZE;
final QrCodeApi qrCodeApi = QrCodeFactory.createQrCodeApi();
final var qrCode = qrCodeApi.createQrImage(new QrCodeConfig("https://simonscholz.github.io/", DEFAULT_IMG_SIZE));
ImageIO.write(qrCode, "png", new File("qr-with-defaults-java.png"));
For more advanced examples, please have a look at the java-sample
module.
All the examples you can see here are implemented in the java-sample
and kotlin-sample
modules.
The qr-code-app
project makes it even easier to create QR codes with this library,
because it offers a UI to create QR codes and also offers a UI for the QR code design.
It can even generate the code for you, so you can just copy and paste it into your project.
Using the defaults only:
Adding a center logo:
Adding a border:
Adding radius to positional squares:
Having circles as positional "squares":
Add some decent red color:
Make it look like a Minecraft Creeper QR code:
Make the QR code transparent and draw it onto a background image:
To see what’s possible with colors, have a look at the rainbow example in the bad examples below.
The library comes with some predefined dot shapes, but you can also create your own dot shapes.
Predefined dot shapes of the library:
-
QrCodeDotShape.SQUARE
-
QrCodeDotShape.ROUNDED_SQUARE
-
QrCodeDotShape.CIRCLE
-
QrCodeDotShape.HEXAGON
-
QrCodeDotShape.TRIANGLE
-
QrCodeDotShape.HEART
-
QrCodeDotShape.HOUSE
-
QrCodeDotShape.STAR
-
QrCodeDotShape.DIAMOND
-
QrCodeDotShape.CROSS
The app and samples show how to use these predefined dot shapes or create your own dot shapes.
If you want to see more examples, just run the sample code or the app.
The following example shows how to use the predefined dot shapes:
import io.github.simonscholz.qrcode.QrCodeConfig
import io.github.simonscholz.qrcode.QrCodeDotShape
import io.github.simonscholz.qrcode.QrCodeFactory
import java.awt.Color
import java.awt.Graphics2D
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import javax.imageio.ImageIO
fun main() {
val path = Paths.get(System.getProperty("user.home"), "qr-code-samples")
Files.createDirectories(path)
val qrCodeDir = path.toAbsolutePath().toString()
val qrCodeApi = QrCodeFactory.createQrCodeApi()
QrCodeConfig.Builder("https://simonscholz.github.io/")
.qrCodeDotStyler(QrCodeDotShape.STAR)
.qrCodeSize(800)
.build()
.run {
qrCodeApi.createQrCodeImage(this)
.toFile(File(qrCodeDir, "/qr-with-STAR-dots-kotlin.png"))
}
}
The following example shows how to create a custom dot shape:
import io.github.simonscholz.qrcode.QrCodeConfig
import io.github.simonscholz.qrcode.QrCodeDotShape
import io.github.simonscholz.qrcode.QrCodeFactory
import java.awt.Color
import java.awt.Graphics2D
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import javax.imageio.ImageIO
fun main() {
val path = Paths.get(System.getProperty("user.home"), "qr-code-samples")
Files.createDirectories(path)
val qrCodeDir = path.toAbsolutePath().toString()
val qrCodeApi = QrCodeFactory.createQrCodeApi()
QrCodeConfig.Builder("https://simonscholz.github.io/")
.qrCodeDotStyler(::drawSmiley)
.qrCodeSize(800)
.build()
.run {
qrCodeApi.createQrCodeImage(this)
.toFile(File(qrCodeDir, "/qr-with-SMILEY-dots-kotlin.png"))
}
private fun drawSmiley(x: Int, y: Int, dotSize: Int, graphics: Graphics2D) {
drawDotImage(x, y, dotSize, graphics, "smiley_fill.png")
}
private fun drawDotImage(x: Int, y: Int, dotSize: Int, graphics: Graphics2D, image: String) {
val resource = Main::class.java.getClassLoader().getResource(image)
resource?.let {
val imageDot = ImageIO.read(it)
graphics.drawImage(imageDot, x, y, dotSize, dotSize, null)
}
}
}
Besides just drawing a logo with the Graphics2D
object, you can also draw whatever you want.
For more advanced examples and Java usage, please have a look at the kotlin-sample
or java-sample
modules.
In order to create certain types of QR codes, you can may want to use the following utils.
Please see kotlin-sample
module or java-sample
module to use these types and run the samples.
String url = SimpleTypes.url("https://simonscholz.github.io/");
String geolocation = SimpleTypes.geolocation(53.59659752940634, 10.006589989354053);
String email = SimpleTypes.email("[email protected]", "Hello World", "This is a test email");
String phoneNumber = SimpleTypes.phoneNumber("+49 176 12345678");
String sms = SimpleTypes.sms("+49 176 12345678", "Hello, this is a test SMS");
These String values can be used to create a QR code.
VEVENT type:
LocalDateTime startDateTime = LocalDateTime.now().plusWeeks(2);
VEvent vevent = new VEvent.Builder("QR Codes with Kotlin & Java")
.location("Java User Group Hamburg")
.startDate(startDateTime)
.endDate(startDateTime.plusHours(2))
.description("Let's create QR Codes with Kotlin & Java")
.build();
String vEventQrCodeText = vevent.toVEventQrCodeText();
The vEventQrCodeText
can then be used to create a QR code.
VCARD type:
VCard vCard = new VCard.Builder("Simon Scholz")
.email("[email protected]")
.address("Main Street 1", "Hamburg", "22855")
.organization("Self Employed")
.phoneNumber("+49 176 12345678")
.website("https://simonscholz.github.io/")
.build();
String vCardQrCodeText = vCard.toVCardQrCodeText();
The vCardQrCodeText
can then be used to create a QR code.
The logo image can be added as file or as Base64 encoded string.
The qr code image can also be returned as Base64 encoded string.
Also the the qr-code-app application, which can also deal with Base64 encoded strings for the logo and the qr code image.
Scaling down the logo image in advance will improve the image quality of the logo.
But compare yourself:
Logo with 500x500px (Qr Code size=300px):
Logo with 60x60px (Qr Code size=300px):
Note
|
I chose 60x60px, because the default logo relative size is 0.2, which means 20% of the QR code size, which is 60x60px when having a 300x300px qr code (300*0.2=60). |
The reason for this is the fact that the scaling capabilities of awt are not that good.
So you should either scale the logo manually or use the following beforehand:
-
java.awt.Image.getScaledInstance(int width, int height, int hints)
Not having enough contrast:
The rainbow QR code to depict what’s possible:
These are bad examples, because the colors are not contrasting enough.
So please be cautious when changing the colors! Not having enough contrast may cause that qr code scanners are not capable to read the qr code! If you try to scan the bad exmaple qr codes from above, you will see that it’s not working.
The qr-code-app
project is a simple Swing application, which uses this library to create QR codes.
-
Create QR codes with a UI
-
Offers a UI for simple QR codes (url, email, phone number, sms, geolocation)
-
Offers a UI for more complex QR codes (vcard, vevent)
-
Offers properties panel for the QR code design (colors, shapes, logo, etc.)
-
Offers a preview panel to see the QR code design
-
Logo can be added as file or base64 encoded string
-
Save the QR code as image
-
Copy the QR code to the clipboard
-
Copy the QR code as Base64 encoded string to the clipboard
-
Save/Export the QR code config/design to a file for later or sharing with others
-
Load/Import a QR code from a config/design file
-
Copy sample code based on current config to the clipboard (Java or Kotlin)
Feel free to ask for new features or create a pull request if you want to add more features.
The latest config is stored in the operating systems app data directory (~/.config/qr-code-app or C:\AppData\qr-code-app) when closing the application. On startup of the application this config is loaded and used to create a QR code.
The config can be shared with others by saving it via File > Export Config
or CTRL+E
shortcut to a file and sending it to others.
Others can then load the config via File > Import Config
or CTRL+I
shortcut.
The good thing is that also the logo image is stored in the config file, so you don’t have to send the logo image separately.
This is archived by converting the logo image to a Base64 encoded string.
The releases section offers distributions for different operating systems,
which consists of a qr-code-app
and qr-code-app.bat
file in the /bin
folder, which can be executed directly.
(does not require Java to be installed on your system)
Thank you so much @lome for providing this awesome code at https://github.com/lome/niceqr, where I gained a lot of ideas and adopted some parts of the code.