Skip to content

Commit 4352780

Browse files
first compatibbility
1 parent 7b56312 commit 4352780

10 files changed

+134
-6
lines changed

android/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ android {
2222
}
2323

2424
dependencies {
25+
api 'com.google.zxing:core:3.3.0'
2526
api fileTree(dir: 'libs', include: ['*.jar'])
2627
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
2728
exclude group: 'com.android.support', module: 'support-annotations'
2829
})
2930
api 'androidx.appcompat:appcompat:1.0.0'
3031
api "com.facebook.react:react-native:+"
32+
3133
testImplementation 'junit:junit:4.12'
3234
}

android/src/main/java/com/pinmi/react/printer/RNBLEPrinterModule.java

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public void printImageData(String imageUrl, Callback errorCallback) {
7373

7474
}
7575

76+
@Override
77+
public void printQrCode(String qrCode, Callback errorCallback) {
78+
79+
}
80+
7681

7782
@ReactMethod
7883
public void connectPrinter(String innerAddress, Callback successCallback, Callback errorCallback) {

android/src/main/java/com/pinmi/react/printer/RNNetPrinterModule.java

+7
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ public void printImageData(String imageUrl, Callback errorCallback) {
6969
adapter.printImageData(imageUrl, errorCallback);
7070
}
7171

72+
@ReactMethod
73+
@Override
74+
public void printQrCode(String qrCode, Callback errorCallback) {
75+
Log.v("qrCode", qrCode);
76+
adapter.printQrCode(qrCode, errorCallback);
77+
}
78+
7279

7380

7481
@Override

android/src/main/java/com/pinmi/react/printer/RNPrinterModule.java

+3
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@ public interface RNPrinterModule {
3737
@ReactMethod
3838
public void printImageData(String imageUrl, Callback errorCallback) ;
3939

40+
@ReactMethod
41+
public void printQrCode(String qrCode, Callback errorCallback) ;
42+
4043
}
4144

android/src/main/java/com/pinmi/react/printer/RNUSBPrinterModule.java

+6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ public void printImageData(String imageUrl, Callback errorCallback) {
6868
adapter.printImageData(imageUrl, errorCallback);
6969
}
7070

71+
@ReactMethod
72+
@Override
73+
public void printQrCode(String qrCode, Callback errorCallback) {
74+
adapter.printQrCode(qrCode, errorCallback);
75+
}
76+
7177

7278
@ReactMethod
7379
public void connectPrinter(Integer vendorId, Integer productId, Callback successCallback, Callback errorCallback) {

android/src/main/java/com/pinmi/react/printer/adapter/BLEPrinterAdapter.java

+5
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,9 @@ public void run() {
189189
public void printImageData(String imageUrl, Callback errorCallback) {
190190

191191
}
192+
193+
@Override
194+
public void printQrCode(String qrCode, Callback errorCallback) {
195+
196+
}
192197
}

android/src/main/java/com/pinmi/react/printer/adapter/NetPrinterAdapter.java

+93
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
import android.util.Base64;
99
import android.util.Log;
1010

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+
1116
import com.facebook.react.bridge.Arguments;
1217
import com.facebook.react.bridge.Callback;
1318
import com.facebook.react.bridge.ReactApplicationContext;
@@ -308,6 +313,94 @@ public void printImageData(final String imageUrl, Callback errorCallback) {
308313
}
309314
}
310315

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+
311404
public static int[][] getPixelsSlow(Bitmap image2) {
312405

313406
Bitmap image = resizeTheImageForPrinting(image2);

android/src/main/java/com/pinmi/react/printer/adapter/PrinterAdapter.java

+2
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ public interface PrinterAdapter {
2727
public void printRawData(String rawBase64Data, Callback errorCallback);
2828

2929
public void printImageData(String imageUrl, Callback errorCallback);
30+
31+
public void printQrCode(String qrCode, Callback errorCallback);
3032
}

android/src/main/java/com/pinmi/react/printer/adapter/USBPrinterAdapter.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public void printImageData(final String imageUrl, Callback errorCallback) {
286286
// Set nL and nH based on the width of the image
287287
byte[] row = new byte[]{(byte)(0x00ff & pixels[y].length)
288288
, (byte)((0xff00 & pixels[y].length) >> 8)};
289-
289+
290290
mUsbDeviceConnection.bulkTransfer(mEndPoint, row, row.length, 100000);
291291

292292
for (int x = 0; x < pixels[y].length; x++) {
@@ -309,6 +309,11 @@ public void printImageData(final String imageUrl, Callback errorCallback) {
309309

310310
}
311311

312+
@Override
313+
public void printQrCode(String qrCode, Callback errorCallback) {
314+
315+
}
316+
312317
public static int[][] getPixelsSlow(Bitmap image2) {
313318

314319
Bitmap image = resizeTheImageForPrinting(image2);

dist/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ export var NetPrinter = {
207207
if (Platform.OS === "ios") {
208208
RNNetPrinter.printQrCode(qrCode, opts, function (error) { return console.warn(error); });
209209
}
210-
// else {
211-
// RNNetPrinter.printQrCode(qrCode, opts, function (error) {
212-
// return console.warn(error);
213-
// });
214-
// }
210+
else {
211+
RNNetPrinter.printQrCode(qrCode, opts, function (error) {
212+
return console.warn(error);
213+
});
214+
}
215215
},
216216

217217

0 commit comments

Comments
 (0)