Skip to content

Commit

Permalink
[ADD] Генерация QR-кода
Browse files Browse the repository at this point in the history
  • Loading branch information
gevorg95 committed Sep 22, 2021
1 parent 906d30e commit cd04c32
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
80 changes: 76 additions & 4 deletions src/main/java/isc/barcode.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
package isc;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.*;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Map;
import javax.imageio.ImageIO;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.multi.GenericMultipleBarcodeReader;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Hashtable;

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;
import static com.google.zxing.common.CharacterSetECI.UTF8;

public class barcode {
Expand All @@ -26,20 +41,77 @@ public barcode() {
}

// Function to read the file with Barcode
public String[] readBarCode(String path) throws IOException {
public String[] readBarCode(String path) throws IOException
{
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(new FileInputStream(path)))));
String[] resultsText = new String[0];
try {
Result[] results = reader.decodeMultiple(binaryBitmap, hintsMap);
resultsText = new String[results.length];
for (int i = 0; i < results.length; i++) {
System.out.println(results[i].getText());
resultsText[i] = results[i].getText();
}
} catch (NotFoundException ex) {
} catch (NotFoundException ex)
{
}

return resultsText;
}

// public static void main(String[] args) throws WriterException, IOException {
// String qrCodeText = "Hi";
// String filePath = "Test.png";
// int size = 25;
// String fileType = "png";
//
// boolean sc = createQRImage(filePath, qrCodeText, size, fileType);
// barcode barcode = new barcode();
//
// generateQRCode("Hi",25,25,"new.png");
// System.out.println(barcode.readBarCode("new.png").length);
// System.out.println("Ура: "+ sc);
// }

public boolean createQRImage(String filePath,String qrCodeText, int size, String fileType) throws WriterException, IOException {
// Create the ByteMatrix for the QR-Code that encodes the given String
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = 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);
}
}
}

File qrFile = new File(filePath);
return ImageIO.write(image, fileType, qrFile);
}

public byte[] generateQRCode(String filePath, String text, int height, String fileType) throws WriterException, IOException
{
QRCodeWriter writer = new QRCodeWriter();
BitMatrix matrix = writer.encode(text, BarcodeFormat.QR_CODE, height, height);

Path path = FileSystems.getDefault().getPath(filePath);
MatrixToImageWriter.writeToPath(matrix, fileType, path);

File qrFile = new File(filePath);
return Files.readAllBytes(qrFile.toPath());
}
}

0 comments on commit cd04c32

Please sign in to comment.