Skip to content

Commit 133892f

Browse files
Added QR Generator
1 parent 42a75b3 commit 133892f

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

qrgenerator.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import com.google.zxing.BarcodeFormat;
2+
import com.google.zxing.EncodeHintType;
3+
import com.google.zxing.qrcode.QRCodeWriter;
4+
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
5+
import com.google.zxing.common.BitMatrix;
6+
import com.google.zxing.client.j2se.MatrixToImageWriter;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.nio.file.FileSystems;
11+
import java.nio.file.Path;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
public class QRCodeGenerator {
16+
public static void main(String[] args) {
17+
String text = "Hello, QR Code!"; // Text to encode in QR code
18+
String filePath = "QRCode.png"; // Output file path
19+
int width = 300; // QR code image width
20+
int height = 300; // QR code image height
21+
22+
// Create a map for encoding hints
23+
Map<EncodeHintType, ErrorCorrectionLevel> hints = new HashMap<>();
24+
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
25+
26+
try {
27+
// Generate the QR code matrix
28+
QRCodeWriter qrCodeWriter = new QRCodeWriter();
29+
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hints);
30+
31+
// Save the QR code image
32+
Path path = FileSystems.getDefault().getPath(filePath);
33+
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
34+
35+
System.out.println("QR Code generated successfully: " + filePath);
36+
} catch (Exception e) {
37+
System.out.println("Error while generating QR Code: " + e.getMessage());
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)