Skip to content

Commit cd04c32

Browse files
committed
[ADD] Генерация QR-кода
1 parent 906d30e commit cd04c32

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

pom.xml

+8
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@
8181
</execution>
8282
</executions>
8383
</plugin>
84+
<plugin>
85+
<groupId>org.apache.maven.plugins</groupId>
86+
<artifactId>maven-compiler-plugin</artifactId>
87+
<configuration>
88+
<source>7</source>
89+
<target>7</target>
90+
</configuration>
91+
</plugin>
8492
</plugins>
8593
</build>
8694
</project>

src/main/java/isc/barcode.java

+76-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
package isc;
22

3-
import java.io.FileInputStream;
4-
import java.io.IOException;
3+
import java.io.*;
4+
import java.nio.file.FileSystems;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
57
import java.util.EnumMap;
68
import java.util.EnumSet;
79
import java.util.Map;
810
import javax.imageio.ImageIO;
11+
912
import com.google.zxing.*;
1013
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
14+
import com.google.zxing.client.j2se.MatrixToImageWriter;
1115
import com.google.zxing.common.HybridBinarizer;
1216
import com.google.zxing.multi.GenericMultipleBarcodeReader;
1317

18+
import java.awt.Color;
19+
import java.awt.Graphics2D;
20+
import java.awt.image.BufferedImage;
21+
import java.io.IOException;
22+
import java.util.Hashtable;
1423

24+
import com.google.zxing.BarcodeFormat;
25+
import com.google.zxing.EncodeHintType;
26+
import com.google.zxing.WriterException;
27+
import com.google.zxing.common.BitMatrix;
28+
import com.google.zxing.qrcode.QRCodeWriter;
29+
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
1530
import static com.google.zxing.common.CharacterSetECI.UTF8;
1631

1732
public class barcode {
@@ -26,20 +41,77 @@ public barcode() {
2641
}
2742

2843
// Function to read the file with Barcode
29-
public String[] readBarCode(String path) throws IOException {
44+
public String[] readBarCode(String path) throws IOException
45+
{
3046
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(new FileInputStream(path)))));
3147
String[] resultsText = new String[0];
3248
try {
3349
Result[] results = reader.decodeMultiple(binaryBitmap, hintsMap);
3450
resultsText = new String[results.length];
3551
for (int i = 0; i < results.length; i++) {
52+
System.out.println(results[i].getText());
3653
resultsText[i] = results[i].getText();
3754
}
38-
} catch (NotFoundException ex) {
55+
} catch (NotFoundException ex)
56+
{
3957
}
4058

4159
return resultsText;
4260
}
4361

62+
// public static void main(String[] args) throws WriterException, IOException {
63+
// String qrCodeText = "Hi";
64+
// String filePath = "Test.png";
65+
// int size = 25;
66+
// String fileType = "png";
67+
//
68+
// boolean sc = createQRImage(filePath, qrCodeText, size, fileType);
69+
// barcode barcode = new barcode();
70+
//
71+
// generateQRCode("Hi",25,25,"new.png");
72+
// System.out.println(barcode.readBarCode("new.png").length);
73+
// System.out.println("Ура: "+ sc);
74+
// }
75+
76+
public boolean createQRImage(String filePath,String qrCodeText, int size, String fileType) throws WriterException, IOException {
77+
// Create the ByteMatrix for the QR-Code that encodes the given String
78+
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<>();
79+
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
80+
QRCodeWriter qrCodeWriter = new QRCodeWriter();
81+
BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, size, size, hintMap);
82+
// Make the BufferedImage that are to hold the QRCode
83+
int matrixWidth = byteMatrix.getWidth();
84+
BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB);
85+
image.createGraphics();
86+
87+
Graphics2D graphics = (Graphics2D) image.getGraphics();
88+
graphics.setColor(Color.WHITE);
89+
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
90+
// Paint and save the image using the ByteMatrix
91+
graphics.setColor(Color.BLACK);
92+
93+
for (int i = 0; i < matrixWidth; i++) {
94+
for (int j = 0; j < matrixWidth; j++) {
95+
if (byteMatrix.get(i, j)) {
96+
graphics.fillRect(i, j, 1, 1);
97+
}
98+
}
99+
}
100+
101+
File qrFile = new File(filePath);
102+
return ImageIO.write(image, fileType, qrFile);
103+
}
104+
105+
public byte[] generateQRCode(String filePath, String text, int height, String fileType) throws WriterException, IOException
106+
{
107+
QRCodeWriter writer = new QRCodeWriter();
108+
BitMatrix matrix = writer.encode(text, BarcodeFormat.QR_CODE, height, height);
109+
110+
Path path = FileSystems.getDefault().getPath(filePath);
111+
MatrixToImageWriter.writeToPath(matrix, fileType, path);
112+
113+
File qrFile = new File(filePath);
114+
return Files.readAllBytes(qrFile.toPath());
115+
}
44116
}
45117

0 commit comments

Comments
 (0)