有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法在Zebra打印机上打印标签

我是Android新手,我正在尝试理解使用Android打印文件的正确架构。我读到了关于打印管理器、打印服务、打印作业等的信息。。。,但我没有找到一个好的简单的例子从哪里开始。我做了一些证明,但没有结果

我的目标是在Zebra打印机上打印标签或字符串。我正在使用ip地址将我的应用程序和ID连接到打印机。我发现了两个例子。 第一个例子:

            String ipAddress = "X.X.X.X";
            int port = 9100;
            PrintStream oStream;


            try {
                Socket client = new Socket(ipAddress, 9100);
                oStream = new PrintStream(client.getOutputStream(), true, "UTF-8");
                oStream.println("-------------------------------------------------\r\n");
                oStream.println(" NAME     : DEMO CLIENT\r\n");
                oStream.println(" CODE  : 00000234242\r\n");
                oStream.println(" ADDRESS   : Street 52\r\n");
                oStream.println(" Phone : 2310-892345\r\n");
                oStream.println("-------------------------------------------------\r\n");

                oStream.flush();

                oStream.close();
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            } 

第二个例子:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pdf_document_test);

        FONT11 = 10;
        FONT12 = 11;
        FONT14 = 13;
        FONT16 = 15;
        btnCreate = (Button)findViewById(R.id.create);
        editText =(EditText) findViewById(R.id.edittext);
        btnPrint = findViewById(R.id.btnPrint);
        typeface = Typeface.createFromAsset(getAssets(), "fonts/impact.ttf");
        gwen = Typeface.createFromAsset(getAssets(), "fonts/Gwendolyn-Regular.ttf");

        bmp = BitmapFactory.decodeResource(getResources(),R.drawable.orizzontale_nero);
        scaleBitmap = Bitmap.createScaledBitmap(bmp,50,20,false);


        // = Typeface.createFromAsset(getApplicationContext().getAssets(), "impact.ttf");
        //carlitoBold = Typeface.createFromAsset(getApplicationContext().getAssets(), "Carlito-Bold.ttf");

        btnCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //new LongOperation().execute(" ");
                new LabelBarcodePDFTask().execute();

            }
        });

        btnPrint.setOnClickListener(v ->{
            new PrintPDF().execute();
        } );

    }

    private class PrintPDF extends AsyncTask<Void,Void,Integer> {

        @Override
        protected Integer doInBackground(Void... voids) {
            FileInputStream fileInputStream = null;
            try {
                fileInputStream = new FileInputStream(PdfDocumentTestActivity.this.getFilesDir()+ File.separator + "/mypdf/test-ITEXTPDF.pdf");
                InputStream is = fileInputStream;
                clientSocket = new Socket(sIP, Integer.parseInt(sPort));
                outToServer = new DataOutputStream(clientSocket.getOutputStream());
                byte[] buffer = new byte[3000];

                while (is.read(buffer) != -1) {
                    outToServer.write(buffer);
                }
                outToServer.flush();
                return 1;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

    }

   

    private class LabelBarcodePDFTask extends AsyncTask<Void,Void,String>{

        @Override
        protected String doInBackground(Void... voids) {
            String directory_path = PdfDocumentTestActivity.this.getFilesDir()+File.separator + "/mypdf/";
            String targetPdf = directory_path+"test-ITEXTPDF1.pdf";
            //Create layout and set background & borders
            Rectangle layout = new Rectangle(PageSize.ARCH_A);
            //layout.setBackgroundColor(new BaseColor(100, 200, 180)); //Background color
            layout.setBorderColor(BaseColor.DARK_GRAY);  //Border color
            layout.setBorderWidth(6);      //Border width
            layout.setBorder(Rectangle.BOX);

            Document document = new Document(layout);
            PdfWriter writer = null;
            try {
                writer = PdfWriter.getInstance(document, new FileOutputStream(targetPdf));
                document.open();
                PdfContentByte cb = writer.getDirectContent();
                //Get width and height of whole page
                float pdfPageWidth = document.getPageSize().getWidth();
                float pdfPageHeight = document.getPageSize().getHeight();

                /*document.add(new Paragraph("pdfPageWidth = "+pdfPageWidth));
                document.add(new Paragraph("pdfPageHeight = "+pdfPageHeight));*/

                Barcode39 barcode39 = new Barcode39();
                barcode39.setCode("123456789");
                Image code39Image = barcode39.createImageWithBarcode(cb, null, null);
                document.add(code39Image);
                document.newPage();
                document.close();
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }




            return targetPdf;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Toast.makeText(getApplicationContext(), "File saved in "+s, Toast.LENGTH_SHORT).show();
        }
    }

这两种解决方案在Brother打印机和OKI打印机上都可以正常工作,但当我在Zebra打印机上运行时,什么都没有发生。 我问您是否可以提供一个示例结构作为起点。简单的事情。还有,如果有办法了解点击“打印文档”按钮后发生的情况


共 (0) 个答案