如何通过蓝牙打印机打印图像

时间:2015-02-05 11:55:12

标签: java android eclipse printing bluetooth

任何人都知道如何通过android中的蓝牙打印机打印图像?...我正在使用下面的代码打印字符串,我无法改变字体样式,所以我想要打印图像。

这是我如何打印字符串消息:

public void printNa(View v) {
        printFare = amount.getText().toString();
            try {
                PRINTER_MAC_ID = "00:12:F3:19:4D:D8";
                print_message = "This is the sample String";


                    mBTAdapter = BluetoothAdapter.getDefaultAdapter();
                    dialogProgress = new Dialog(Ticketing.this);
                    try {
                        if (mBTAdapter.isDiscovering()){
                            mBTAdapter.cancelDiscovery();
                        }else{
                            mBTAdapter.startDiscovery();
                        }
                } catch (Exception e) {
                    Toast.makeText(this, "Unable to print: " + e, Toast.LENGTH_LONG).show();
                    return;
                }
                    System.out.println("BT Searching status :" + mBTAdapter.isDiscovering());
                    if (mBTAdapter == null) {
                        Toast.makeText(this, "Device has no bluetooth capability",Toast.LENGTH_LONG).show();
                        return;
                    }else{
                        if (!mBTAdapter.isEnabled()) {
                            Intent i = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE);
                            startActivityForResult(i, 0);
                        }
                        // Register the BroadcastReceiver
                        IntentFilter filter = new IntentFilter(
                        BluetoothDevice.ACTION_FOUND);
                        registerReceiver(mReceiver, filter);
                    }

                } catch (Exception e) {
                    Toast.makeText(this, "Unable to print: " + e, Toast.LENGTH_LONG).show();
                    return;
                }
        }   
    /********/
    public void printBillToDevice(final String address) {
        mBTAdapter.cancelDiscovery();
        try {           
            BluetoothDevice mdevice = mBTAdapter.getRemoteDevice(address);
            Method m = mdevice.getClass().getMethod("createRfcommSocket",new Class[] { int.class });
            mBTSocket = (BluetoothSocket) m.invoke(mdevice, 1);
            mBTSocket.connect();

            //  image = "logo";
            //  print_image(Environment.getExternalStorageDirectory().toString() + "/LOGO.png");
        //  byte[] format = { 27, 33, 0 };
        //  byte[] arrayOfByte1 = { 27, 33, 0 };

            int copyPrintIs = Integer.parseInt(copyPrint);
                for(int x = 1; x <= copyPrintIs; x++){
                    OutputStream os = mBTSocket.getOutputStream();
                    os.flush();
                    //os.write(iu_ini_ang_ipiprint.getBytes());
                    // Bold
            //format[2] = ((byte)(0x8 | arrayOfByte1[2]));
                    // Height
            //format[2] = ((byte)(0x10 | arrayOfByte1[2]));
                    // Width
                //format[2] = ((byte) (0x20 | arrayOfByte1[2]));
                    //out.write(str.getBytes(),0,str.getBytes().length);
        //os.write(format);
                    os.write(print_message.getBytes(),0,print_message.getBytes().length);
                    System.out.println(iu_ini_ang_ipiprint);
                    SystemClock.sleep(4000);
                }
            copyPrint = "1";
            if (mBTAdapter != null)
                mBTAdapter.cancelDiscovery();
                mBTSocket.close();
                setResult(RESULT_OK);
        }catch(Exception e) {
                Log.e("Class ", "My Exe ", e);
                e.printStackTrace();
                setResult(RESULT_CANCELED);
                return;
        }
}
    /********/
    /********/
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            try {
                String action = intent.getAction();
                // When discovery finds a device
                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    // Get the BluetoothDevice object from the Intent
                    BluetoothDevice device = intent
                            .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    System.out.println("***" + device.getName() + " : "
                            + device.getAddress());

                    if (device.getAddress().equalsIgnoreCase(PRINTER_MAC_ID)) {
                        mBTAdapter.cancelDiscovery();
                        dialogProgress.dismiss();
                        Toast.makeText(Ticketing.this,device.getName() + " Printing reciept", Toast.LENGTH_LONG).show();
                            printBillToDevice(PRINTER_MAC_ID);
                    }
                }
            } catch (Exception e) {
                Toast.makeText(Ticketing.this,"Error printing reciept: " + e,Toast.LENGTH_LONG).show();
                return;
            }
        }
    };

我无法找到改变我的字体样式的好解决方案所以我决定打印图像文件,但我无法找到一个很好的解决方案再次通过热敏打印机打印图像,我正在使用APEX 3打印机。 我是新来的,所以任何帮助都会被贬低。

抱歉我的英语不好。

谢谢你!

0 个答案:

没有答案