Open Raveerote opened 7 years ago
Go to http://www.zjiang.com/en/init.php/service/driver and Download -> SDK for Android OS in this you will find -> btsdk.jar copy this and create the libs folder in your /node_modules/react-native-bluetooth-serial/android/libs and paste, add this line compile files('libs/btsdk.jar') in /node_modules/react-native-bluetooth-serial/android/builde.gradle. Add this lines in RCTBluetoothSerialModule
private Bitmap base64ToBitmap(String b64) { byte[] imageAsBytes = Base64.decode(b64.getBytes(), Base64.DEFAULT); return BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length); }
private String bitmapToBase64(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
@ReactMethod
/**
* Write to device over serial port
*/
public void write_img(String imgB64, Callback callback) {
String encodeIMG = imgB64;
String base64Image = encodeIMG.split(",")[1];
Bitmap bmp = base64ToBitmap(base64Image);
String encodedImage = bitmapToBase64(bmp);
//---------------------------------------------------
byte[] data = POS_PrintBMP(bmp, 384, 0); // --- PRINT IMG FROM BITMAP
//---------------------------------------------------
mBluetoothService.write(data);
callback.invoke(true);
}
public static byte[] POS_PrintBMP(Bitmap mBitmap, int nWidth, int nMode) {
int width = (nWidth + 7) / 8 * 8;
int height = mBitmap.getHeight() * width / mBitmap.getWidth();
height = (height + 7) / 8 * 8;
Bitmap rszBitmap = mBitmap;
if(mBitmap.getWidth() != width) {
rszBitmap = Other.resizeImage(mBitmap, width, height);
}
Bitmap grayBitmap = Other.toGrayscale(rszBitmap);
byte[] dithered = Other.thresholdToBWPic(grayBitmap);
byte[] data = Other.eachLinePixToCmd(dithered, width, nMode);
return data;
}
@NativoLink can you more details how add this ?
private Bitmap base64ToBitmap(String b64) { byte[] imageAsBytes = Base64.decode(b64.getBytes(), Base64.DEFAULT); return BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length); }
or can you give me full code RCTBluetoothSerialModule , please i need this.
how do you send a text into thermal printer using write method
转到http://www.zjiang.com/en/init.php/service/driver和下载 - > SDK for Android OS 你会发现 - > btsdk.jar复制这个并在/ node_modules中创建libs文件夹/ react-native-bluetooth-serial / android / libs并粘贴, 在/node_modules/react-native-bluetooth-serial/android/builde.gradle中添加此行编译文件('libs / btsdk.jar')。 在RCTBluetoothSerialModule中添加此行
private Bitmap base64ToBitmap(String b64){ byte [] imageAsBytes = Base64.decode(b64.getBytes(),Base64.DEFAULT); 返回BitmapFactory.decodeByteArray(imageAsBytes,0,imageAsBytes.length); }
private String bitmapToBase64(Bitmap bitmap) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); byte[] byteArray = byteArrayOutputStream .toByteArray(); return Base64.encodeToString(byteArray, Base64.DEFAULT); } @ReactMethod /** * Write to device over serial port */ public void write_img(String imgB64, Callback callback) { String encodeIMG = imgB64; String base64Image = encodeIMG.split(",")[1]; Bitmap bmp = base64ToBitmap(base64Image); String encodedImage = bitmapToBase64(bmp); //--------------------------------------------------- byte[] data = POS_PrintBMP(bmp, 384, 0); // --- PRINT IMG FROM BITMAP //--------------------------------------------------- mBluetoothService.write(data); callback.invoke(true); } public static byte[] POS_PrintBMP(Bitmap mBitmap, int nWidth, int nMode) { int width = (nWidth + 7) / 8 * 8; int height = mBitmap.getHeight() * width / mBitmap.getWidth(); height = (height + 7) / 8 * 8; Bitmap rszBitmap = mBitmap; if(mBitmap.getWidth() != width) { rszBitmap = Other.resizeImage(mBitmap, width, height); } Bitmap grayBitmap = Other.toGrayscale(rszBitmap); byte[] dithered = Other.thresholdToBWPic(grayBitmap); byte[] data = Other.eachLinePixToCmd(dithered, width, nMode); return data; }
Bitmap grayBitmap = Other.toGrayscale(rszBitmap); Other? where is the Other?
@NativoLink thx for code 👍
Must add: @qcxiaoshuai import android.graphics.Bitmap; import android.graphics.BitmapFactory;
import java.io.ByteArrayOutputStream; import zj.com.customize.sdk.Other;
with RCTBluetoothSerialModule.java too and its work fine :)
This is just for Android's solution. How should IOS be solved? It's urgent. Thank you!
This is just for Android's solution. How should IOS be solved? It's urgent. Thank you!
+1
what about iOS? Thank you!
how to use this function ? please
@NativoLink thanks for this, able to print images in some thermal printers but some aren't printing even with this, any idea? And how can i solve this?
@renrukun861113 @msnajib hello guys did you find the solution how to print Image in iOS ?
Expo react native Bluetooth serial
Display cache from Android expo
I'm trying to write image but they all didn't work. Here's the code
1. var byteArray = Base64.decodeString(base64String); for (var i = 0 ; i < byteArray.length; i++){ this.write(new Buffer(byteArray[i].toString(2))) }
this.write(byteArray.buffer)
var byteArray = Base64.decodeArrayBuffer(base64String); console.log(byteArray) this.write(byteArray)