如何让星级TSP100LAN收据打印机与Android配合使用?

esb*_*esb 4 printing android javapos receipt stario-sdk

在这里找到了一个API

但是,当我运行AndroidSample测试应用程序时,我会收到错误.

当我按下"获取打印机状态"按钮时,我得到"打印机在线".看起来这个按钮很有效.

然而:

  • 按"从打印机读取数据"会产生"失败.无法读取固件名称".

  • 按"打印收据"会导致应用程序挂起3秒钟.然后没事.

  • 按"打印已检查的块收据"会产生"打印成功"或大挂起(有时强制关闭).无论如何,什么都没有打印出来.

bbe*_*all 6

我花了很长时间才弄明白如何让它发挥作用.我会尽我所能让你开始.我对android很新,所以请随意指出我做错的事情.通过向上移动内容偶尔会错误地打印,从而切掉顶部并在底部添加很多空间.如果有人能搞清楚,我会非常感激.

干得好:

需要APK中的这些文件:不要相信我修改了它们:1.RasterDocument.java 2. StarBitmap.java

主要印刷方式:

    public static void PrintReceipt(Context context, RelativeLayout layout){

        String portName = "tcp:10.1.250.20"; //ip address of your printer
        String portSettings = "";

//have to measure the layout for it to print correctly, otherwise sizes are zero
        layout.measure(View.MeasureSpec.makeMeasureSpec(layout.getLayoutParams().width, View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(layout.getLayoutParams().height, View.MeasureSpec.EXACTLY));
        layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());

        Bitmap bitmap = Bitmap.createBitmap(layout.getWidth(),layout.getHeight(), Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
        layout.draw(canvas);


        int maxWidth = 576; //default width of tsp100 receipt
        RasterDocument rasterDoc = new RasterDocument(RasterDocument.RasSpeed.Full, RasterDocument.RasPageEndMode.FeedAndFullCut, RasterDocument.RasPageEndMode.FeedAndFullCut, RasterDocument.RasTopMargin.Standard, 0, 0, 0);
        StarBitmap starbitmap = new StarBitmap(bitmap, false, maxWidth);

        StarIOPort port = null;
        try
        {
            /*
                   using StarIOPort3.1.jar (support USB Port)
                   Android OS Version: upper 2.2
               */
            port = StarIOPort.getPort(portName, portSettings, 10000, context);
            /*
                   using StarIOPort.jar
                   Android OS Version: under 2.1
                   port = StarIOPort.getPort(portName, portSettings, 10000);
               */

            try
            {
                Thread.sleep(500);
            }
            catch(InterruptedException e) {}

            byte[] command = rasterDoc.BeginDocumentCommandData();
            port.writePort(command, 0, command.length);
            command = starbitmap.getImageRasterDataForPrinting();
            port.writePort(command, 0, command.length);
            command = rasterDoc.EndDocumentCommandData();
            port.writePort(command, 0, command.length);

            try
            {
                Thread.sleep(1000);
            }
            catch(InterruptedException e) {}
        }
        catch (StarIOPortException e)
        {
            ShowAlertMessage(context, "Failure", "Failed to connect to printer. " + e.getMessage());
        }
        finally
        {
            if(port != null)
            {
                try {
                    StarIOPort.releasePort(port);
                } catch (StarIOPortException e) {}
            }
        }

    }

    private static void ShowAlertMessage(final Context context, final String alertTitle, final String message){
        try {
            ((Activity)context).runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    AlertDialog.Builder dialog = new AlertDialog.Builder(context);
                    dialog.setNegativeButton("Ok", null);
                    AlertDialog alert = dialog.create();
                    alert.setTitle(alertTitle);
                    alert.setMessage(message);
                    alert.show();
                }});
        } catch (final Exception e) {
            Log.e(PrinterFunctions.class.getName(), e.getMessage());
        }
    }
Run Code Online (Sandbox Code Playgroud)

那么:在另一种方法中,向PrintReceipt发送一个relativelayout.在类的构造函数中设置当前上下文.

  Context currentContext;
    RelativeLayout relativeLayout;
    private final int receiptWidth = 576;
    private Typeface typeFace = Typeface.DEFAULT;
    private int normalFontSize = 23;
    private int largeFontSize = 28;

public SetupReceiptClass(Context context){
    currentContext = context;
}
public void SetupReceipt(String customerName){
       //Create layout for receipt
        relativeLayout = new RelativeLayout(currentContext);
        RelativeLayout.LayoutParams params;
        params = new RelativeLayout.LayoutParams(receiptWidth,     ViewGroup.LayoutParams.MATCH_PARENT);
        relativeLayout.setLayoutParams(params);
        relativeLayout.setId(R.id.ReceiptLayout);

//Create whatever views you want here and add them to the RelativeLayout to make up your receipt
relativeLayout.addView(whateverViewsYouCreate);

//Finally, Print the receipt.
        new Thread(new Runnable() {

            @Override
            public void run() {
                PrintReceipt(currentContext, relativeLayout);
            }
        }).start();

}
Run Code Online (Sandbox Code Playgroud)

再说一次,我是android的新手,这可能不是最好的方式,但它是打印.你发现任何酷我都喜欢听他们.