Skip to content

Commit 6cf3b8d

Browse files
committedNov 22, 2021
android qr code added
1 parent b1e3c43 commit 6cf3b8d

File tree

4 files changed

+34
-42
lines changed

4 files changed

+34
-42
lines changed
 

‎android/.project

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</natures>
2323
<filteredResources>
2424
<filter>
25-
<id>1632297731534</id>
25+
<id>0</id>
2626
<name></name>
2727
<type>30</type>
2828
<matcher>

‎android/.settings/org.eclipse.buildship.core.prefs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ arguments=
22
auto.sync=false
33
build.scans.enabled=false
44
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
5-
connection.project.dir=
5+
connection.project.dir=../example/android
66
eclipse.preferences.version=1
77
gradle.user.home=
88
java.home=/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home

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

+31-39
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import android.util.Base64;
99
import android.util.Log;
1010

11+
import com.facebook.common.internal.ImmutableMap;
1112
import com.google.zxing.BarcodeFormat;
13+
import com.google.zxing.EncodeHintType;
1214
import com.google.zxing.MultiFormatWriter;
1315
import com.google.zxing.WriterException;
1416
import com.google.zxing.common.BitMatrix;
@@ -19,6 +21,9 @@
1921
import com.facebook.react.bridge.WritableArray;
2022
import com.facebook.react.bridge.WritableMap;
2123
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;
2227

2328
import java.io.ByteArrayOutputStream;
2429
import java.io.IOException;
@@ -29,6 +34,7 @@
2934
import java.net.Socket;
3035
import java.net.URL;
3136
import java.util.ArrayList;
37+
import java.util.Hashtable;
3238
import java.util.List;
3339

3440
import android.graphics.BitmapFactory;
@@ -263,12 +269,11 @@ public static Bitmap getBitmapFromURL(String src) {
263269
}
264270
}
265271

266-
267272
@Override
268273
public void printImageData(final String imageUrl, Callback errorCallback) {
269274
final Bitmap bitmapImage = getBitmapFromURL(imageUrl);
270275

271-
if(bitmapImage == null) {
276+
if (bitmapImage == null) {
272277
errorCallback.invoke("image not found");
273278
return;
274279
}
@@ -293,8 +298,8 @@ public void printImageData(final String imageUrl, Callback errorCallback) {
293298
// the printer will resume to normal text printing
294299
printerOutputStream.write(SELECT_BIT_IMAGE_MODE);
295300
// 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) });
298303
for (int x = 0; x < pixels[y].length; x++) {
299304
// for each stripe, recollect 3 bytes (3 bytes = 24 bits)
300305
printerOutputStream.write(recollectSlice(y, x, pixels));
@@ -315,9 +320,9 @@ public void printImageData(final String imageUrl, Callback errorCallback) {
315320

316321
@Override
317322
public void printQrCode(String qrCode, Callback errorCallback) {
318-
final Bitmap bitmapImage = TextToQrImageEncode(imageUrl);
323+
final Bitmap bitmapImage = TextToQrImageEncode(qrCode);
319324

320-
if(bitmapImage == null) {
325+
if (bitmapImage == null) {
321326
errorCallback.invoke("image not found");
322327
return;
323328
}
@@ -342,8 +347,8 @@ public void printQrCode(String qrCode, Callback errorCallback) {
342347
// the printer will resume to normal text printing
343348
printerOutputStream.write(SELECT_BIT_IMAGE_MODE);
344349
// 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) });
347352
for (int x = 0; x < pixels[y].length; x++) {
348353
// for each stripe, recollect 3 bytes (3 bytes = 24 bits)
349354
printerOutputStream.write(recollectSlice(y, x, pixels));
@@ -362,45 +367,32 @@ public void printQrCode(String qrCode, Callback errorCallback) {
362367
}
363368
}
364369

370+
private Bitmap TextToQrImageEncode(String Value) {
365371

372+
com.google.zxing.Writer writer = new QRCodeWriter();
366373

367-
private Bitmap TextToQrImageEncode(String Value) throws WriterException {
368-
BitMatrix bitMatrix;
374+
BitMatrix bitMatrix = null;
369375
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+
}
393386
}
387+
return bmp;
388+
} catch (WriterException e) {
389+
// Log.e("QR ERROR", ""+e);
390+
394391
}
395-
Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);
396392

397-
bitmap.setPixels(pixels, 0, 500, 0, 0, bitMatrixWidth, bitMatrixHeight);
398-
return bitmap;
393+
return null;
399394
}
400395

401-
402-
403-
404396
public static int[][] getPixelsSlow(Bitmap image2) {
405397

406398
Bitmap image = resizeTheImageForPrinting(image2);

‎example/android/.project

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</natures>
1717
<filteredResources>
1818
<filter>
19-
<id>1632297731500</id>
19+
<id>0</id>
2020
<name></name>
2121
<type>30</type>
2222
<matcher>

0 commit comments

Comments
 (0)
Please sign in to comment.