Java qr code генератор

Simple Java QR Code Generator Example

Quick Response Codes (QR Codes) are becoming one of the most popular barcode systems in the world, thanks to smartphones. Their usage is really versatile, from automotive, product packaging to online ticket services. Business started using QR Codes on their documents, business cards and advertisement to quickly reach them. In this tutorial we show you how to generate QR Codes. First, we create a simple QR Code for a website URL address. In the second example, we use a VCard to generate a QR Code. A vCard is a file format standard for electronic business cards.

A barcode is a machine-readable optical label that contains information about the item to which it is attached. A QR Code (abbreviated from Quick Response Code) is the trademark for a type of matrix barcode (or two-dimensional barcode) first designed for the automotive industry in Japan. More recently, the system has become popular outside of the industry due to its fast readability and comparatively large storage capacity. A QR code uses black modules arranged in a square pattern on a white background. The information can be made up of four standardised encoding modes (numeric, alphanumeric, byte/binary, and kanji) to efficiently store data.

Maven Dependencies

We use Apache Maven to manage our project dependencies. Include the following dependency in the projects pom.xml file.

Читайте также:  Css templates background images

QRGen uses Zebra Crossing (ZXing) under the hood. ZXing is a liberal open source library, which can generate/parse almost all barcodes, including QR Codes. The downside is that you must write bloated code in order to generate a simple QR Code. QRGen wrote a layer on top of ZXing, exposing the power of the framework with a simple to use – builder pattern inspired – API programming model.

Create QR Code

Using QRGen you can easily create a QR code. It uses a builder pattern inspired API programming model. Which means you can simple chain every method an thus reducing the boiler plate code, you otherwise have to write. We start with the QRCode.from() static method where we pass in the URL of our website. Next, we configure the necessary configurations like: width, image type, etc. Then, we call the stream() method. This returns a ByteArrayOutputStream with the corresponding generated QR Code. Finally, we write the output stream to disk.

package com.memorynotfound.qrcode; import net.glxn.qrgen.core.image.ImageType; import net.glxn.qrgen.javase.QRCode; import java.io.*; public class CreateQrCode < public static void main(String. args)< ByteArrayOutputStream bout = QRCode.from("https://memorynotfound.com") .withSize(250, 250) .to(ImageType.PNG) .stream(); try < OutputStream out = new FileOutputStream("/tmp/qr-code.png"); bout.writeTo(out); out.flush(); out.close(); >catch (FileNotFoundException e) < e.printStackTrace(); >catch (IOException e) < e.printStackTrace(); >> >

Output

simple java qr code generator example

Create QR Code with VCard

This example uses the same programming model as before. Instead of writing a URL, we include a VCard . A vCard is a file format standard for electronic business cards, which contains meta-data like: name, address, company, etc. First, we create an instance of the VCard . Next, we use the QRCode.from() static method where we pass in the VCard instance. Next, we configure the necessary configurations like: width, image type, etc. Then, we call the stream() method. This returns a ByteArrayOutputStream with the corresponding generated QR Code. Finally, we write the output stream to disk.

package com.memorynotfound.qrcode; import net.glxn.qrgen.core.image.ImageType; import net.glxn.qrgen.core.vcard.VCard; import net.glxn.qrgen.javase.QRCode; import java.io.*; public class CreateQrCodeVCard < public static void main(String. args)< VCard vCard = new VCard(); vCard.setName("memorynotfound.com"); vCard.setAddress("street 1, xxxx address"); vCard.setCompany("company Inc."); vCard.setPhoneNumber("+xx/xx.xx.xx"); vCard.setTitle("title"); vCard.setEmail("[email protected]"); vCard.setWebsite("https://memorynotfound.com"); ByteArrayOutputStream bout = QRCode.from(vCard) .withSize(250, 250) .to(ImageType.PNG) .stream(); try < OutputStream out = new FileOutputStream("/tmp/qr-code-vcard.png"); bout.writeTo(out); out.flush(); out.close(); >catch (FileNotFoundException e) < e.printStackTrace(); >catch (IOException e) < e.printStackTrace(); >> >

Output

simple java qr code generator example

QR Code with VCard Example

QRGen QR Code Generator Usages

Here is a usage overview of the QRGen programming API model. When using special characters don’t forget to supply the correct encoding.

