android 连接网络打印机

android 连接网络打印机
最近做了一个点菜的项目,里面涉及到一个服务员点完菜,厨房马上就能将做菜单打印出来的功能。这就需要我们连接打印机了,由于刚开始买了一个佳博的热敏网口打印机做测试,所以我跑到佳博的官网去找相关的sdk和开发文档,好不容易搞完了。到客户那边一看,发现他们用的打印机跟我做测试的打印机不是同一型号,那我的厨打功能肯定就不行了,所以就在网上找了些连接网络打印机的通用方法,也就是通过Socket来连接打印机。连接是能连接上了,但是怎么将我这边的字符串装成打印机那边能够识别的字节流又让我头疼了,费了很大的劲儿终于在一篇博客中找到我想要的资料。下面跟大家看一下代码
   // 定义编码方式
    private static String encoding = null;
    private Socket sock = null;
    // 通过socket流进行读写
    private OutputStream socketOut = null;
    private OutputStreamWriter writer = null;

    /**
     * 初始化Pos实例
     *
     * @param ip
     *            打印机IP
     * @param port
     *            打印机端口号
     * @param encoding
     *            编码
     * @throws IOException
     */
    public Pos(String ip, int port, String encoding) throws IOException {
        sock = new Socket(ip, port);
        socketOut = new DataOutputStream(sock.getOutputStream());
        this.encoding = encoding;
        writer = new OutputStreamWriter(socketOut, encoding);
    }

    /**
     * 关闭IO流和Socket
     *
     * @throws IOException
     */
    protected void closeIOAndSocket() throws IOException {
        writer.close();
        socketOut.close();
        sock.close();
    }

    /**
     * 打印二维码
     *
     * @param qrData
     *            二维码的内容
     * @throws IOException
     */
    protected void qrCode(String qrData) throws IOException {
        int moduleSize = 8;
        int length = qrData.getBytes(encoding).length;

        // 打印二维码矩阵
        writer.write(0x1D);// init
        writer.write("(k");// adjust height of barcode
        writer.write(length + 3); // pl
        writer.write(0); // ph
        writer.write(49); // cn
        writer.write(80); // fn
        writer.write(48); //
        writer.write(qrData);

        writer.write(0x1D);
        writer.write("(k");
        writer.write(3);
        writer.write(0);
        writer.write(49);
        writer.write(69);
        writer.write(48);

        writer.write(0x1D);
        writer.write("(k");
        writer.write(3);
        writer.write(0);
        writer.write(49);
        writer.write(67);
        writer.write(moduleSize);

        writer.write(0x1D);
        writer.write("(k");
        writer.write(3); // pl
        writer.write(0); // ph
        writer.write(49); // cn
        writer.write(81); // fn
        writer.write(48); // m

        writer.flush();

    }

    /**
     * 进纸并全部切割
     *
     * @return
     * @throws IOException
     */
    protected void feedAndCut() throws IOException {
        writer.write(0x1D);
        writer.write(86);
        writer.write(65);
        // writer.write(0);
        // 切纸前走纸多少
        writer.write(10);
        writer.flush();

        // 另外一种切纸的方式
        // byte[] bytes = {29, 86, 0};
        // socketOut.write(bytes);
    }

    /**
     * 打印换行
     *
     * @return length 需要打印的空行数
     * @throws IOException
     */
    protected void printLine(int lineNum) throws IOException {
        for (int i = 0; i < lineNum; i++) {
            writer.write("\n");
        }
        writer.flush();
    }

    /**
     * 打印换行(只换一行)
     *
     * @throws IOException
     */
    protected void printLine() throws IOException {
        writer.write("\n");
        writer.flush();
    }

    /**
     * 打印空白(一个Tab的位置,约4个汉字)
     *
     * @param length
     *            需要打印空白的长度,
     * @throws IOException
     */
    protected void printTabSpace(int length) throws IOException {
        for (int i = 0; i < length; i++) {
            writer.write("\t");
        }
        writer.flush();
    }

    /**
     * 打印空白(一个汉字的位置)
     *
     * @param length
     *            需要打印空白的长度,
     * @throws IOException
     */
    protected void printWordSpace(int length) throws IOException {
        for (int i = 0; i < length; i++) {
            writer.write("  ");
        }
        writer.flush();
    }
    protected void printSpace(int length)throws IOException{
        for (int i = 0; i < length; i++) {
            writer.write(" ");
        }
        writer.flush();
    }

    /**
     * 打印位置调整
     *
     * @param position
     *            打印位置 0:居左(默认) 1:居中 2:居右
     * @throws IOException
     */
    protected void printLocation(int position) throws IOException {
        writer.write(0x1B);
        writer.write(97);
        writer.write(position);
        writer.flush();
    }

    /**
     * 绝对打印位置
     *
     * @throws IOException
     */
    protected void printLocation(int light, int weight) throws IOException {
        writer.write(0x1B);
        writer.write(0x24);
        writer.write(light);
        writer.write(weight);
        writer.flush();
    }

    /**
     * 打印文字
     *
     * @param text
     * @throws IOException
     */
    protected void printText(String text) throws IOException {
        String s = text;
        byte[] content = s.getBytes("gbk");
        socketOut.write(content);
        socketOut.flush();
    }

    /**
     * 新起一行,打印文字
     *
     * @param text
     * @throws IOException
     */
    protected void printTextNewLine(String text) throws IOException {
        // 换行
        writer.write("\n");
        writer.flush();
        String s = text;
        byte[] content = s.getBytes("gbk");
        socketOut.write(content);
        socketOut.flush();
    }

    /**
     * 初始化打印机
     *
     * @throws IOException
     */
    protected void initPos() throws IOException {
        writer.write(0x1B);
        writer.write(0x40);
        writer.flush();
    }

    /**
     * 加粗
     *
     * @param flag
     *            false为不加粗
     * @return
     * @throws IOException
     */
    protected void bold(boolean flag) throws IOException {
        if (flag) {
            // 常规粗细
            writer.write(0x1B);
            writer.write(69);
            writer.write(0xF);
            writer.flush();
        } else {
            // 加粗
            writer.write(0x1B);
            writer.write(69);
            writer.write(0);
            writer.flush();
        }
    }

    /**
     * 设置字体大小
     * */
    
    protected void setTextSize(int size)throws IOException{
        writer.write(CMD_FontSize(size));
        writer.flush();
    }
    // / <summary>
        // / 字体的大小
        // / </summary>
        // / <param name="nfontsize">0:正常大小 1:两倍高 2:两倍宽 3:两倍大小 4:三倍高 5:三倍宽 6:三倍大小
        // 7:四倍高 8:四倍宽 9:四倍大小 10:五倍高 11:五倍宽 12:五倍大小</param>
        // / <returns></returns>
        public String CMD_FontSize(int nfontsize) {
            String _cmdstr = "";
            // 设置字体大小
            switch (nfontsize) {
            case -1:
                _cmdstr = new StringBuffer().append((char) 29).append((char) 33)
                        .append((char) 0).toString();// 29 33
                break;

            case 0:
                _cmdstr = new StringBuffer().append((char) 29).append((char) 33)
                        .append((char) 0).toString();// 29 33
                break;

            case 1:
                _cmdstr = new StringBuffer().append((char) 29).append((char) 33)
                        .append((char) 1).toString();
                break;

            case 2:
                _cmdstr = new StringBuffer().append((char) 29).append((char) 33)
                        .append((char) 16).toString();
                break;

            case 3:
                _cmdstr = new StringBuffer().append((char) 29).append((char) 33)
                        .append((char) 17).toString();
                break;

            case 4:
                _cmdstr = new StringBuffer().append((char) 29).append((char) 33)
                        .append((char) 2).toString();
                break;

            case 5:
                _cmdstr = new StringBuffer().append((char) 29).append((char) 33)
                        .append((char) 32).toString();
                break;

            case 6:
                _cmdstr = new StringBuffer().append((char) 29).append((char) 33)
                        .append((char) 34).toString();
                break;

            case 7:
                _cmdstr = new StringBuffer().append((char) 29).append((char) 33)
                        .append((char) 3).toString();
                break;

            case 8:
                _cmdstr = new StringBuffer().append((char) 29).append((char) 33)
                        .append((char) 48).toString();
                break;

            case 9:
                _cmdstr = new StringBuffer().append((char) 29).append((char) 33)
                        .append((char) 51).toString();
                break;

            case 10:
                _cmdstr = new StringBuffer().append((char) 29).append((char) 33)
                        .append((char) 4).toString();
                break;

            case 11:
                _cmdstr = new StringBuffer().append((char) 29).append((char) 33)
                        .append((char) 64).toString();
                break;

            case 12:
                _cmdstr = new StringBuffer().append((char) 29).append((char) 33)
                        .append((char) 68).toString();
                break;

            }
            return _cmdstr;
        }
    public String CMD_FontSize_BTP_M280(int size) {
        String _cmdstr = "";
        // 只有0和1两种模式
        int fontsize = size;

        switch (fontsize) {
        case 1:
            _cmdstr = new StringBuffer().append((char) 28).append((char) 33)
                    .append((char) 8).toString();
            break;
        case 2:
            _cmdstr = new StringBuffer().append((char) 28).append((char) 33)
                    .append((char) 4).toString();
            break;
        case 3:
            _cmdstr = new StringBuffer().append((char) 28).append((char) 87)
                    .append((char) 1).toString();
            break;
        default:
            _cmdstr = new StringBuffer().append((char) 28).append((char) 87)
                    .append((char) 0).toString();
            break;
        }

        return _cmdstr;
    }
    // ------------------------字体变大-----------------------------

    /**
     * 字体变大为标准的n倍
     *
     * @param num
     * @return
     */
    public static byte[] fontSizeSetBig(int num) {
        byte realSize = 0;
        switch (num) {
        case 1:
            realSize = 0;
            break;
        case 2:
            realSize = 17;
            break;
        case 3:
            realSize = 34;
            break;
        case 4:
            realSize = 51;
            break;
        case 5:
            realSize = 68;
            break;
        case 6:
            realSize = 85;
            break;
        case 7:
            realSize = 102;
            break;
        case 8:
            realSize = 119;
            break;
        }
        byte[] result = new byte[3];
        result[0] = 29;
        result[1] = 33;
        result[2] = realSize;
        return result;
    }
完整的项目点击这里下载
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值