@amitkhare/capacitor-cat-printer 0.5.0 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,188 @@
1
+ package khare.catprinter.plugin;
2
+
3
+ import android.graphics.Bitmap;
4
+ import android.graphics.Canvas;
5
+ import android.graphics.Color;
6
+ import android.graphics.Paint;
7
+ import android.graphics.Typeface;
8
+
9
+ import com.google.zxing.BarcodeFormat;
10
+ import com.google.zxing.EncodeHintType;
11
+ import com.google.zxing.MultiFormatWriter;
12
+ import com.google.zxing.WriterException;
13
+ import com.google.zxing.common.BitMatrix;
14
+ import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
15
+
16
+ import java.util.HashMap;
17
+ import java.util.Map;
18
+
19
+ /**
20
+ * Barcode and QR code generation utilities
21
+ */
22
+ public class BarcodeGenerator {
23
+
24
+ /**
25
+ * Generate a barcode bitmap
26
+ */
27
+ public static Bitmap generateBarcode(String data, String type, int width, int height,
28
+ boolean showText) throws WriterException {
29
+ BarcodeFormat format = getBarcodeFormat(type);
30
+
31
+ Map<EncodeHintType, Object> hints = new HashMap<>();
32
+ hints.put(EncodeHintType.MARGIN, 0);
33
+
34
+ MultiFormatWriter writer = new MultiFormatWriter();
35
+ BitMatrix bitMatrix = writer.encode(data, format, width, height, hints);
36
+
37
+ int barcodeHeight = showText ? height - 30 : height;
38
+ Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
39
+ Canvas canvas = new Canvas(bitmap);
40
+ canvas.drawColor(Color.WHITE);
41
+
42
+ Paint paint = new Paint();
43
+ paint.setColor(Color.BLACK);
44
+
45
+ // Draw barcode
46
+ for (int x = 0; x < bitMatrix.getWidth(); x++) {
47
+ for (int y = 0; y < Math.min(bitMatrix.getHeight(), barcodeHeight); y++) {
48
+ if (bitMatrix.get(x, y)) {
49
+ canvas.drawPoint(x, y, paint);
50
+ }
51
+ }
52
+ }
53
+
54
+ // Draw text below barcode
55
+ if (showText) {
56
+ paint.setTextSize(20);
57
+ paint.setTypeface(Typeface.MONOSPACE);
58
+ paint.setTextAlign(Paint.Align.CENTER);
59
+ canvas.drawText(data, width / 2f, height - 8, paint);
60
+ }
61
+
62
+ return bitmap;
63
+ }
64
+
65
+ /**
66
+ * Generate a QR code bitmap
67
+ */
68
+ public static Bitmap generateQRCode(String data, int size, String errorCorrectionLevel)
69
+ throws WriterException {
70
+ Map<EncodeHintType, Object> hints = new HashMap<>();
71
+ hints.put(EncodeHintType.MARGIN, 1);
72
+ hints.put(EncodeHintType.ERROR_CORRECTION, getErrorCorrectionLevel(errorCorrectionLevel));
73
+
74
+ MultiFormatWriter writer = new MultiFormatWriter();
75
+ BitMatrix bitMatrix = writer.encode(data, BarcodeFormat.QR_CODE, size, size, hints);
76
+
77
+ Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
78
+
79
+ for (int x = 0; x < size; x++) {
80
+ for (int y = 0; y < size; y++) {
81
+ bitmap.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
82
+ }
83
+ }
84
+
85
+ return bitmap;
86
+ }
87
+
88
+ /**
89
+ * Create a bitmap with barcode centered/aligned on paper width
90
+ */
91
+ public static Bitmap createAlignedBarcode(String data, String type, int paperWidth,
92
+ int barcodeHeight, boolean showText,
93
+ String align) throws WriterException {
94
+ // Calculate barcode width (use 80% of paper width for most barcodes)
95
+ int barcodeWidth = (int) (paperWidth * 0.8);
96
+
97
+ Bitmap barcode = generateBarcode(data, type, barcodeWidth, barcodeHeight, showText);
98
+
99
+ // Create full-width bitmap with alignment
100
+ int totalHeight = barcode.getHeight();
101
+ Bitmap result = Bitmap.createBitmap(paperWidth, totalHeight, Bitmap.Config.ARGB_8888);
102
+ Canvas canvas = new Canvas(result);
103
+ canvas.drawColor(Color.WHITE);
104
+
105
+ int x = 0;
106
+ switch (align) {
107
+ case "center":
108
+ x = (paperWidth - barcodeWidth) / 2;
109
+ break;
110
+ case "right":
111
+ x = paperWidth - barcodeWidth;
112
+ break;
113
+ default:
114
+ x = 0;
115
+ }
116
+
117
+ canvas.drawBitmap(barcode, x, 0, null);
118
+ barcode.recycle();
119
+
120
+ return result;
121
+ }
122
+
123
+ /**
124
+ * Create a bitmap with QR code centered/aligned on paper width
125
+ */
126
+ public static Bitmap createAlignedQRCode(String data, int paperWidth, int qrSize,
127
+ String errorCorrection, String align)
128
+ throws WriterException {
129
+ Bitmap qrCode = generateQRCode(data, qrSize, errorCorrection);
130
+
131
+ Bitmap result = Bitmap.createBitmap(paperWidth, qrSize, Bitmap.Config.ARGB_8888);
132
+ Canvas canvas = new Canvas(result);
133
+ canvas.drawColor(Color.WHITE);
134
+
135
+ int x = 0;
136
+ switch (align) {
137
+ case "center":
138
+ x = (paperWidth - qrSize) / 2;
139
+ break;
140
+ case "right":
141
+ x = paperWidth - qrSize;
142
+ break;
143
+ default:
144
+ x = 0;
145
+ }
146
+
147
+ canvas.drawBitmap(qrCode, x, 0, null);
148
+ qrCode.recycle();
149
+
150
+ return result;
151
+ }
152
+
153
+ private static BarcodeFormat getBarcodeFormat(String type) {
154
+ switch (type.toUpperCase()) {
155
+ case "CODE39":
156
+ return BarcodeFormat.CODE_39;
157
+ case "EAN13":
158
+ return BarcodeFormat.EAN_13;
159
+ case "EAN8":
160
+ return BarcodeFormat.EAN_8;
161
+ case "UPC_A":
162
+ return BarcodeFormat.UPC_A;
163
+ case "UPC_E":
164
+ return BarcodeFormat.UPC_E;
165
+ case "ITF":
166
+ return BarcodeFormat.ITF;
167
+ case "CODABAR":
168
+ return BarcodeFormat.CODABAR;
169
+ case "CODE128":
170
+ default:
171
+ return BarcodeFormat.CODE_128;
172
+ }
173
+ }
174
+
175
+ private static ErrorCorrectionLevel getErrorCorrectionLevel(String level) {
176
+ switch (level.toUpperCase()) {
177
+ case "L":
178
+ return ErrorCorrectionLevel.L;
179
+ case "Q":
180
+ return ErrorCorrectionLevel.Q;
181
+ case "H":
182
+ return ErrorCorrectionLevel.H;
183
+ case "M":
184
+ default:
185
+ return ErrorCorrectionLevel.M;
186
+ }
187
+ }
188
+ }