|
8 | 8 | import android.util.Base64;
|
9 | 9 | import android.util.Log;
|
10 | 10 |
|
| 11 | +import com.google.zxing.BarcodeFormat; |
| 12 | +import com.google.zxing.MultiFormatWriter; |
| 13 | +import com.google.zxing.WriterException; |
| 14 | +import com.google.zxing.common.BitMatrix; |
| 15 | + |
11 | 16 | import com.facebook.react.bridge.Arguments;
|
12 | 17 | import com.facebook.react.bridge.Callback;
|
13 | 18 | import com.facebook.react.bridge.ReactApplicationContext;
|
@@ -308,6 +313,94 @@ public void printImageData(final String imageUrl, Callback errorCallback) {
|
308 | 313 | }
|
309 | 314 | }
|
310 | 315 |
|
| 316 | + @Override |
| 317 | + public void printQrCode(String qrCode, Callback errorCallback) { |
| 318 | + final Bitmap bitmapImage = TextToQrImageEncode(imageUrl); |
| 319 | + |
| 320 | + if(bitmapImage == null) { |
| 321 | + errorCallback.invoke("image not found"); |
| 322 | + return; |
| 323 | + } |
| 324 | + |
| 325 | + if (this.mSocket == null) { |
| 326 | + errorCallback.invoke("bluetooth connection is not built, may be you forgot to connectPrinter"); |
| 327 | + return; |
| 328 | + } |
| 329 | + |
| 330 | + final Socket socket = this.mSocket; |
| 331 | + |
| 332 | + try { |
| 333 | + int[][] pixels = getPixelsSlow(bitmapImage); |
| 334 | + |
| 335 | + OutputStream printerOutputStream = socket.getOutputStream(); |
| 336 | + |
| 337 | + printerOutputStream.write(SET_LINE_SPACE_24); |
| 338 | + printerOutputStream.write(CENTER_ALIGN); |
| 339 | + |
| 340 | + for (int y = 0; y < pixels.length; y += 24) { |
| 341 | + // Like I said before, when done sending data, |
| 342 | + // the printer will resume to normal text printing |
| 343 | + printerOutputStream.write(SELECT_BIT_IMAGE_MODE); |
| 344 | + // 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)}); |
| 347 | + for (int x = 0; x < pixels[y].length; x++) { |
| 348 | + // for each stripe, recollect 3 bytes (3 bytes = 24 bits) |
| 349 | + printerOutputStream.write(recollectSlice(y, x, pixels)); |
| 350 | + } |
| 351 | + |
| 352 | + // Do a line feed, if not the printing will resume on the same line |
| 353 | + printerOutputStream.write(LINE_FEED); |
| 354 | + } |
| 355 | + printerOutputStream.write(SET_LINE_SPACE_32); |
| 356 | + printerOutputStream.write(LINE_FEED); |
| 357 | + |
| 358 | + printerOutputStream.flush(); |
| 359 | + } catch (IOException e) { |
| 360 | + Log.e(LOG_TAG, "failed to print data"); |
| 361 | + e.printStackTrace(); |
| 362 | + } |
| 363 | + } |
| 364 | + |
| 365 | + |
| 366 | + |
| 367 | + private Bitmap TextToQrImageEncode(String Value) throws WriterException { |
| 368 | + BitMatrix bitMatrix; |
| 369 | + 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); |
| 393 | + } |
| 394 | + } |
| 395 | + Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444); |
| 396 | + |
| 397 | + bitmap.setPixels(pixels, 0, 500, 0, 0, bitMatrixWidth, bitMatrixHeight); |
| 398 | + return bitmap; |
| 399 | + } |
| 400 | + |
| 401 | + |
| 402 | + |
| 403 | + |
311 | 404 | public static int[][] getPixelsSlow(Bitmap image2) {
|
312 | 405 |
|
313 | 406 | Bitmap image = resizeTheImageForPrinting(image2);
|
|
0 commit comments