What are different libraries to generate QR code and example in java
There are several libraries available to generate QR codes in Java.
Here are a few popular ones:
1. ZXing: ZXing is a popular open-source library for generating QR codes in Java.
It is also known as the "Zebra Crossing" library.
Here is an example of how to use ZXing to generate a QR code:
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
public class QRCodeGenerator {
public static void main(String[] args) {
String data = "Hello, World!";
String fileName = "qrcode.png";
int size = 250;
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix;
try {
bitMatrix = qrCodeWriter.encode(data, BarcodeFormat.QR_CODE, size, size);
Path path = FileSystems.getDefault().getPath(fileName);
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
} catch (WriterException | IOException e) {
e.printStackTrace();
}
}
}
2. QArt Codes: QArt Codes is a library that allows you to generate artistic QR codes with custom designs.
Here is an example of how to use QArt Codes to generate a QR code:
import com.github.qartc.qartc.QArt;
import com.github.qartc.qartc.QArtConfig;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class QRCodeGenerator {
public static void main(String[] args) {
String data = "Hello, World!";
String fileName = "qrcode.png";
QArtConfig config = QArt.newBuilder()
.data(data)
.charset("UTF-8")
.margin(4)
.build();
BufferedImage image = QArt.create(config);
File output = new File(fileName);
try {
ImageIO.write(image, "png", output);
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. QRGen: QRGen is another library for generating QR codes in Java. Here is an example of how to use QRGen to generate a QR code:
import net.glxn.qrgen.core.image.ImageType;
import net.glxn.qrgen.javase.QRCode;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
public class QRCodeGenerator {
public static void main(String[] args) {
String data = "Hello, World!";
String fileName = "qrcode.png";
int size = 250;
QRCode qrCode = QRCode.from(data).withSize(size, size).to(ImageType.PNG);
File file = new File(fileName);
try {
Files.write(file.toPath(), qrCode.stream().toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Comments
Post a Comment