8
8
import android .util .Base64 ;
9
9
import android .util .Log ;
10
10
11
+ import com .facebook .common .internal .ImmutableMap ;
11
12
import com .google .zxing .BarcodeFormat ;
13
+ import com .google .zxing .EncodeHintType ;
12
14
import com .google .zxing .MultiFormatWriter ;
13
15
import com .google .zxing .WriterException ;
14
16
import com .google .zxing .common .BitMatrix ;
19
21
import com .facebook .react .bridge .WritableArray ;
20
22
import com .facebook .react .bridge .WritableMap ;
21
23
import com .facebook .react .modules .core .DeviceEventManagerModule ;
24
+ import com .google .zxing .qrcode .QRCodeWriter ;
25
+ import com .google .zxing .qrcode .encoder .ByteMatrix ;
26
+ import com .pinmi .react .printer .R ;
22
27
23
28
import java .io .ByteArrayOutputStream ;
24
29
import java .io .IOException ;
29
34
import java .net .Socket ;
30
35
import java .net .URL ;
31
36
import java .util .ArrayList ;
37
+ import java .util .Hashtable ;
32
38
import java .util .List ;
33
39
34
40
import android .graphics .BitmapFactory ;
@@ -263,12 +269,11 @@ public static Bitmap getBitmapFromURL(String src) {
263
269
}
264
270
}
265
271
266
-
267
272
@ Override
268
273
public void printImageData (final String imageUrl , Callback errorCallback ) {
269
274
final Bitmap bitmapImage = getBitmapFromURL (imageUrl );
270
275
271
- if (bitmapImage == null ) {
276
+ if (bitmapImage == null ) {
272
277
errorCallback .invoke ("image not found" );
273
278
return ;
274
279
}
@@ -293,8 +298,8 @@ public void printImageData(final String imageUrl, Callback errorCallback) {
293
298
// the printer will resume to normal text printing
294
299
printerOutputStream .write (SELECT_BIT_IMAGE_MODE );
295
300
// Set nL and nH based on the width of the image
296
- printerOutputStream .write (new byte []{( byte )( 0x00ff & pixels [ y ]. length )
297
- , (byte )((0xff00 & pixels [y ].length ) >> 8 )});
301
+ printerOutputStream .write (
302
+ new byte [] { ( byte ) ( 0x00ff & pixels [ y ]. length ) , (byte ) ((0xff00 & pixels [y ].length ) >> 8 ) });
298
303
for (int x = 0 ; x < pixels [y ].length ; x ++) {
299
304
// for each stripe, recollect 3 bytes (3 bytes = 24 bits)
300
305
printerOutputStream .write (recollectSlice (y , x , pixels ));
@@ -315,9 +320,9 @@ public void printImageData(final String imageUrl, Callback errorCallback) {
315
320
316
321
@ Override
317
322
public void printQrCode (String qrCode , Callback errorCallback ) {
318
- final Bitmap bitmapImage = TextToQrImageEncode (imageUrl );
323
+ final Bitmap bitmapImage = TextToQrImageEncode (qrCode );
319
324
320
- if (bitmapImage == null ) {
325
+ if (bitmapImage == null ) {
321
326
errorCallback .invoke ("image not found" );
322
327
return ;
323
328
}
@@ -342,8 +347,8 @@ public void printQrCode(String qrCode, Callback errorCallback) {
342
347
// the printer will resume to normal text printing
343
348
printerOutputStream .write (SELECT_BIT_IMAGE_MODE );
344
349
// Set nL and nH based on the width of the image
345
- printerOutputStream .write (new byte []{( byte )( 0x00ff & pixels [ y ]. length )
346
- , (byte )((0xff00 & pixels [y ].length ) >> 8 )});
350
+ printerOutputStream .write (
351
+ new byte [] { ( byte ) ( 0x00ff & pixels [ y ]. length ) , (byte ) ((0xff00 & pixels [y ].length ) >> 8 ) });
347
352
for (int x = 0 ; x < pixels [y ].length ; x ++) {
348
353
// for each stripe, recollect 3 bytes (3 bytes = 24 bits)
349
354
printerOutputStream .write (recollectSlice (y , x , pixels ));
@@ -362,45 +367,32 @@ public void printQrCode(String qrCode, Callback errorCallback) {
362
367
}
363
368
}
364
369
370
+ private Bitmap TextToQrImageEncode (String Value ) {
365
371
372
+ com .google .zxing .Writer writer = new QRCodeWriter ();
366
373
367
- private Bitmap TextToQrImageEncode (String Value ) throws WriterException {
368
- BitMatrix bitMatrix ;
374
+ BitMatrix bitMatrix = null ;
369
375
try {
370
- bitMatrix = new MultiFormatWriter ().encode (
371
- Value ,
372
- BarcodeFormat .DATA_MATRIX .QR_CODE ,
373
- QRcodeWidth , QRcodeWidth , null
374
- );
375
-
376
- } catch (IllegalArgumentException Illegalargumentexception ) {
377
-
378
- return null ;
379
- }
380
- int bitMatrixWidth = bitMatrix .getWidth ();
381
-
382
- int bitMatrixHeight = bitMatrix .getHeight ();
383
-
384
- int [] pixels = new int [bitMatrixWidth * bitMatrixHeight ];
385
-
386
- for (int y = 0 ; y < bitMatrixHeight ; y ++) {
387
- int offset = y * bitMatrixWidth ;
388
-
389
- for (int x = 0 ; x < bitMatrixWidth ; x ++) {
390
-
391
- pixels [offset + x ] = bitMatrix .get (x , y ) ?
392
- getResources ().getColor (R .color .black ):getResources ().getColor (R .color .white );
376
+ bitMatrix = writer .encode (Value , com .google .zxing .BarcodeFormat .QR_CODE , 250 , 250 ,
377
+ ImmutableMap .of (EncodeHintType .MARGIN , 1 ));
378
+ int width = 250 ;
379
+ int height = 250 ;
380
+ Bitmap bmp = Bitmap .createBitmap (width , height , Bitmap .Config .ARGB_8888 );
381
+
382
+ for (int i = 0 ; i < width ; i ++) {
383
+ for (int j = 0 ; j < height ; j ++) {
384
+ bmp .setPixel (i , j , bitMatrix .get (i , j ) ? Color .BLACK : Color .WHITE );
385
+ }
393
386
}
387
+ return bmp ;
388
+ } catch (WriterException e ) {
389
+ // Log.e("QR ERROR", ""+e);
390
+
394
391
}
395
- Bitmap bitmap = Bitmap .createBitmap (bitMatrixWidth , bitMatrixHeight , Bitmap .Config .ARGB_4444 );
396
392
397
- bitmap .setPixels (pixels , 0 , 500 , 0 , 0 , bitMatrixWidth , bitMatrixHeight );
398
- return bitmap ;
393
+ return null ;
399
394
}
400
395
401
-
402
-
403
-
404
396
public static int [][] getPixelsSlow (Bitmap image2 ) {
405
397
406
398
Bitmap image = resizeTheImageForPrinting (image2 );
0 commit comments