徽标图像在收据打印机中未居中对齐

时间:2019-01-20 07:39:29

标签: android thermal-printer escpos

打印机不考虑图像的对齐方式。默认情况下,徽标始终保持对齐。文本对齐没有问题。因此aligncenter()方法没有什么问题。当我在while循环中设置对齐方式时,在纸上会打印一系列问号。

public void printImage(Bitmap bitmap, boolean center) {

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int newWidth = this.mType58 ? 384 : 576;

       if (width > (this.mType58 ? 384 : 576)) {
            bitmap = LibUtil.setImgSize(bitmap, (float) newWidth, (float) (height / (width / newWidth)), (float) width, (float) height);
       }

    try {
          PrinterWriter mThirdPrinter = this.mType58 ? new PrinterWriter58mm(384, 384) : new PrinterWriter80mm(576, 576);            
          ArrayList<byte[]> datas = mThirdPrinter.getImageByte(bitmap);

           if (mThirdPrinter.getDataAndClose() == null) {

               if (this.mCallback != null) {
                     this.mCallback.onError(ErrorCode.IMAGE_NOT_FONUD);
           }

          return;
      }

        try {
               datas.add(mThirdPrinter.getDataAndClose());
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

        if (datas == null) {
            DebugLog.LogD("imgpath is empty, datas is null, maybe is TFCard");
            return;
        }

        this.mPrinterModule.sendData(new byte[]{(byte) 27, (byte) 74, (byte) 0});

        Iterator it = datas.iterator();
        byte [] alignment = alignCenter();          
        int i = 0 ;
        byte [] data1 = null ;

        while (it.hasNext()) {              
            data1[i] = (byte) it.next();
        }

        this.mPrinterModule.sendData(alignment);
        this.mPrinterModule.sendData(data1);

    } catch (IOException e2) {
        e2.printStackTrace();
    }

}

1 个答案:

答案 0 :(得分:0)

当我在另一个办公室时,这种项目我遇到类似的问题,库中的所有对齐功能均不起作用。但是我有棘手的解决方案,以使徽标图像获得中心。 我绘制具有白色或透明分辨率的位图,并将宽度位置恰好位于收据的中间

public Bitmap createBitmap(Rect rectImage, int i, int j) {

        Paint p = new Paint();
        p.setStyle(Style.FILL_AND_STROKE);
        p.setAntiAlias(true);
        p.setFilterBitmap(true);
        p.setDither(true);
        p.setColor(Color.WHITE);

        Bitmap bitmap = Bitmap.createBitmap(rectImage.width() * 2,
                rectImage.height() * 2, Bitmap.Config.ARGB_8888);

        Canvas c = new Canvas(bitmap);
//      c.drawColor(Color.RED);
        c.drawRect(rectImage.left, rectImage.top, rectImage.right,
                rectImage.bottom, p);
        return bitmap;

    }

然后将此位图与您的徽标合并,请使用与此类似的代码(而非此代码)

public static Bitmap mergeToPin(Bitmap left, Bitmap right) {
    Bitmap result = Bitmap.createBitmap(left.getWidth(), left.getHeight(), left.getConfig());
    Canvas canvas = new Canvas(result);
    int widthleft = left.getWidth();
    int widthright = right.getWidth();
    canvas.drawBitmap(left, 0f, 0f, null);
    canvas.drawBitmap(right, widthleft, 0f, null);
    return result;
}

您知道,收据的大小可以预测。这样就可以为透明图像的宽度尺寸设置静态值