Commit 721d3247 authored by pengguangpu's avatar pengguangpu

添加海信驱动并测试

parent 13d6bbf2
...@@ -13,5 +13,6 @@ public class ExampleUnitTest { ...@@ -13,5 +13,6 @@ public class ExampleUnitTest {
@Test @Test
public void addition_isCorrect() { public void addition_isCorrect() {
assertEquals(4, 2 + 2); assertEquals(4, 2 + 2);
System.out.println("".getBytes().length);
} }
} }
\ No newline at end of file
...@@ -2,6 +2,7 @@ package com.miya.print; ...@@ -2,6 +2,7 @@ package com.miya.print;
import android.content.Context; import android.content.Context;
import com.miya.print.exception.PrinterException;
import com.miya.print.utils.D; import com.miya.print.utils.D;
/** /**
...@@ -16,8 +17,9 @@ public class PrinterManager { ...@@ -16,8 +17,9 @@ public class PrinterManager {
*/ */
enum Type { enum Type {
//这里添加扩展的驱动 //这里添加扩展的驱动
// TYPE_HISENSE("hisense", ChuangjiePrinter.class.getName()), TYPE_HISENSE("hisense", HisensePrinter.class.getName()),
TYPE_SUNMI("sunmiK1", SunmiK1Printer.class.getName()), TYPE_SUNMI_TI("sunmiT1", SunmiT1Printer.class.getName()),
TYPE_SUNMI_K1("sunmiK1", SunmiK1Printer.class.getName()),
TYPE_YINGTAI("yingtai", YingtaiPrinter.class.getName()), TYPE_YINGTAI("yingtai", YingtaiPrinter.class.getName()),
TYPE_CHUANGJIE("chuangjie", ChuangjiePrinter.class.getName()), TYPE_CHUANGJIE("chuangjie", ChuangjiePrinter.class.getName()),
TYPE_SANGDA("sangda", SangdaPrinter.class.getName()); TYPE_SANGDA("sangda", SangdaPrinter.class.getName());
...@@ -87,6 +89,7 @@ public class PrinterManager { ...@@ -87,6 +89,7 @@ public class PrinterManager {
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
printer = null;
} }
} }
//没有找到对应的打印机 //没有找到对应的打印机
...@@ -96,6 +99,7 @@ public class PrinterManager { ...@@ -96,6 +99,7 @@ public class PrinterManager {
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
printer = null;
} }
D.i(TAG, "没找到对应的打印机器,请检查"); D.i(TAG, "没找到对应的打印机器,请检查");
return false; return false;
......
...@@ -49,6 +49,7 @@ class SunmiK1Printer extends BasePrinter { ...@@ -49,6 +49,7 @@ class SunmiK1Printer extends BasePrinter {
} }
} }
}; };
;
@Override @Override
public boolean init(Context context) { public boolean init(Context context) {
...@@ -163,16 +164,24 @@ class SunmiK1Printer extends BasePrinter { ...@@ -163,16 +164,24 @@ class SunmiK1Printer extends BasePrinter {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!"); throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
} }
try { try {
int ret = setAlign(align); /**
//打印的样式默认为CODE128码,数字在条码下面 * 打印一维条形码
ret = extPrinterService.printBarCode(content, 8, 2, 120, 2); * data 条码内容
//强制走两行纸才能生效 * height 条码高度
ret = extPrinterService.lineWrap(2); * width 条码宽度
if (isFeed) { * textposition 条码内容位置
ret = feedPaper(); */
} //这里的高度固定传100,宽度传2,条码内容文字传2
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
buffer.write(new byte[]{0x1D, 0x48, (byte) 2,
0x1D, 0x77, (byte) 2, 0x1D, 0x68, (byte) 100, 0x0A});
byte[] barcode;
barcode = BytesUtil.getBytesFromDecString(content);
buffer.write(new byte[]{0x1D, 0x6B, 0x49, (byte) (barcode.length + 2), 0x7B, (byte) (0x43)});
buffer.write(barcode);
int ret = extPrinterService.sendRawData(buffer.toByteArray());
return changeStatus2Custom(ret); return changeStatus2Custom(ret);
} catch (RemoteException e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage()); throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
} }
...@@ -190,9 +199,9 @@ class SunmiK1Printer extends BasePrinter { ...@@ -190,9 +199,9 @@ class SunmiK1Printer extends BasePrinter {
* modeSize ⼆维码块⼤⼩ 1-16 像素点 * modeSize ⼆维码块⼤⼩ 1-16 像素点
* errorlevel ⼆维码纠错等级 0-3 四个等级 * errorlevel ⼆维码纠错等级 0-3 四个等级
*/ */
ret = extPrinterService.printQrCode(content, 4, 3); ret = extPrinterService.printQrCode(content, 6, 3);
//强制走两行纸才能生效 //强制走两行纸才能生效
ret = extPrinterService.lineWrap(2); ret = extPrinterService.lineWrap(3);
if (isFeed) { if (isFeed) {
ret = feedPaper(); ret = feedPaper();
} }
...@@ -231,9 +240,7 @@ class SunmiK1Printer extends BasePrinter { ...@@ -231,9 +240,7 @@ class SunmiK1Printer extends BasePrinter {
if (actualBm != null) { if (actualBm != null) {
actualBm.recycle(); actualBm.recycle();
} }
if (isFeed) { //打印图片会默认强制走纸一行
ret = feedPaper();
}
return changeStatus2Custom(ret); return changeStatus2Custom(ret);
} catch (RemoteException e) { } catch (RemoteException e) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage()); throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
...@@ -431,7 +438,6 @@ class SunmiK1Printer extends BasePrinter { ...@@ -431,7 +438,6 @@ class SunmiK1Printer extends BasePrinter {
public void printQr(String data, int modulesize, int errorlevel) { public void printQr(String data, int modulesize, int errorlevel) {
if (extPrinterService == null) { if (extPrinterService == null) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!"); throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
} }
try { try {
extPrinterService.setAlignMode(1); extPrinterService.setAlignMode(1);
......
package com.miya.print;
import android.content.Context;
public class SunmiT1Printer extends BasePrinter {
final static String TAG = SunmiT1Printer.class.getSimpleName();
@Override
public boolean init(Context context) {
return super.init(context);
}
@Override
public boolean release() {
return super.release();
}
@Override
public int printText(int align, int size, String content, boolean isFeed) {
return super.printText(align, size, content, isFeed);
}
}
package com.miya.print.utils;
import android.support.annotation.Nullable;
import java.nio.charset.Charset;
/**
* 累积工具类
*/
public class AccumulateUtils {
/**
* 80mm打印纸一行允许的最大打印字符数(默认字符大小)
*/
final static int MAX_WORDS_80MM = 46;
/**
* 58mm打印纸一行允许的最大打印字符数(默认字符大小)
*/
final static int MAX_WORDS_58MM = 32;
/**
* 80mm打印纸类型
*/
public final static int PAGE_MODE_80MM = 0;
/**
* 58mm打印纸类型
*/
public final static int PAGE_MODE_58MM = 1;
/**
* 累积字符串
*/
@Nullable
private static String accumulateStr = "";
private AccumulateUtils() {
throw new UnsupportedOperationException();
}
/**
* 累积字符串,自带换行
*
* @param pageMode 打印纸类型
* @param size 字符大小
* @param align 对齐方式
* @param str 待累积的新字符串
* @return 当前累积的字符串
*/
public static String accumulateStringWithLF(int pageMode, int align, int size, String str) {
if (str == null) {
return accumulateStr;
}
switch (pageMode) {
case PAGE_MODE_80MM:
return accumulateString_80mm(align, size, str, true);
case PAGE_MODE_58MM:
return accumulateString_58mm(align, size, str, true);
}
return accumulateStr;
}
/**
* 累积字符串
*
* @param pageMode 打印纸类型
* @param size 字符大小
* @param align 对齐方式
* @param str 待累积的新字符串
* @param isFeed 是否走纸
* @return 当前累积的字符串
*/
public static String accumulateString(int pageMode, int align, int size, String str, boolean isFeed) {
if (str == null) {
return accumulateStr;
}
switch (pageMode) {
case PAGE_MODE_80MM:
return accumulateString_80mm(align, size, str, isFeed);
case PAGE_MODE_58MM:
return accumulateString_58mm(align, size, str, isFeed);
}
return accumulateStr;
}
/**
* 立即清除已经累积的字符串
*/
public static void clearAccmulateString() {
accumulateStr = "";
}
/**
* 获取字符串的byte长度
*
* @param str
* @return
*/
private static int getBytesLength(String str) {
if (str != null) {
return str.getBytes(Charset.forName("utf-8")).length;
}
return 0;
}
/**
* 80mm打印纸累积字符串,自带换行
*
* @param align 对齐方式
* @param size 字符大小
* @param str 字符串内容
* @param isFeed 是否走纸
* @return 当前累积的字符串
*/
public static String accumulateString_80mm(int align, int size, String str, boolean isFeed) {
if (str == null) {
return accumulateStr;
}
//待拼接的字符串
String targetStr = "";
switch (align) {
case 0:
//居左
accumulateStr = accumulateStr + str;
break;
case 1:
//居中
if (getBytesLength(str) > MAX_WORDS_80MM) {
accumulateStr = accumulateStr + str;
break;
}
//计算空格数
for (int i = 0; i < (MAX_WORDS_80MM - getBytesLength(str)) / 2; i++) {
targetStr = targetStr + " ";
}
targetStr = targetStr + str;
accumulateStr = accumulateStr + targetStr;
break;
case 2:
//居右
if (getBytesLength(str) > MAX_WORDS_80MM) {
accumulateStr = accumulateStr + str;
break;
}
//计算空格数
for (int i = 0; i < MAX_WORDS_80MM - getBytesLength(str); i++) {
targetStr = targetStr + " ";
}
targetStr = targetStr + str;
accumulateStr = accumulateStr + targetStr;
break;
}
if (isFeed) {
accumulateStr += "\n";
}
return accumulateStr;
}
/**
* 58mm打印纸累积字符串,自带换行
*
* @param align 对齐方式
* @param size 字符大小
* @param str 字符串内容
* @param isFeed 是否走纸
* @return 当前累积的字符串
*/
public static String accumulateString_58mm(int align, int size, String str, boolean isFeed) {
if (str == null) {
return accumulateStr;
}
//待拼接的字符串
String targetStr = "";
switch (align) {
case 0:
//居左
accumulateStr = accumulateStr + str;
break;
case 1:
//居中
if (getBytesLength(str) > MAX_WORDS_58MM) {
accumulateStr = accumulateStr + str;
break;
}
//计算空格数
for (int i = 0; i < (MAX_WORDS_58MM - getBytesLength(str)) / 2; i++) {
targetStr = targetStr + " ";
}
targetStr = targetStr + str;
accumulateStr = accumulateStr + targetStr;
break;
case 2:
//居右
if (getBytesLength(str) > MAX_WORDS_58MM) {
accumulateStr = accumulateStr + str;
break;
}
//计算空格数
for (int i = 0; i < MAX_WORDS_58MM - getBytesLength(str); i++) {
targetStr = targetStr + " ";
}
targetStr = targetStr + str;
accumulateStr = accumulateStr + targetStr;
break;
}
if (isFeed) {
accumulateStr += "\n";
}
return accumulateStr;
}
/**
* 获取当前已经累积完的字符串
*
* @return 当前已经累积完的字符串
*/
public static String getAccumulatedString() {
return accumulateStr;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment