其他分享
首页 > 其他分享> > xprint库傻瓜式打印

xprint库傻瓜式打印

作者:互联网

xprint库是一款傻瓜式连接打印:

1:支持文字
2:支持字体大小和粗细设置
3:支持二维码,图片格式
4:支持表格,表格下划线
5:支持自定义byte数据
6:支持hex和十进制

具体使用

添加arr文件
在这里插入图片描述

app moudle build加入:

adnroid{

   repositories {
        flatDir {
            dirs 'libs'
        }
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }

}
dependencies{
 compile(name: 'xprint-release', ext: 'aar')
}

全局设置Print连接

/**
 * Created by 刘红鹏 on 2021/10/21.
 * <p>https://github.com/AnglePengCoding</p>
 * <p>https://blog.csdn.net/LIU_HONGPENG</p>
 */

public class App extends BaseApp {

    private static PrintImpl sPrinterImpl;

    @Override
    public void onCreate() {
        super.onCreate();
        sPrinterImpl = new PrintImpl();
    }

    public static PrintImpl getPrinterImpl() {
        return sPrinterImpl;
    }

}

在需要的class,添加蓝牙监听和初始化Printer

    App.getPrinterImpl().connectPrinter(new IConnectCallback() {
            @Override
            public void onPrinterConnectSuccess() {
                
            }

            @Override
            public void onPrinterConnectFailed(int i) {

            }
        });
   App.getPrinterImpl().initPrinter();

打印文字


public class CanvasUtils {

