修改Instance方法

This commit is contained in:
梁越超 2023-10-10 00:14:58 +08:00
parent 48a6252707
commit 50c40589c9
2 changed files with 50 additions and 20 deletions

View File

@ -94,6 +94,9 @@ public class JcPrinter extends CordovaPlugin {
case "connectPrinter": case "connectPrinter":
this.connectPrinter(args, callbackContext); this.connectPrinter(args, callbackContext);
return true; return true;
case "isConnection":
this.isConnection(callbackContext);
return true;
case "action": case "action":
String methodName = args.optString(0); String methodName = args.optString(0);
JSONArray methodJsonArgs = args.getJSONArray(1); JSONArray methodJsonArgs = args.getJSONArray(1);
@ -148,14 +151,13 @@ public class JcPrinter extends CordovaPlugin {
int pageCount = data.length(); int pageCount = data.length();
quantity = 1; quantity = 1;
int totalQuantity = pageCount * quantity; int totalQuantity = pageCount * quantity;
//总打印份数表示所有页面的打印份数之和例如如果你有3页需要打印第一页打印3份第二页打印2份第三页打印5份那么count的值应为103+2+5
//总打印份数
Util.getInstance().setTotalQuantityOfPrints(totalQuantity); Util.getInstance().setTotalQuantityOfPrints(totalQuantity);
printDensity = 8; printDensity = 8;
printMode = 2; printMode = 2;
Log.d(TAG, "测试:参数设置-打印浓度: " + printDensity + ",打印模式:" + printMode); Log.d(TAG, "测试:参数设置-打印浓度: " + printDensity + ",打印模式:" + printMode);
/* /*
* 参数1 打印浓度 参数2 纸张类型 参数3 打印模式 * 参数1打印浓度 参数2:纸张类型 参数3:打印模式
* 打印浓度 B50/B50W/T6/T7/T8 建议设置6或8Z401/B32建议设置8B3S/B21/B203/B1建议设置3 * 打印浓度 B50/B50W/T6/T7/T8 建议设置6或8Z401/B32建议设置8B3S/B21/B203/B1建议设置3
*/ */
Util.getInstance().startPrintJob(printDensity, 2, printMode, new PrintCallback() { Util.getInstance().startPrintJob(printDensity, 2, printMode, new PrintCallback() {
@ -316,6 +318,29 @@ public class JcPrinter extends CordovaPlugin {
} }
private void isConnection(CallbackContext callbackContext) throws JSONException {
int connectFlag = Util.isConnection();
cordova.getActivity().runOnUiThread(() -> {
String hint = "";
switch (connectFlag) {
case 0:
hint = "已连接";
callbackContext.success(hint);
break;
case -1:
hint = "未连接";
callbackContext.error(hint);
break;
case -3:
hint = "不支持的机型";
callbackContext.error(hint);
break;
default:
break;
}
});
}
private void connectPrinter(JSONArray args, CallbackContext callbackContext) throws JSONException { private void connectPrinter(JSONArray args, CallbackContext callbackContext) throws JSONException {
JSONObject obj = args.getJSONObject(0); JSONObject obj = args.getJSONObject(0);
String address = obj.getString("address"); String address = obj.getString("address");

View File

@ -38,8 +38,12 @@ exports.connectPrinter = function (arg0, success, error) {
exec(success, error, 'JcPrinter', 'connectPrinter', [arg0]); exec(success, error, 'JcPrinter', 'connectPrinter', [arg0]);
} }
exports.connectPrinter = function (success, error) {
exec(success, error, 'JcPrinter', 'connectPrinter');
}
exports.Instance = function(){
exports.Instance = {
/** /**
* 初始化画板 * 初始化画板
* 该方法用于初始化绘图板以便进行绘制操作 * 该方法用于初始化绘图板以便进行绘制操作
@ -49,9 +53,9 @@ exports.Instance = function(){
* @param rotate 旋转角度可选值090180270表示绘图板的旋转角度 * @param rotate 旋转角度可选值090180270表示绘图板的旋转角度
* @param fontDir 字体文件路径用于绘制文本时指定字体暂不支持自定义默认即可 * @param fontDir 字体文件路径用于绘制文本时指定字体暂不支持自定义默认即可
*/ */
async function drawEmptyLabel(width, height, rotate) { drawEmptyLabel: async function (width, height, rotate, fontDir) {
return new Promise((resolve, reject) => exec(resolve,reject,"JcPrinter","action",["drawEmptyLabel",[width, height, rotate,fontDir||""]])); return new Promise((resolve, reject) => exec(resolve,reject,"JcPrinter","action",["drawEmptyLabel",[width, height, rotate,fontDir||""]]));
} },
/** /**
* 绘制文本 * 绘制文本
* 该方法用于在绘图板上绘制文本包括位置内容字体样式等信息 * 该方法用于在绘图板上绘制文本包括位置内容字体样式等信息
@ -71,9 +75,9 @@ exports.Instance = function(){
* @param lineSpace 行间距倍距单位毫米 * @param lineSpace 行间距倍距单位毫米
* @param mFontStyles 字体样式数组长度为4依次表示加粗斜体下划线删除下划线暂不生效 * @param mFontStyles 字体样式数组长度为4依次表示加粗斜体下划线删除下划线暂不生效
*/ */
async function drawLabelText(x, y, width, height, value,fontFamily, fontSize, rotate, textAlignHorizontal, textAlignVertical, lineModel, letterSpace, lineSpace, mFontStyles){ drawLabelText: async function (x, y, width, height, value,fontFamily, fontSize, rotate, textAlignHorizontal, textAlignVertical, lineModel, letterSpace, lineSpace, mFontStyles){
return new Promise((resolve, reject) => exec(resolve,reject,"JcPrinter","action",["drawLabelText",[x, y, width, height, value, fontFamily||"", fontSize, rotate, textAlignHorizontal, textAlignVertical, lineModel, letterSpace, lineSpace, mFontStyles]])); return new Promise((resolve, reject) => exec(resolve,reject,"JcPrinter","action",["drawLabelText",[x, y, width, height, value, fontFamily||"", fontSize, rotate, textAlignHorizontal, textAlignVertical, lineModel, letterSpace, lineSpace, mFontStyles]]));
} },
/** /**
* 绘制一维码 * 绘制一维码
@ -90,9 +94,9 @@ exports.Instance = function(){
* @param textHeight 文本高度单位毫米 * @param textHeight 文本高度单位毫米
* @param textPosition 文本位置可选值0-下方显示1-上方显示2-不显示 * @param textPosition 文本位置可选值0-下方显示1-上方显示2-不显示
*/ */
async function drawLabelBarCode(x, y, width, height, codeType, value, fontSize, rotate, textHeight, textPosition){ drawLabelBarCode: async function (x, y, width, height, codeType, value, fontSize, rotate, textHeight, textPosition){
return new Promise((resolve, reject) => exec(resolve,reject,"JcPrinter","action",["drawLabelBarCode",[x, y, width, height, codeType, value, fontSize, rotate, textHeight, textPosition]])); return new Promise((resolve, reject) => exec(resolve,reject,"JcPrinter","action",["drawLabelBarCode",[x, y, width, height, codeType, value, fontSize, rotate, textHeight, textPosition]]));
} },
/** /**
* 绘制二维码 * 绘制二维码
@ -106,9 +110,9 @@ exports.Instance = function(){
* @param codeType 二维码类型可选值31-QR_CODE32-PDF41733-DATA_MATRIX34-AZTEC * @param codeType 二维码类型可选值31-QR_CODE32-PDF41733-DATA_MATRIX34-AZTEC
* @param rotate 旋转角度可选值090180270 * @param rotate 旋转角度可选值090180270
*/ */
async function drawLabelQrCode(x, y, width, height, value, codeType, rotate){ drawLabelQrCode: async function (x, y, width, height, value, codeType, rotate){
return new Promise((resolve, reject) => exec(resolve,reject,"JcPrinter","action",["drawLabelQrCode",[x, y, width, height, value, codeType, rotate]])); return new Promise((resolve, reject) => exec(resolve,reject,"JcPrinter","action",["drawLabelQrCode",[x, y, width, height, value, codeType, rotate]]));
} },
/** /**
* 绘制图形 * 绘制图形
@ -125,9 +129,9 @@ exports.Instance = function(){
* @param lineType 图形类型可选值1-2-椭圆3-矩形4-圆角矩形 * @param lineType 图形类型可选值1-2-椭圆3-矩形4-圆角矩形
* @param dashWidth 虚线样式数组中包含实线段长度和空线段长度 * @param dashWidth 虚线样式数组中包含实线段长度和空线段长度
*/ */
async function drawLabelGraph(x, y, width, height, graphType, rotate, cornerRadius, lineWidth, lineType, dashWidth){ drawLabelGraph: async function (x, y, width, height, graphType, rotate, cornerRadius, lineWidth, lineType, dashWidth){
return new Promise((resolve, reject) => exec(resolve,reject,"JcPrinter","action",["drawLabelGraph",[x, y, width, height, graphType, rotate, cornerRadius, lineWidth, lineType, dashWidth]])); return new Promise((resolve, reject) => exec(resolve,reject,"JcPrinter","action",["drawLabelGraph",[x, y, width, height, graphType, rotate, cornerRadius, lineWidth, lineType, dashWidth]]));
} },
/** /**
* 绘制图像 * 绘制图像
@ -142,9 +146,9 @@ exports.Instance = function(){
* @param imageProcessingType 处理算法默认1即可 * @param imageProcessingType 处理算法默认1即可
* @param imageProcessingValue 阈值 阈值默认127即可 * @param imageProcessingValue 阈值 阈值默认127即可
*/ */
async function drawLabelImage(imageData, x, y, width, height, rotate, imageProcessingType, imageProcessingValue){ drawLabelImage: async function (imageData, x, y, width, height, rotate, imageProcessingType, imageProcessingValue){
return new Promise((resolve, reject) => exec(resolve,reject,"JcPrinter","action",["drawLabelImage",[imageData, x, y, width, height, rotate, imageProcessingType, imageProcessingValue]])); return new Promise((resolve, reject) => exec(resolve,reject,"JcPrinter","action",["drawLabelImage",[imageData, x, y, width, height, rotate, imageProcessingType, imageProcessingValue]]));
} },
/** /**
* 绘制线条 * 绘制线条
@ -158,12 +162,13 @@ exports.Instance = function(){
* @param lineType 线条类型1:实线2:虚线类型虚实比例1:1 * @param lineType 线条类型1:实线2:虚线类型虚实比例1:1
* @param dashWidth 线条为虚线时的宽度包含实线段长度和空线段长度单位毫米 * @param dashWidth 线条为虚线时的宽度包含实线段长度和空线段长度单位毫米
*/ */
async function drawLabelLine(x, y, width, height, rotate, lineType, dashWidth){ drawLabelLine: async function (x, y, width, height, rotate, lineType, dashWidth){
return new Promise((resolve, reject) => exec(resolve,reject,"JcPrinter","action",["drawLabelLine",[x, y, width, height, rotate, lineType, dashWidth]])); return new Promise((resolve, reject) => exec(resolve,reject,"JcPrinter","action",["drawLabelLine",[x, y, width, height, rotate, lineType, dashWidth]]));
} },
async function generateLabelJson(){ generateLabelJson: async function (){
return new Promise((resolve, reject) => exec(resolve,reject,"JcPrinter","generateLabelJson")); return new Promise((resolve, reject) => exec(resolve,reject,"JcPrinter","generateLabelJson"));
} }
}();
};