// get QR file from text using defaults File file = QRCode.from("Hello World").file(); // get QR stream from text using defaults ByteArrayOutputStream stream = QRCode.from("Hello World").stream(); // override the image type to be JPG QRCode.from("Hello World").to(ImageType.JPG).file(); QRCode.from("Hello World").to(ImageType.JPG).stream(); // override image size to be 250x250 QRCode.from("Hello World").withSize(250, 250).file(); QRCode.from("Hello World").withSize(250, 250).stream(); // override size and image type QRCode.from("Hello World").to(ImageType.GIF).withSize(250, 250).file(); QRCode.from("Hello World").to(ImageType.GIF).withSize(250, 250).stream(); // supply own outputstream QRCode.from("Hello World").to(ImageType.PNG).writeTo(outputStream); // supply own file name QRCode.from("Hello World").file("QRCode"); // supply charset hint to ZXING QRCode.from("Hello World").withCharset("UTF-8"); // supply error correction level hint to ZXING QRCode.from("Hello World").withErrorCorrection(ErrorCorrectionLevel.L); // supply any hint to ZXING QRCode.from("Hello World").withHint(EncodeHintType.CHARACTER_SET, "UTF-8"); // encode contact data as vcard using defaults VCard johnDoe = new VCard("John Doe") .setEmail("[email protected]") .setAddress("John Doe Street 1, 5678 Doestown") .setTitle("Mister") .setCompany("John Doe Inc.") .setPhoneNumber("1234") .setWebsite("www.example.org"); QRCode.from(johnDoe).file(); // if using special characters don't forget to supply the encoding VCard johnSpecial = new VCard("Jöhn Dɵe") .setAddress("ëåäöƞ Sträät 1, 1234 Döestüwn"); QRCode.from(johnSpecial).withCharset("UTF-8").file();

References

Источник

Java QR Code Generator — zxing example

Java QR Code Generator - zxing example

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Today we will look into the Java QR code generator program. If you are tech and gadget savvy, then you must be aware of the QR code. You will find it everywhere these days — in blogs, websites, and even in some public places. This is very popular in mobile apps, where you scan the QR code using a QR Code scanner app and it will show you the text or redirect you to the web page if it’s URL. I came across this recently and found it very interesting. If you want to know about QR Code, you can find a lot of useful information at Wikipedia QR Code Page.

Java QR code generator

When I found QR code images on so many websites, I started looking for java QR code generator. I looked into some open source APIs and found zxing to be the simple and best to use. If you want to generate a QR code image, then we only need its core library. Just add below dependency to your maven project.

If you want to read QR image through the command line, then we need to use it’s JavaSE library. You can add below dependency for it.

 com.google.zxing javase 3.3.2  

zxing maven dependencies

This will also let you know two extra dependencies required to run from the command line, as shown in the below image. We will have to add these jars into classpath to run the client app to read the QR code image. We will see this in action later on in this tutorial.

zxing example to generate QR code image

package com.journaldev.qrcode.generator; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Hashtable; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class GenerateQRCode < public static void main(String[] args) throws WriterException, IOException < String qrCodeText = "https://www.journaldev.com"; String filePath = "JD.png"; int size = 125; String fileType = "png"; File qrFile = new File(filePath); createQRImage(qrFile, qrCodeText, size, fileType); System.out.println("DONE"); >private static void createQRImage(File qrFile, String qrCodeText, int size, String fileType) throws WriterException, IOException < // Create the ByteMatrix for the QR-Code that encodes the given String HashtablehintMap = new Hashtable<>(); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, size, size, hintMap); // Make the BufferedImage that are to hold the QRCode int matrixWidth = byteMatrix.getWidth(); BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB); image.createGraphics(); Graphics2D graphics = (Graphics2D) image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, matrixWidth, matrixWidth); // Paint and save the image using the ByteMatrix graphics.setColor(Color.BLACK); for (int i = 0; i < matrixWidth; i++) < for (int j = 0; j < matrixWidth; j++) < if (byteMatrix.get(i, j)) < graphics.fillRect(i, j, 1, 1); >> > ImageIO.write(image, fileType, qrFile); > > 

java qr code generator, zxing example

Here is the QR Code image file created by this program. You can use your mobile QR Code scanner app to test it. It should point to JournalDev Home URL.

zxing example to read QR code

If you don’t have a mobile app to test it, don’t worry. You can read QR code with zxing API through the command line. Below is the command to read the QR code image file. Notice the additional jars in the classpath that zxing depends on.

$java -cp $HOME/.m2/repository/com/google/zxing/javase/3.3.2/javase-3.3.2.jar. $HOME/.m2/repository/com/google/zxing/core/3.3.2/core-3.3.2.jar:$HOME/.m2/repository/com/beust/jcommander/1.72/jcommander-1.72.jar:$HOME/.m2/repository/com/github/jai-imageio/jai-imageio-core/1.3.1/jai-imageio-core-1.3.1.jar com.google.zxing.client.j2se.CommandLineRunner JD.png 

zxing read qr code image

The below image shows the output produced by this command. You can download the QR Code Generator and Reader maven project from our GitHub Repository.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. Learn more about us

Источник

Оцените статью