【问题标题】:print image via bluetooth printer prints string通过蓝牙打印机打印图像打印字符串
【发布时间】:2014-09-29 12:52:32
【问题描述】:

我一直在通过蓝牙打印机打印图像。当我测试它时 文本打印效果很好。但是当涉及到图像时,只打印字符串字符。 我已将布局转换为位图。并将其保存到SD卡中。我需要将位图转换为 支持打印机的东西。我正在为我的应用程序使用“ZEBRA EZ320”打印机。 我已经使用以下代码将布局转换为位图,

View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();

【问题讨论】:

    标签: android printing bitmap bluetooth


    【解决方案1】:

    我找到了解决问题的方法。

    Bitmap localBitmap=BitmapFactory.decodeResource(getResources(), image);
    BluetoothConnection  myConn = new BluetoothConnection(macaddr);
    ZebraPrinter myPrinter = new ZebraPrinterCpcl(myConn);
    myConn.open();
    new ZebraPrinterLegacyDelegator(myPrinter).getGraphicsUtil().printImage(localBitmap, 0, 0, -1, -1,       false);
    // to reduce extra space 
    myConn.write("! UTILITIES\r\nIN-MILLIMETERS\r\nSETFF 10 2\r\nPRINT\r\n".getBytes());
    myConn.close();
    

    【讨论】:

    • 但是我有一些对齐问题。我上面提到的代码将图像打印在工作表的左侧。我需要将它与中心对齐。建议我任何想法。
    【解决方案2】:

    上述答案中提到的代码使用了 ZebraPrinterLegacyDelegator,已被弃用。

    使用以下代码,

    InputStream inputStream = assetManager.open("printing/ic_launcher.png");
    
            ZebraImageI zebraImageI = ZebraImageFactory.getImage(BitmapFactory.decodeStream(inputStream));
            zebraPrinter.printImage(zebraImageI, 250, 0, 0, -1, false);
    

    Zebra 打印机实例可以如下创建,

    zebraPrinter = ZebraPrinterFactory.getInstance(printerConnection);
    

    printImage 参数如下,

    image - the image to be printed.
    x - horizontal starting position in dots.
    y - vertical starting position in dots.
    width - desired width of the printed image. Passing a value less than 1 will preserve original width.
    height - desired height of the printed image. Passing a value less than 1 will preserve original height.
    insideFormat - boolean value indicating whether this image should be printed by itself (false), or is part of a format being written to the connection (true).
    

    为了解决您的对齐问题,更改 x 值以将图像移动到您方便的位置。

    【讨论】:

    • 感谢您的回复@Gokul。
    猜你喜欢
    • 2011-12-28
    • 2016-06-23
    • 2014-08-25
    • 2018-11-14
    • 1970-01-01
    • 2014-07-22
    • 2018-04-28
    • 1970-01-01
    • 2011-07-05
    相关资源
    最近更新 更多