完成打印数据处理

This commit is contained in:
范大德 2023-10-09 19:48:45 +08:00
parent e0015cb8b8
commit ca8a203a99
2 changed files with 179 additions and 125 deletions

View File

@ -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<String> jsonList;
/**
* 图像处理数据
*/
private ArrayList<String> 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;i<methodJsonArgs.length();i++){
Object obj = methodJsonArgs.opt(i);
if(obj instanceof JSONArray){
Object[] array = new Object[((JSONArray) obj).length()];
for(int j=0;j<((JSONArray) obj).length();j++){
array[i] = ((JSONArray) obj).opt(j);
}
obj = array;
}
methodArgs[i] = obj;
}
for(Method m:this.methods){
if(m.getName() == methodName){
try {
m.invoke(Util.getInstance(),methodArgs);
callbackContext.success();
} catch (IllegalAccessException | InvocationTargetException e) {
callbackContext.error(e.getMessage());
}
return true;
}
}
case "generateLabelJson":
callbackContext.success(new String(Util.getInstance().generateLabelJson()));
return true;
}
return false;
}
@ -109,21 +130,22 @@ public class JcPrinter extends CordovaPlugin {
/**
* 打印标签
*
* @param pages 页数页表示数据不一样
* @param copies 份数
*/
private void print(JSONArray args, CallbackContext callbackContext) {
JSONArray data = args.optJSONArray(0);
JSONArray jsonInfo = args.optJSONArray(1);
List<String>info = new ArrayList<>();
for(int i=0;i<jsonInfo.length();i++){
info.add(jsonInfo.optString(i));
}
//重置错误状态变量
isError = false;
//重置取消打印状态变量
isCancel = false;
//清除数据
initPrintData();
//总打印份数
final int[] generatedPrintDataPageCount = {0};
pageCount = 1;
int pageCount = data.length();
quantity = 1;
int totalQuantity = pageCount * quantity;
//总打印份数表示所有页面的打印份数之和例如如果你有3页需要打印第一页打印3份第二页打印2份第三页打印5份那么count的值应为103+2+5
@ -282,28 +304,17 @@ public class JcPrinter extends CordovaPlugin {
if (pageIndex > 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;
List<String>pageData = new ArrayList<>();
JSONArray array= data.optJSONArray(pageIndex);
for(int i=0;i<array.length();i++){
pageData.add(array.optString(i));
}
Util.getInstance().commitData(pageData,info);
}
});
}
private void initPrintData() {
jsonList = new ArrayList<>();
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值分别应为325
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++;
}
}
}

View File

@ -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 旋转角度可选值090180270表示绘图板的旋转角度
* @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 旋转角度可选值090180270
* @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-CODE12821-UPC-A22-UPC-E23-EAN824-EAN1325-CODE9326-CODE3927-CODEBAR28-ITF25
* @param value 一维码内容
* @param fontSize 字号大小单位毫米
* @param rotate 旋转角度可选值090180270
* @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_CODE32-PDF41733-DATA_MATRIX34-AZTEC
* @param rotate 旋转角度可选值090180270
*/
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 旋转角度可选值090180270
* @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 旋转角度可选值090180270
* @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 旋转角度可选值090180270
* @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");
}
}