Android 实现蓝牙打印的功能

本文介绍了一个Android蓝牙打印工具类,包括初始化、打印方法、图片处理等。通过蓝牙Socket连接,支持打印文字、图片,提供了汉字处理和定位等功能,适用于蓝牙打印机开发。
摘要由CSDN通过智能技术生成

第一步:首先需要一个蓝牙打印工具类
import android.bluetooth.BluetoothSocket;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

import com.github.promeg.pinyinhelper.Pinyin;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Calendar;

/**

  • 蓝牙打印工具类
    */
    public class PrintUtil {

    private OutputStreamWriter mWriter = null;
    private OutputStream mOutputStream = null;

    public final static int WIDTH_PIXEL = 384;
    public final static int IMAGE_SIZE = 320;
    private static String myTime;

    /**

    • 初始化Pos实例
    • @param encoding 编码
    • @throws IOException
      */
      public PrintUtil(OutputStream outputStream, String encoding) throws IOException {
      mWriter = new OutputStreamWriter(outputStream, encoding);
      mOutputStream = outputStream;
      initPrinter();
      }

    public void print(byte[] bs) throws IOException {
    mOutputStream.write(bs);
    }

    public void printRawBytes(byte[] bytes) throws IOException {
    mOutputStream.write(bytes);
    mOutputStream.flush();
    }

    /**

    • 初始化打印机
    • @throws IOException
      */
      public void initPrinter() throws IOException {
      mWriter.write(0x1B);
      mWriter.write(0x40);
      mWriter.flush();
      //获取当前时间
      getTime();
      }

    /**

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

    /**

    • 打印换行(只换一行)
    • @throws IOException
      */
      public void printLine() throws IOException {
      printLine(1);
      }

    /**

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

    /**

    • 绝对打印位置
    • @return
    • @throws IOException
      */
      public byte[] setLocation(int offset) throws IOException {
      byte[] bs = new byte[4];
      bs[0] = 0x1B;
      bs[1] = 0x24;
      bs[2] = (byte) (offset % 256);
      bs[3] = (byte) (offset / 256);
      return bs;
      }

    public byte[] getGbk(String stText) throws IOException {
    byte[] returnText = stText.getBytes(“GBK”); // 必须放在try内才可以
    return returnText;
    }

    private int getStringPixLength(String str) {
    int pixLength = 0;
    char c;
    for (int i = 0; i < str.length(); i++) {
    c = str.charAt(i);
    if (Pinyin.isChinese©) {
    pixLength += 24;
    } else {
    pixLength += 12;
    }
    }
    return pixLength;
    }

    public int getOffset(String str) {
    return WIDTH_PIXEL - getStringPixLength(str);
    }

    /**

    • 打印文字
    • @param text
    • @throws IOException
      */
      public void printText(String text) throws IOException {
      mWriter.write(text);
      mWriter.flush();
      }

    /**

    • 对齐0:左对齐,1:居中,2:右对齐
      */
      public void printAlignment(int alignment) throws IOException {
      mWriter.write(0x1b);
      mWriter.write(0x61);
      mWriter.write(alignment);
      }

    public void printLargeText(String text) throws IOException {

     mWriter.write(0x1b);
     mWriter.write(0x21);
     mWriter.write(48);
    
     mWriter.write(text);
    
     mWriter.write(0x1b);
     mWriter.write(0x21);
     mWriter.write(0);
    
     mWriter.flush();
    

    }

    public void printTwoColumn(String title, String content) throws IOException {
    int iNum = 0;
    byte[] byteBuffer = new byte[100];
    byte[] tmp;

     tmp = getGbk(title);
     System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length);
     iNum += tmp.length;
    
     tmp = setLocation(getOffset(content));
     System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length);
     iNum += tmp.length;
    
     tmp = getGbk(content);
     System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length);
    
     print(byteBuffer);
    

    }

    public void printThreeColumn(String left, String middle, String right) throws IOException {
    int iNum = 0;
    byte[] byteBuffer = new byte[200];
    byte[] tmp = new byte[0];

     System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length);
     iNum += tmp.length;
    
     tmp = getGbk(left);
     System.arraycopy(tmp, 0, byteBuffer, iNum, tmp.length);
     iNum += tmp.length;
    
     int pixLength = getStringPixLength(left) % WIDTH_PIXEL;
     if (pixLength > WIDTH_PIXEL / 2 || pixLength == 0) {
         middle = "\n\t\t" + middle;
     }
    
     tmp = setLocation(192);
     System.arraycopy(tmp, 0, byteBuffer, iN
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值