    /**
     * 画布模版   图片跟文字并行
     *
     * @param mPrinter  -
     */
    public static void printCustomImage(PrinterInstance mPrinter, String code, String unit,
                                        String checkDate, String checkOpinion,
                                        String rectificationOpinion, String ename, String unicode, String qrCode) {
        mPrinter.setPrinter(PrinterConstants.Command.ALIGN, PrinterConstants.Command.ALIGN_CENTER);
        mPrinter.setFont(0, 0, 0, 1, 0);
        mPrinter.printText("\n");
        mPrinter.printText("测试打印" + "\n");
        mPrinter.setFont(0, 0, 0, 0, 0);
        mPrinter.setPrinter(PrinterConstants.Command.ALIGN, PrinterConstants.Command.ALIGN_LEFT);
        mPrinter.printText(code + "\n");
        mPrinter.printText("————————————————" + "\n");
        mPrinter.printText(unit + "\n");
        mPrinter.printText(checkDate + "\n");
        mPrinter.printText(checkOpinion + "\n");
        mPrinter.printText(rectificationOpinion + "\n");
        mPrinter.printText("————————————————" + "\n");
        mPrinter.printText(ename + "\n");
        mPrinter.printText("测试打印文字2:" + "\n");
        mPrinter.printText(unicode + "\n");
        mPrinter.setPrinter(PrinterConstants.Command.ALIGN, PrinterConstants.Command.ALIGN_CENTER);

        Barcode barcode = new Barcode(PrinterConstants.BarcodeType.QRCODE, 2, 3, 6,
                qrCode);
        mPrinter.printBarCode(barcode);
        mPrinter.printText("扫码查看检" + "\n\n\n\n");
        mPrinter.printText("\n");
    }

具体api参考

public class PrintImpl implements IPrint {
    private IConnectCallback mCallback;
    private PrinterInstance mPrinter;
    @SuppressLint({"HandlerLeak"})
    private final Handler printerHandler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 101) {
                PrintImpl.this.mCallback.onPrinterConnectSuccess();
            } else {
                PrintImpl.this.mCallback.onPrinterConnectFailed(msg.what);
            }

        }
    };

    public PrintImpl() {
    }

    public PrinterInstance connectPrinter(@NonNull IConnectCallback callback) {
        this.mCallback = callback;

        try {
            DeviceControlSpd deviceControl = new DeviceControlSpd(PowerType.NEW_MAIN, new int[]{8});
            deviceControl.PowerOnDevice();
            deviceControl.newSetDir(46, 0);
        } catch (IOException var3) {
            var3.printStackTrace();
        }

        this.mPrinter = PrinterInstance.getPrinterInstance(new File("/dev/ttyMT0"), 115200, 0, this.printerHandler);
        this.mPrinter.openConnection();
        return this.mPrinter;
    }

    public PrinterInstance connectPrinter(File device, int baudrate, int flags, @NonNull IConnectCallback callback) {
        this.mCallback = callback;
        this.mPrinter = PrinterInstance.getPrinterInstance(device, baudrate, flags, this.printerHandler);
        this.mPrinter.openConnection();
        return this.mPrinter;
    }

    public void closeConnect() {
        if(this.mPrinter != null) {
            this.mPrinter.closeConnection();
            this.mPrinter = null;

            try {
                DeviceControlSpd deviceControl = new DeviceControlSpd(PowerType.NEW_MAIN, new int[]{8});
                deviceControl.PowerOffDevice();
            } catch (IOException var2) {
                var2.printStackTrace();
            }

        }
    }

    public int sendBytesData(byte[] srcData) {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            return this.mPrinter.sendBytesData(srcData);
        }
    }

    public int read(byte[] buffer) {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            return this.mPrinter.read(buffer);
        }
    }

    public void initPrinter() {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            this.mPrinter.initPrinter();
        }
    }

    public void setFont(int mCharacterType, int mWidth, int mHeight, int mBold, int mUnderline) {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            this.mPrinter.setFont(mCharacterType, mWidth, mHeight, mBold, mUnderline);
        }
    }

    public void setPrinter(int command, int value) {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            List commandList = new ArrayList();
            commandList.add(Integer.valueOf(0));
            commandList.add(Integer.valueOf(1));
            commandList.add(Integer.valueOf(13));
            this.mPrinter.setPrinter(((Integer)commandList.get(command)).intValue(), value);
        }
    }

    public int setPaperType(int paperType) {
        List<byte[]> paperList = new ArrayList();
        paperList.add(new byte[]{31, 17, 31, 68, 0, 31, 31});
        paperList.add(new byte[]{31, 17, 31, 68, 1, 31, -64, -128, 31, 70, 23, 31, 31});
        paperList.add(new byte[]{31, 17, 31, 68, 2, 31, 31});
        return this.sendBytesData((byte[])paperList.get(paperType));
    }

    public int setDensity(int density) {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            return this.mPrinter.sendBytesData(new byte[]{31, 17, 31, 22, (byte)density, 31, 31});
        }
    }

    public void setSpeed(int speed) {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            this.mPrinter.sendBytesData(new byte[]{31, 17, 31, 21, (byte)speed, 31, 31});
        }
    }

    public void setPaperFeed(int line) {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            line *= 8;
            byte[] bytes = new byte[]{27, 74, (byte)line};
            this.mPrinter.sendBytesData(bytes);
        }
    }

    public void setPaperBack(int line) {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            line *= 8;
            byte[] bytes = new byte[]{27, 75, (byte)line};
            this.mPrinter.sendBytesData(bytes);
        }
    }

    public void printSelfCheck() {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            byte[] bytes = new byte[]{29, 40, 65, 2, 0, 0, 2};
            this.mPrinter.sendBytesData(bytes);
        }
    }

    public void printText(String text) {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            this.mPrinter.printText(text);
        }
    }

    public int printBarCode(Barcode barcode) {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            return this.mPrinter.printBarCode(barcode);
        }
    }

    public void printImage(Bitmap bitmap, int alignType, int left, boolean isCompressed) {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            List<PAlign> alignList = new ArrayList();
            alignList.add(PAlign.START);
            alignList.add(PAlign.CENTER);
            alignList.add(PAlign.END);
            alignList.add(PAlign.NONE);
            this.mPrinter.printImage(bitmap, (PAlign)alignList.get(alignType), left, isCompressed);
        }
    }

    public void printBigImage(Bitmap bitmap, int alignType, int left, boolean isCompressed) {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            List<PAlign> alignList = new ArrayList();
            alignList.add(PAlign.START);
            alignList.add(PAlign.CENTER);
            alignList.add(PAlign.END);
            alignList.add(PAlign.NONE);
            PictureUtils.printBitmapImage(this.mPrinter, bitmap, (PAlign)alignList.get(alignType), left, isCompressed);
        }
    }

    public void printTable(Table table) {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            this.mPrinter.printTable(table);
        }
    }

    public int update(InputStream inputStream, String hexFileLength) {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            return UpdatePrinter.update(inputStream, hexFileLength, this.mPrinter);
        }
    }

    public void setSensitivity(int sensitivity) {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            this.mPrinter.sendBytesData(new byte[]{31, 17, 31, 70, (byte)sensitivity, 31, 31});
        }
    }

    public void setOutPaperLen(int len) {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            len *= 8;
            this.mPrinter.sendBytesData(new byte[]{31, 17, 31, -64, (byte)len, 31, 31});
        }
    }

    public void searchGap() {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            this.mPrinter.sendBytesData(new byte[]{12});
        }
    }

    public void setAllParams(byte[]... params) {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            int len = params.length * 3 + 4;
            byte[] send = new byte[len];

            for(int i = 0; i < params.length; ++i) {
                System.arraycopy(params[i], 0, send, i * 3 + 2, 3);
            }

            send[0] = 31;
            send[1] = 17;
            send[len - 2] = 31;
            send[len - 1] = 31;
            this.mPrinter.sendBytesData(send);
        }
    }

    public int getPrinterStatus() {
        if(this.mPrinter == null) {
            throw new RuntimeException("先调用connectPrinter方法初始化打印机操作类");
        } else {
            return this.mPrinter.getPrinterStatus();
        }
    }

    public PrinterInstance getPrinter() {
        return this.mPrinter;
    }
}

标签:打印,int,31,mPrinter,byte,xprint,new,傻瓜式,public
来源: https://blog.csdn.net/LIU_HONGPENG/article/details/121120910