我设法用热敏打印机打印阿拉伯字符串。但是,字符是相反的(从左到右而不是从右到左书写)。 我通过反转字符串的字符来解决该问题,效果很好。 现在,我遇到了一个新问题,因为阿拉伯语单词末尾的某些字符的形状与所附照片中的错误。
我该如何解决?
第一行中的单词应为“السلام”,如图所示。
第二行中的正确单词应为“النوع”,如图所示。
这是我要打印的代码
void sendData() throws IOException {
try {
byte[] ALLINEA_CT = {0x1B, 0x61, 0x01}; //text to center
String title = new StringBuilder("السلام").reverse().toString()+ '\n';
mmOutputStream.write(ALLINEA_CT);
mmOutputStream.write(title.getBytes("ISO-8859-6"));
String kind =new StringBuilder("النوع").reverse().toString();
String number = new StringBuilder("العدد").reverse().toString();
String cost = new StringBuilder("التكلفة").reverse().toString();
String BILL = "";
BILL = BILL+ "-----------------------------\n";
BILL = BILL + String.format("%1$4s %2$4s %3$17s",cost,number,kind);
mmOutputStream.write(BILL.getBytes("ISO-8859-6"));
} catch (Exception e) {
e.printStackTrace();
}
}
这是建立连接的方式
OutputStream mmOutputStream;
InputStream mmInputStream;
BluetoothAdapter mBluetoothAdapter;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
void findBT() {
try {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null) {
}
if(!mBluetoothAdapter.isEnabled()) {
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
if (device.getName().equals(printername)) {
mmDevice = device;
break;
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
// tries to open a connection to the bluetooth printer device
void openBT() throws IOException {
try {
// Standard SerialPortService ID
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
最好的方法是用文本创建一个位图并打印(大多数热敏打印机都打印位图)。 示例:
Bitmap bitmap = Bitmap.createBitmap(256+128, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
String text = "السلام";
canvas.drawText(text, 20, 20, new Paint());
printImage(bitmap);
我正在使用此代码来打印图像https://github.com/MFori/Android-Bluetooth-Printer