6

我正在开发一个应用程序,我必须在其中打印一张收据,收据有一个徽标(静态)图像视图,我如何将它打印到蓝牙打印机?而且我已经使用 GestureOverlayView 进行了签名,现在我必须打印该手势以及徽标和一些有关收据的数据。在此处输入图像描述

我还需要打印一个阿拉伯字符串。这显示在文本视图中。为了显示签名,我在我的布局中使用图像视图。请检查图像,我附上我必须打印的图像,请给我一些关于打印它的想法。

我可以更改打印格式,这意味着我不必以矩形打印数据,但图像对齐是主要问题,我将如何了解对齐?

4

2 回答 2

9

试试用这个....

public class BluetoothPrinterActivity extends Activity {

BluetoothAdapter mBTAdapter;
BluetoothSocket mBTSocket = null;
Dialog dialogProgress;
String BILL, TRANS_ID;
String PRINTER_MAC_ID = "00:1F:B7:02:8F:44";
final String ERROR_MESSAGE = "There has been an error in printing the bill.";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {

        BILL = "\nSale Slip No: 12345678" + " " + "04-08-2011\n";
        BILL = BILL + "----------------------------------------";
        BILL = BILL + "\n\n";
        BILL = BILL + "Total Qty:" + " " + "2.0\n";
        BILL = BILL + "Total Value:" + " " + "17625.0\n";
        BILL = BILL + "-----------------------------------------";

        mBTAdapter = BluetoothAdapter.getDefaultAdapter();

        if (mBTAdapter == null) {
            Toast.makeText(this, "Device has no bluetooth capability",Toast.LENGTH_LONG).show();
            finish();
        } 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); // Don't forget to unregister during onDestroy

            dialogProgress = new Dialog(BluetoothPrinterActivity.this);
            dialogProgress.setTitle("Finding printer...");
            dialogProgress.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        public void onDismiss(DialogInterface dialog) {
                            dialog.dismiss();
                            setResult(RESULT_CANCELED);
                            finish();
                        }
                    });
            dialogProgress.show();

        }

        if (mBTAdapter.isDiscovering())
            mBTAdapter.cancelDiscovery();
        else
            mBTAdapter.startDiscovery();

        System.out.println("BT Searching status :" + mBTAdapter.isDiscovering());

    } catch (Exception e) {
        Log.e("Class ", "My Exe ", e);
    }
}

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(BluetoothPrinterActivity.this,device.getName() + " Printing data",Toast.LENGTH_LONG).show();
                    printBillToDevice(PRINTER_MAC_ID);
                    Toast.makeText(BluetoothPrinterActivity.this,device.getName() + " found", Toast.LENGTH_LONG).show();
                }
            }
        } catch (Exception e) {
            Log.e("Class ", "My Exe ", e);
        }
    }
};


@Override
protected void onDestroy() {
    super.onDestroy();
    try {
        if (dialogProgress != null)
            dialogProgress.dismiss();
        if (mBTAdapter != null)
            mBTAdapter.cancelDiscovery();
        this.unregisterReceiver(mReceiver);
    } catch (Exception e) {
        Log.e("Class ", "My Exe ", e);
    }
}


@Override
public void onBackPressed() {
    try {
        if (mBTAdapter != null)
            mBTAdapter.cancelDiscovery();
            this.unregisterReceiver(mReceiver);
    } catch (Exception e) {
        Log.e("Class ", "My Exe ", e);
    }
    setResult(RESULT_CANCELED);
    finish();
}


public void printBillToDevice(final String address) {
    new Thread(new Runnable() {
        public void run() {
            runOnUiThread(new Runnable() {
                public void run() {
                    dialogProgress.setTitle("Connecting...");
                    dialogProgress.show();
                }

            });

            mBTAdapter.cancelDiscovery();

            try {
                System.out.println("**************************#****connecting");
                BluetoothDevice mdevice = mBTAdapter.getRemoteDevice(address);
                Method m = mdevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
                mBTSocket = (BluetoothSocket) m.invoke(mdevice, 1);

                mBTSocket.connect();
                OutputStream os = mBTSocket.getOutputStream();
                os.flush();

                os.write(BILL.getBytes());
                System.out.println(BILL);

                setResult(RESULT_OK);
                finish();
            } catch (Exception e) {
                Log.e("Class ", "My Exe ", e);
                e.printStackTrace();
                setResult(RESULT_CANCELED);
                finish();

            }

            runOnUiThread(new Runnable() {
                public void run() {
                    try {
                        dialogProgress.dismiss();
                    } catch (Exception e) {
                        Log.e("Class ", "My Exe ", e);
                    }
                }

            });

        }

    }).start();
  }
}

从这个链接蓝牙打印机问题在android

于 2013-06-04T10:47:31.287 回答
3

在您可以从已经提出的问题中获得解决方案之前,我会尽力给出答案

您有 3 个从 android 应用程序打印的选项

1> SDKs/Libraries:(如starmicronics,仅限于少数设备)

2> Google Play Apps:(直接调用第三方应用的intent )

3>谷歌云打印:(推荐。它易于使用并集成到应用程序中)通过这个我们连接任何打印机,如经典打印机、云打印打印机。

使用谷歌打印作为用户视角用户应该激活谷歌打印服务到gmail帐户,谷歌云打印在许多地方使用!

设置谷歌打印服务

博客

https://stackoverflow.com/questions/11323805/how-to-setup-network-printer-to-google-cloud-print/14911180#14911180

谷歌云打印设置1

谷歌云打印设置2

通过 gchrome 打印

谷歌云打印机

将云打印机集成到应用程序:

在 Android 中,没有像其他平台一样的Airprint选项,但谷歌为此提供了很棒的云打印选项,这样任何打印机都可以使用移动设备上的打印选项。

示例代码:

函数代码

谷歌云打印代码

于 2013-03-21T08:14:56.870 回答