diff --git a/src/android/cordova/plugin/jcprinter/JcPrinter.java b/src/android/cordova/plugin/jcprinter/JcPrinter.java index 590f432..bfb7e77 100644 --- a/src/android/cordova/plugin/jcprinter/JcPrinter.java +++ b/src/android/cordova/plugin/jcprinter/JcPrinter.java @@ -28,6 +28,8 @@ import androidx.core.app.ActivityCompat; import com.gengcon.www.jcprintersdk.bean.ImageDataInfo; import com.gengcon.www.jcprintersdk.callback.PrintCallback; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -39,18 +41,8 @@ public class JcPrinter extends CordovaPlugin { private static final String TAG = "MainActivity"; private static final String RB_THERMAL = "热转印"; private Context context; - /** - * 图像数据 - */ - private ArrayList jsonList; - /** - * 图像处理数据 - */ - private ArrayList infoList; - /** - * 总页数 - */ - private int pageCount; + + private Method[] methods = Util.getInstance().getClass().getDeclaredMethods(); /** * 页打印份数 @@ -102,6 +94,35 @@ public class JcPrinter extends CordovaPlugin { case "connectPrinter": this.connectPrinter(args, callbackContext); return true; + case "action": + String methodName = args.optString(0); + JSONArray methodJsonArgs = args.getJSONArray(1); + Object[] methodArgs = new Object[methodJsonArgs.length()]; + for(int i=0;iinfo = new ArrayList<>(); + for(int i=0;i pageCount) { return; } - //已生成数据的页数小于总页数才生成数据 - if (generatedPrintDataPageCount[0] < pageCount) { - //需要生成的数据长度 - int commitDataLength = Math.min((pageCount - generatedPrintDataPageCount[0]), bufferSize); - generateMultiPagePrintData(generatedPrintDataPageCount[0], generatedPrintDataPageCount[0] + commitDataLength); - Util.getInstance().commitData( - jsonList.subList(generatedPrintDataPageCount[0], generatedPrintDataPageCount[0] + commitDataLength), - infoList.subList(generatedPrintDataPageCount[0], generatedPrintDataPageCount[0] + commitDataLength)); - generatedPrintDataPageCount[0] += commitDataLength; - + ListpageData = new ArrayList<>(); + JSONArray array= data.optJSONArray(pageIndex); + for(int i=0;i(); - infoList = new ArrayList<>(); - } - private void connectPrinter(JSONArray args, CallbackContext callbackContext) throws JSONException { JSONObject obj = args.getJSONObject(0); String address = obj.getString("address"); @@ -381,96 +392,6 @@ public class JcPrinter extends CordovaPlugin { callbackContext.success(printers); return printers; } - - private void generateMultiPagePrintData(int index, int cycleIndex) { - while (index < cycleIndex) { - float width = 40; - float height = 60; - int orientation = 0; - float marginX = 2.0F; - float marginY = 2.0F; - //矩形框类型 - float rectangleWidth = width - marginX * 2; - float rectangleHeight = height - marginY * 2; - float lineWidth = 0.5F; - //1.圆 2.椭圆 3.矩形 4.圆角矩形 - int graphType = 3; - - float lineHeight = rectangleHeight / 5.0F; - - float titleWidth = rectangleWidth * 2 / 5.0F; - float contentWidth = rectangleWidth * 3 / 5.0F; - - float fontSize = 3.0F; - float offsetY = 0F; - float offsetX = 0F; - - - /* - * 设置画布大小 - * - * @param width 画布宽度 - * @param height 画布高度 - * @param orientation 画布旋转角度 - * @param fontDir 字体路径暂不可用,默认""即可 - * - */ - Util.getInstance().drawEmptyLabel(width, height, orientation, ""); -// Util.getInstance().drawLabelText(2.5F, 2.0F, rectangleWidth, rectangleHeight, "型号", "宋体", fontSize, 0, 0, 0, 6, 0, 1, new boolean[]{false, false, false, false}); - - //绘制图形 - Util.getInstance().drawLabelGraph(marginX + offsetX, marginY + offsetY, rectangleWidth, rectangleHeight, graphType, 0, 2, lineWidth, 1, new float[]{0.7575f, 0.7575f}); - - //换行模式使用6,宽高固定,内容过大时缩放(区别于模式1的地方在于 文字内容按预算字号,预算文本框宽度排版后未超出预设高度时,不会放大文字,而是按照预设对齐方式将文字对齐文本框) - - //绘制线条 - Util.getInstance().drawLabelLine(marginX + offsetX, marginY + lineHeight - lineWidth + offsetY, rectangleWidth, lineWidth, 0, 1, new float[]{}); - Util.getInstance().drawLabelLine(marginX + offsetX, marginY + lineHeight * 2 - lineWidth + offsetY, rectangleWidth, lineWidth, 0, 1, new float[]{}); - Util.getInstance().drawLabelLine(marginX + offsetX, marginY + lineHeight * 3 - lineWidth + offsetY, rectangleWidth, lineWidth, 0, 1, new float[]{}); - Util.getInstance().drawLabelLine(marginX + offsetX, marginY + lineHeight * 4 - lineWidth + offsetY, rectangleWidth, lineWidth, 0, 1, new float[]{}); - - Util.getInstance().drawLabelLine(marginX + titleWidth - lineWidth + offsetX, marginY + lineHeight + offsetY, lineWidth, rectangleHeight - lineHeight, 0, 1, new float[]{}); - - //绘制大标题,换行模式使用6,宽高固定,内容过大时缩放(区别于模式1的地方在于 文字内容按预算字号,预算文本框宽度排版后未超出预设高度时,不会放大文字,而是按照预设对齐方式将文字对齐文本框) - - Util.getInstance().drawLabelText(marginX * 3 + offsetX, marginY + offsetY, rectangleWidth - marginX * 4, lineHeight, "|AA|1052535|01|2389W08559|#奥莱斯#12R22.5-AR535", "宋体", fontSize * 1.5F, 0, 1, 1, 6, 0, 1, new boolean[]{false, false, false, false}); - // 绘制小标题 - Util.getInstance().drawLabelText(marginX * 2.5f + offsetX, marginY + lineHeight - lineWidth + offsetY, titleWidth - marginX * 3, lineHeight, "型号", "宋体", fontSize, 0, 1, 1, 6, 0, 1, new boolean[]{false, false, false, false}); - Util.getInstance().drawLabelText(marginX * 2.5f + offsetX, marginY + lineHeight * 2 - lineWidth + offsetY, titleWidth - marginX * 3, lineHeight, "资产编号", "宋体", fontSize, 0, 1, 1, 6, 0, 1, new boolean[]{false, false, false, false}); - Util.getInstance().drawLabelText(marginX * 2.5f + offsetX, marginY + lineHeight * 3 - lineWidth + offsetY, titleWidth - marginX * 3, lineHeight, "启用日期", "宋体", fontSize, 0, 1, 1, 6, 0, 1, new boolean[]{false, false, false, false}); - Util.getInstance().drawLabelText(marginX * 2.5f + offsetX, marginY + lineHeight * 4 - lineWidth + offsetY, titleWidth - marginX * 3, lineHeight, "存放地点", "宋体", fontSize, 0, 1, 1, 6, 0, 1, new boolean[]{false, false, false, false}); - - Util.getInstance().drawLabelText(marginX * 2.5f + titleWidth + offsetX, marginY + lineHeight - lineWidth + offsetY, contentWidth - marginX * 3, lineHeight, "DELL显示器 E6540", "宋体", fontSize, 0, 0, 1, 6, 0, 1, new boolean[]{false, false, false, false}); - Util.getInstance().drawLabelText(marginX * 2.5f + titleWidth + offsetX, marginY + lineHeight * 2 - lineWidth + offsetY, contentWidth - marginX * 3, lineHeight, "C212004", "宋体", fontSize, 0, 0, 1, 6, 0, 1, new boolean[]{false, false, false, false}); - Util.getInstance().drawLabelText(marginX * 2.5f + titleWidth + offsetX, marginY + lineHeight * 3 - lineWidth + offsetY, contentWidth - marginX * 3, lineHeight, "2014-06-10", "宋体", fontSize, 0, 0, 1, 6, 0, 1, new boolean[]{false, false, false, false}); - Util.getInstance().drawLabelText(marginX * 2.5f + titleWidth + offsetX, marginY + lineHeight * 4 - lineWidth + offsetY, contentWidth - marginX * 3, lineHeight, (index + 1) + "号办公室", "宋体", fontSize, 0, 0, 1, 6, 0, 1, new boolean[]{false, false, false, false}); - - - //生成打印数据 - byte[] jsonByte = Util.getInstance().generateLabelJson(); - - //转换为jsonStr - String jsonStr = new String(jsonByte); - - - jsonList.add(jsonStr); - //除B32/Z401/T8的printMultiple为11.81,其他的为8 - //printQuantity属性用于指定当前页的打印份数。例如,如果你需要打印3页,第一页打印3份,第二页打印2份,第三页打印5份,那么在3次提交数据时,printQuantity值分别应为3,2,5。 - String jsonInfo = "{ " + - "\"printerImageProcessingInfo\": " + "{ " + - "\"orientation\":" + orientation + "," + - " \"margin\": [ 0, 0, 0, 0 ], " + - " \"printQuantity\": " + quantity + ", " + - " \"horizontalOffset\": 0, " + - " \"verticalOffset\": 0, " + - " \"width\":" + width + "," + - " \"height\":" + height + "," + - "\"printMultiple\":" + printMultiple + "," + - " \"epc\": \"\" }}"; - infoList.add(jsonInfo); - - index++; - } - } + } diff --git a/www/jcPrinter.js b/www/jcPrinter.js index 70004cc..ad9edcf 100644 --- a/www/jcPrinter.js +++ b/www/jcPrinter.js @@ -10,4 +10,137 @@ exports.getList = function (arg0, success, error) { exports.connectPrinter = function (arg0, success, error) { exec(success, error, 'JcPrinter', 'connectPrinter', [arg0]); +} + +exports.generateData = function(instance, success, error){ + +} + +exports.Instance = function(){ + /** + * 初始化画板 + * 该方法用于初始化绘图板,以便进行绘制操作。 + * + * @param width 绘图板的宽度,单位:毫米 + * @param height 绘图板的高度,单位:毫米 + * @param rotate 旋转角度,可选值:0、90、180、270,表示绘图板的旋转角度 + * @param fontDir 字体文件路径,用于绘制文本时指定字体,暂不支持自定义,默认“”即可 + */ + function drawEmptyLabel(width, height, rotate,success,error) { + exec(success,error,"JcPrinter","action",["drawEmptyLabel",[width, height, rotate,fontDir||""]]); + } + /** + * 绘制文本 + * 该方法用于在绘图板上绘制文本,包括位置、内容、字体样式等信息。 + * + * @param x 位置x,单位:毫米 + * @param y 位置y,单位:毫米 + * @param width 文本框宽度,单位:毫米 + * @param height文本框高度,单位:毫米 + * @param value 文本内容 + * @param fontFamily 字体名称,传入空字符串时使用默认字体(暂时用默认字体) + * @param fontSize 字体大小,单位:毫米 + * @param rotate 旋转角度,可选值:0、90、180、270 + * @param textAlignHorizontal 水平对齐方式:0-左对齐,1-居中对齐,2-右对齐 + * @param textAlignVertical 垂直对齐方式:0-顶对齐,1-垂直居中,2-底对齐 + * @param lineModel 行模式:1-宽高固定,内容大小自适应,2-宽度固定,高度自适应,3-宽高固定,超出内容用省略号表示,4-宽高固定,超出内容直接裁切,6-宽高固定,内容超过预设的宽高时自动缩小 + * @param letterSpace 字母之间的标准间隔,单位:毫米 + * @param lineSpace 行间距(倍距),单位:毫米 + * @param mFontStyles 字体样式,数组长度为4,依次表示:加粗,斜体,下划线,删除下划线(暂不生效) + */ + function drawLabelText(x, y, width, height, value,fontFamily, fontSize, rotate, textAlignHorizontal, textAlignVertical, lineModel, letterSpace, lineSpace, mFontStyles,success,error){ + exec(success,error,"JcPrinter","action",["drawLabelText",[x, y, width, height, value, fontFamily||"", fontSize, rotate, textAlignHorizontal, textAlignVertical, lineModel, letterSpace, lineSpace, mFontStyles]]); + } + + /** + * 绘制一维码 + * 该方法用于在绘图板上绘制一维码,包括位置、类型、内容等信息。 + * + * @param x 水平坐标x,单位:毫米 + * @param y 垂直坐标y,单位:毫米 + * @param width 宽度,单位:毫米 + * @param height 高度,单位:毫米 + * @param codeType 一维码类型,可选值:20-CODE128,21-UPC-A,22-UPC-E,23-EAN8,24-EAN13,25-CODE93,26-CODE39,27-CODEBAR,28-ITF25 + * @param value 一维码内容 + * @param fontSize 字号大小,单位:毫米 + * @param rotate 旋转角度,可选值:0、90、180、270 + * @param textHeight 文本高度,单位:毫米 + * @param textPosition 文本位置,可选值:0-下方显示,1-上方显示,2-不显示 + */ + function drawLabelBarCode(x, y, width, height, codeType, value, fontSize, rotate, textHeight, textPosition,success,error){ + exec(success,error,"JcPrinter","action",["drawLabelBarCode",[x, y, width, height, codeType, value, fontSize, rotate, textHeight, textPosition]]); + } + + /** + * 绘制二维码 + * 该方法用于在绘图板上绘制二维码,包括位置、类型、内容等信息。 + * + * @param x 水平坐标x,单位:毫米 + * @param y 垂直坐标y,单位:毫米 + * @param width 宽度,单位:毫米 + * @param height 高度,单位:毫米 + * @param value 二维码内容 + * @param codeType 二维码类型,可选值:31-QR_CODE,32-PDF417,33-DATA_MATRIX,34-AZTEC + * @param rotate 旋转角度,可选值:0、90、180、270 + */ + function drawLabelQrCode(x, y, width, height, value, codeType, rotate,success,error){ + exec(success,error,"JcPrinter","action",["drawLabelQrCode",[x, y, width, height, value, codeType, rotate]]); + } + + /** + * 绘制图形 + * 该方法用于在绘图板上绘制图形,包括位置、类型、线条样式等信息。 + * + * @param x 水平坐标x,单位:毫米 + * @param y 垂直坐标y,单位:毫米 + * @param width 宽度,单位:毫米 + * @param height 高度,单位:毫米 + * @param graphType 线条类型,可选值:1-实线,2-虚线类型(虚实比例1:1) + * @param rotate 旋转角度,可选值:0、90、180、270 + * @param cornerRadius 圆角半径,单位:毫米 + * @param lineWidth 线宽,单位:毫米 + * @param lineType 图形类型,可选值:1-圆,2-椭圆,3-矩形,4-圆角矩形 + * @param dashWidth 虚线样式,数组中包含实线段长度和空线段长度 + */ + function drawLabelGraph(x, y, width, height, graphType, rotate, cornerRadius, lineWidth, lineType, dashWidth,success,error){ + exec(success,error,"JcPrinter","action",["drawLabelGraph",[x, y, width, height, graphType, rotate, cornerRadius, lineWidth, lineType, dashWidth]]); + } + + /** + * 绘制图像 + * 该方法用于在绘图板上绘制图像,包括图像的Base64数据、位置、大小、旋转角度等信息。 + * + * @param imageData 图像的Base64数据(去除数据头) + * @param x 水平坐标x,单位:毫米 + * @param y 垂直坐标y,单位:毫米 + * @param width 宽度,单位:毫米 + * @param height 高度,单位:毫米 + * @param rotate 旋转角度,可选值:0、90、180、270 + * @param imageProcessingType 处理算法,默认1即可 + * @param imageProcessingValue 阈值 阈值,默认127即可 + */ + function drawLabelImage(imageData, x, y, width, height, rotate, imageProcessingType, imageProcessingValue,success,error){ + exec(success,error,"JcPrinter","action",["drawLabelImage",[imageData, x, y, width, height, rotate, imageProcessingType, imageProcessingValue]]); + } + + /** + * 绘制线条 + * 该方法用于在绘图板上绘制线条,包括位置、大小、旋转角度、线条类型等信息。 + * + * @param x 水平坐标x,单位:毫米 + * @param y 垂直坐标y,单位:毫米 + * @param width 宽度,单位:毫米 + * @param height 高度,单位:毫米 + * @param rotate 旋转角度,可选值:0、90、180、270 + * @param lineType 线条类型,1:实线,2:虚线类型(虚实比例1:1) + * @param dashWidth 线条为虚线时的宽度,包含实线段长度和空线段长度,单位:毫米 + */ + function drawLabelLine(x, y, width, height, rotate, lineType, dashWidth,success,error){ + exec(success,error,"JcPrinter","action",["drawLabelLine",[x, y, width, height, rotate, lineType, dashWidth]]); + } + + + function generateLabelJson(success,error){ + exec(success,error,"JcPrinter","generateLabelJson"); + } } \ No newline at end of file