react-native-bluetooth-serial icon indicating copy to clipboard operation
react-native-bluetooth-serial copied to clipboard

How to send write image

Open Raveerote opened this issue 7 years ago • 13 comments

I'm trying to write image but they all didn't work. Here's the code

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)

Raveerote avatar Oct 13 '17 08:10 Raveerote

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 avatar Dec 22 '17 03:12 NativoLink

@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.

ghost avatar Dec 22 '17 18:12 ghost

how do you send a text into thermal printer using write method

ghost avatar Jan 13 '18 08:01 ghost

转到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?

qcxiaoshuai avatar Sep 19 '18 03:09 qcxiaoshuai

@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 :)

Kaitsuvaan avatar Nov 13 '18 08:11 Kaitsuvaan

This is just for Android's solution. How should IOS be solved? It's urgent. Thank you!

renrukun861113 avatar Jan 13 '19 03:01 renrukun861113

This is just for Android's solution. How should IOS be solved? It's urgent. Thank you!

+1

msnajib avatar Jan 25 '19 08:01 msnajib

what about iOS? Thank you!

msnajib avatar Jan 29 '19 15:01 msnajib

how to use this function ? please

lexdorf-17 avatar May 31 '19 17:05 lexdorf-17

@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?

zainmustafa avatar Aug 19 '19 09:08 zainmustafa

@renrukun861113 @msnajib hello guys did you find the solution how to print Image in iOS ?

FazilMuhammed avatar Oct 13 '20 08:10 FazilMuhammed

Expo react native Bluetooth serial

codevangelist avatar Aug 27 '21 17:08 codevangelist

Display cache from Android expo

codevangelist avatar Aug 27 '21 17:08 codevangelist