打印机共享搜不到

  • 打印机共享搜不到已关闭评论
  • A+
所属分类:远程维修
摘要

节privatestaticfinalintMAX_DATA_SIZE=700;publicstaticfinalintTYPE_BYTE=0X1;//字节流publicstaticfinalintTYPE_STRING=0X2;//字符流privatestaticESCUtilmEsc;


private static final int MAX_DATA_SIZE = 700;

public static final int TYPE_BYTE = 0X1; // 字节流
public static final int TYPE_STRING = 0X2; // 字符流

private static ESCUtil mEsc;
private static Context mContext;

/**
* 获取单例
*
* @return
*/
public static ESCUtil getEsc() {
return mEsc;
}

public ESCUtil(Context context) {
mContext = context;
}

public static ESCUtil getInstance(Context context) {
if (mEsc == null) {
synchronized (ESCUtil.class) {
if (mEsc == null) {
mEsc = new ESCUtil(context.getApplicationContext());
}
}
}
return mEsc;
}

//设置绝对位置
public void selectPosition() {
byte[] data = new byte[]{0x1b, 0x24, 0x32, 0x30, 0x20, 0x35, 0x20, 0x30};
sendByteData(data);
}

/**
* 将字符串安装指定字符集编码,发送出去
*
* @param data 待处理数据
* @param type 发送数据类型
*/
public void sendData(String data, int type) {
if (TextUtils.isEmpty(data)) {
return;
}
try {
// 将字符串转换为字节
byte[] send;
if (type == TYPE_BYTE) {
send = data.getBytes("ISO-8859-1");
} else {
send = data.getBytes("gb2312");
}
sendByteData(send);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

/**
* 发送字节流数据
*
* @param data 待处理数据
*/
public void sendByteData(byte[] data) {
if (data == null || data.length <= 0) { return; } // 每次发送700字节 if (data.length > MAX_DATA_SIZE) {
byte[] send;
// 将数据拆分
List bytes = splitByte(data, MAX_DATA_SIZE);
for (int i = 0; i < bytes.size(); i++) { send = bytes.get(i); Formater.printByteData(send); USBPort.writeData(send); } } else { Formater.printByteData(data); USBPort.writeData(data); } } /** * 将原始byte数据拆分,每次发送的最大字节数为{@link #MAX_DATA_SIZE} * * @param src 图片源数据 * @param len 每次发送的最大字节数 * @return */ private List splitByte(byte[] src, int len) {
int count = (src.length + len - 1) / len;
byte[] cache;
List bytes = new ArrayList<>();
for (int i = 0; i < count; i++) { int start = i * len; int end = (i + 1) * len; if (end > src.length) {
end = src.length;
}
cache = new byte[end - start];
System.arraycopy(src, start, cache, 0, end - start);
bytes.add(cache);
}
return bytes;
}

//初始化打印机
public void initPrinter() {
byte[] data = new byte[2];
data[0] = 0x1B;
data[1] = 0x40;
sendByteData(data);
sendByteData(new byte[]{(byte) 0x1B, 0x61, 0x01}); //居中
}

//换行
public void lineFeed() {
sendByteData(new byte[]{(byte) 0x1B, 0x64, 0x05});
}

//设置行间距为0
public void setLineSpaceZero() {
sendByteData(new byte[]{(byte) 0x1B, 0x32});
}

//初始化fontA
public void initFontA() {
sendByteData(new byte[]{(byte) 0x1B, 0x4D, 0x00});
}

//初始化fontB
public void initFontB() {
sendByteData(new byte[]{(byte) 0x1B, 0x4D, 0x01});
}

//设置字

  • 微信在线客服
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 扫一扫码上服务
  • weinxin