Commit c792f59f authored by pengguangpu's avatar pengguangpu

添加打印sdk专用异常;添加桑达打印驱动

parent 45e85213
......@@ -73,7 +73,7 @@
android:text="打印默认字符" />
</TableRow>
<TableRow>
<TableRow android:visibility="gone">
<EditText
android:id="@+id/etTextSize"
......@@ -139,26 +139,24 @@
android:text="切纸" />
</TableRow>
<TableRow>
<TableRow android:visibility="gone">
<Button
android:id="@+id/btnSet58"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="设置成58mm打印纸"
android:visibility="gone" />
android:text="设置成58mm打印纸" />
</TableRow>
<TableRow>
<TableRow android:visibility="gone">
<Button
android:id="@+id/btnSet80"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="设置成80mm打印纸"
android:visibility="gone" />
android:text="设置成80mm打印纸" />
</TableRow>
<TableRow>
......
......@@ -40,4 +40,9 @@ dependencies {
*/
implementation files('libs/CJusbjar.jar')
implementation files('libs/PrintCmd.jar')
/**
* 桑达&中科英泰打印jar包
*/
implementation files('libs/icod_3.1.1.jar')
}
......@@ -23,6 +23,15 @@ public class BasePrinter implements IPrinter {
*/
String name;
/**
* 对齐方式
* 0:居左对齐
* 1:居中对齐
* 2:居右对齐
* 其它值:走当前配置对齐方式
*/
int align = 0;
@Override
public boolean init(Context context) {
this.context = context;
......
......@@ -41,15 +41,6 @@ public class ChuangjiePrinter extends BasePrinter {
*/
int textSize = -1;
/**
* 对齐方式
* 0:居左对齐
* 1:居中对齐
* 2:居右对齐
* 其它值:走当前配置对齐方式
*/
int align = 0;
/**
* 80mm打印纸允许打印的最大图片的宽度,此为经验值
*/
......@@ -275,10 +266,14 @@ public class ChuangjiePrinter extends BasePrinter {
actualBm = BitmapUtils.scaleBitmap(bm, MAX_IMG_WIDTH_80MM, 0);
ret = printQrcodeBitmap(actualBm);
} else {
//如果是居中打印,则拼接白色左边距
if (align == 1) {
//如果是居中打印,则拼接白色左边距
actualBm = BitmapUtils.addWhiteLeftMargin(bm, (MAX_IMG_WIDTH_80MM - bm.getWidth()) / 2);
ret = printQrcodeBitmap(actualBm);
} else if (align == 2) {
//居右打印
actualBm = BitmapUtils.addWhiteLeftMargin(bm, MAX_IMG_WIDTH_80MM - bm.getWidth());
ret = printQrcodeBitmap(actualBm);
}
}
if (actualBm != null) {
......@@ -293,15 +288,16 @@ public class ChuangjiePrinter extends BasePrinter {
@Override
public int printQrcode(int align, String content, boolean isFeed) {
int ret = setAlign(align);
//创捷机器打印二维码不能通过setAlign实现居中,永远会居左打印
if (this.align == 0) {
//居左打印
ret = printQrcode(content, 0, 6, 1);
} else if (this.align == 1) {
//居中打印
// ret = printQrcode()
ret = printQrcode(content, 12, 6, 1);
} else if (this.align == 2) {
//居右
// ret = printQrcode()
ret = printQrcode(content, 24, 6, 1);
}
if (isFeed) {
ret = feedPaper();
......@@ -312,6 +308,7 @@ public class ChuangjiePrinter extends BasePrinter {
@Override
public int printBarcode(int align, String content, boolean isFeed) {
int ret = setAlign(align);
//创捷机器打印条码只能居中打印,并且内部会强制走纸
ret = printBarCode(content);
if (isFeed) {
ret = feedPaper();
......@@ -361,6 +358,7 @@ public class ChuangjiePrinter extends BasePrinter {
* @param iRound 环绕模式,0环绕(混排,有些机型不支持)、1立即打印(不混排)
* @return
*/
@Deprecated
public int printQrcode(String StrData, int iLmargin, int iMside, int iRound) {
int printState = -1;
if (null != mUsbDriver && null != mUsbDev) {
......@@ -369,13 +367,22 @@ public class ChuangjiePrinter extends BasePrinter {
return printState;
}
/**
* 打印图片
*
* @param data 源图片
* @return
*/
@Deprecated
public int printQrcodeBitmap(Bitmap data) {
int printState = -1;
if (null != mUsbDriver && null != mUsbDev) {
Bitmap bm = ImageUtils.getSinglePic(data);
int[] pixData = ImageUtils.getPixelsByBitmap(bm);
printState = mUsbDriver.write(PrintCmd.printQrcodeBitmap(pixData, data.getWidth(), data.getHeight()));
if (bm != null) {
bm.recycle();
}
}
return printState;
}
......
......@@ -92,7 +92,7 @@ public enum PrinterStatusEnum {
* 根据状态码查找对应的枚举
*
* @param status 状态码
* @return 找到了就返回对应的枚举,没找到就返回null
* @return 找到了就返回对应的枚举,没找到就返回CODE_FAILED(-1, "操作失败")
*/
public static PrinterStatusEnum find(int status) {
for (PrinterStatusEnum statusEnum : PrinterStatusEnum.values()) {
......@@ -100,6 +100,6 @@ public enum PrinterStatusEnum {
return statusEnum;
}
}
return null;
return CODE_FAILED;
}
}
package com.miya.print;
import android.content.Context;
import com.miya.print.exception.PrinterException;
import com.miya.print.utils.D;
import com.szsicod.print.escpos.PrinterAPI;
import com.szsicod.print.io.InterfaceAPI;
import com.szsicod.print.io.SerialAPI;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
/**
* 桑达机器打印驱动
*/
public class SangdaPrinter extends BasePrinter {
final static String TAG = SangdaPrinter.class.getSimpleName();
public PrinterAPI mPrinter = PrinterAPI.getInstance();
InterfaceAPI io = null;
@Override
public boolean init(Context context) {
super.init(context);
if (isConnect()) {
mPrinter.disconnect();
}
try {
io = new SerialAPI(new File("/dev/ttyS1"), 38400, 0);
if (PrinterAPI.SUCCESS == mPrinter.connect(io)) {
D.i(TAG, "桑达打印机连接成功");
return true;
} else {
D.e(TAG, "桑达打印机连接失败");
return false;
}
} catch (Exception e) {
D.e(TAG, "桑达打印机连接失败");
}
return false;
}
@Override
public boolean release() {
disconnect();
return true;
}
@Override
public int printText(int align, int size, String content, boolean isFeed) {
if (mPrinter != null) {
int ret = setAlign(align);
try {
ret = mPrinter.printString(content, "GBK", isFeed);
return ret;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), e.getMessage());
}
}
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
@Override
public int printBarcode(int align, String content, boolean isFeed) {
if (mPrinter != null) {
int ret = setAlign(align);
try {
ret = mPrinter.printBarCode(0, content.length(), content);
if (isFeed) {
ret = feedPaper();
}
return ret;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), e.getMessage());
}
}
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
@Override
public int setAlign(int align) {
if (mPrinter != null) {
int ret = mPrinter.setAlignMode(align);
this.align = align;
return ret;
}
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
@Override
public int feedPaper() {
if (mPrinter != null) {
int ret = mPrinter.printFeed();
return ret;
}
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
public PrinterAPI getPrinter() {
return mPrinter;
}
public void disconnect() {
if (mPrinter != null) {
mPrinter.disconnect();
mPrinter = null;
D.i(TAG, "桑达打印资源已经关闭");
}
}
public boolean isConnect() {
if (mPrinter != null) {
return mPrinter.isConnect();
}
return false;
}
public String getStatusDescribe() {
int status = mPrinter.getStatus();
Arrays.fill(statusList, 0);
try {
if (status == -1) {
statusList[0] = 1;
return "数据传输错误,请检查连接并重新发送";
}
StringBuilder builder = new StringBuilder();
StringBuffer descriptBuffer = new StringBuffer();
StringBuffer troubleBuffer = new StringBuffer();
//传感应状态
if ((status & 0x200) > 0) {
statusList[1] = 1;
descriptBuffer.append("少纸, ");//[1]
troubleBuffer.append("PaperFew|");
}
if ((status & 0x400) > 0 || (status & 0x08) > 0) {
statusList[2] = 1;
descriptBuffer.append("缺纸, ");
troubleBuffer.append("OutOfPaper|");
}
//脱机状态
if ((status & 0x4) > 0) {
descriptBuffer.append("发生错误, ");
troubleBuffer.append("happen error|");
}
if ((status & 0x20) > 0) {
statusList[4] = 1;
descriptBuffer.append("盖板打开, ");
troubleBuffer.append("box open|");
}
//打印机状态
if ((status & 0x1) > 0) {
statusList[3] = 1;
descriptBuffer.append("脱机, ");
troubleBuffer.append("Offline|");
}
if ((status & 0x2) > 0 || (status & 0x10) > 0) {
descriptBuffer.append("正在feed, ");//[8]
troubleBuffer.append("feeding|");
}
//错误状态
if ((status & 0x100) > 0) {
statusList[5] = 1;
descriptBuffer.append("机械错误, ");
troubleBuffer.append("MachineError|");
}
if ((status & 0x40) > 0) {
statusList[6] = 1;
descriptBuffer.append("可自动恢复错误, ");
troubleBuffer.append("CorrectingError|");
}
if ((status & 0x80) > 0) {
statusList[7] = 1;
descriptBuffer.append("不可恢复错误, ");
troubleBuffer.append("NotCorrectError|");
}
String descript = descriptBuffer.toString().trim();
if (!descript.isEmpty()) {
descript = descript.substring(0, descript.length() - 1);
} else {
descript = "正常";
}
return descript;
} catch (Exception e) {
return "Offline123123";
}
}
private static int[] statusList = { //错误列表
//数据传输错误0
0,
0, //少纸1
0, //缺纸2
0, //脱机3
0, //机头抬起4
0, //机械错误5
0, //可自动恢复错误6
0, //不可恢复错误7
//offline130
};
/**
* 判断打印机是否可以正常打印
*/
public static boolean isPrinterOk() {
for (int i = 0; i < statusList.length; i++) {
if (statusList[i] != 0) {
return false;
}
}
return true;
}
/**
* 判断缺纸
*
* @return
*/
public static boolean isOutOfPaper() {
if (statusList[2] == 1) {
return true;
}
return false;
}
/**
* 判断脱机
*
* @return
*/
public static boolean isOffline() {
if (statusList[0] == 1 || statusList[3] == 1) {
return true;
}
return false;
}
}
package com.miya.print.exception;
import android.util.Printer;
/**
* 打印异常定义
*/
public class PrinterException extends RuntimeException {
/**
* 错误码
*/
int errCode;
/**
* 错误描述
*/
String errDesc;
public PrinterException(int errCode, String errDesc) {
super(errDesc);
this.errCode = errCode;
this.errDesc = errDesc;
}
public int getErrCode() {
return errCode;
}
public void setErrCode(int errCode) {
this.errCode = errCode;
}
public String getErrDesc() {
return errDesc;
}
public void setErrDesc(String errDesc) {
this.errDesc = errDesc;
}
@Override
public String toString() {
return "PrinterException{" +
"errCode=" + errCode +
", errDesc='" + errDesc + '\'' +
'}';
}
}
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