1
1
package isc ;
2
2
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 ;
5
7
import java .util .EnumMap ;
6
8
import java .util .EnumSet ;
7
9
import java .util .Map ;
8
10
import javax .imageio .ImageIO ;
11
+
9
12
import com .google .zxing .*;
10
13
import com .google .zxing .client .j2se .BufferedImageLuminanceSource ;
14
+ import com .google .zxing .client .j2se .MatrixToImageWriter ;
11
15
import com .google .zxing .common .HybridBinarizer ;
12
16
import com .google .zxing .multi .GenericMultipleBarcodeReader ;
13
17
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 ;
14
23
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 ;
15
30
import static com .google .zxing .common .CharacterSetECI .UTF8 ;
16
31
17
32
public class barcode {
@@ -26,20 +41,77 @@ public barcode() {
26
41
}
27
42
28
43
// Function to read the file with Barcode
29
- public String [] readBarCode (String path ) throws IOException {
44
+ public String [] readBarCode (String path ) throws IOException
45
+ {
30
46
BinaryBitmap binaryBitmap = new BinaryBitmap (new HybridBinarizer (new BufferedImageLuminanceSource (ImageIO .read (new FileInputStream (path )))));
31
47
String [] resultsText = new String [0 ];
32
48
try {
33
49
Result [] results = reader .decodeMultiple (binaryBitmap , hintsMap );
34
50
resultsText = new String [results .length ];
35
51
for (int i = 0 ; i < results .length ; i ++) {
52
+ System .out .println (results [i ].getText ());
36
53
resultsText [i ] = results [i ].getText ();
37
54
}
38
- } catch (NotFoundException ex ) {
55
+ } catch (NotFoundException ex )
56
+ {
39
57
}
40
58
41
59
return resultsText ;
42
60
}
43
61
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
+ }
44
116
}
45
117
0 commit comments