Ali
Ali

Reputation: 171

Modify Arabic letters printed using thermal printer

I managed to print Arabic strings with a thermal printer. However, the characters are reversed (written from left to right not from right to left). I solved that by inverting the characters of the string and it did well. Now, I have a new problem because some characters at the end of the arabic words are in the wrong shape as in the attached photo.

enter image description here

How can I solve that?

The word in the first line should be "السلام" not as shown.

The right word in the second line should be "النوع" not as shown.

This is my code to print

 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();
        }
    }

This is how a connection is made

      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();
        }
    }

Upvotes: 2

Views: 3102

Answers (1)

MedSaid
MedSaid

Reputation: 26

The best way is to create a bitmap with your text and print it (most of thermal printers print bitmaps). Example:

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);

I am using this code to print images https://github.com/MFori/Android-Bluetooth-Printer

Upvotes: 1

Related Questions