Browse Source

横向设计

master
han\hanst 5 months ago
parent
commit
539d392212
  1. 39
      src/main/java/com/gaotao/modules/base/utils/CoordinateTransformer.java
  2. 11
      src/main/java/com/gaotao/modules/base/utils/ZplGenerator.java

39
src/main/java/com/gaotao/modules/base/utils/CoordinateTransformer.java

@ -10,23 +10,23 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
@Data @Data
public class CoordinateTransformer { public class CoordinateTransformer {
private String orientation; private String orientation;
private CanvasSize canvasSize; private CanvasSize canvasSize;
private TransformConfig config; private TransformConfig config;
public CoordinateTransformer(String orientation, CanvasSize canvasSize) { public CoordinateTransformer(String orientation, CanvasSize canvasSize) {
this.orientation = orientation != null ? orientation : "portrait"; this.orientation = orientation != null ? orientation : "portrait";
this.canvasSize = canvasSize; this.canvasSize = canvasSize;
this.config = getConfig(); this.config = getConfig();
} }
/** /**
* 获取转换配置 * 获取转换配置
*/ */
private TransformConfig getConfig() { private TransformConfig getConfig() {
TransformConfig config = new TransformConfig(); TransformConfig config = new TransformConfig();
if ("landscape".equals(this.orientation)) { if ("landscape".equals(this.orientation)) {
config.setScaleX(1.0); config.setScaleX(1.0);
config.setScaleY(1.0); config.setScaleY(1.0);
@ -39,10 +39,10 @@ public class CoordinateTransformer {
config.setOffsetX(0.0); config.setOffsetX(0.0);
config.setOffsetY(0.0); config.setOffsetY(0.0);
} }
return config; return config;
} }
/** /**
* 将画布坐标转换为ZPL坐标 * 将画布坐标转换为ZPL坐标
*/ */
@ -53,14 +53,15 @@ public class CoordinateTransformer {
(int) Math.round(canvasY * config.getScaleY()) (int) Math.round(canvasY * config.getScaleY())
); );
} else { } else {
int width = canvasSize.getWidth();
// 横向打印需要坐标旋转变换 // 横向打印需要坐标旋转变换
return new ZplCoordinate( return new ZplCoordinate(
(int) Math.round(canvasX * config.getScaleX()),
(int) Math.round(canvasY * config.getScaleY())
(int) Math.round(canvasY * config.getScaleX()),
(int) Math.round((width-canvasX-100) * config.getScaleY())
); );
} }
} }
/** /**
* 将ZPL坐标转换为画布坐标 * 将ZPL坐标转换为画布坐标
*/ */
@ -78,14 +79,14 @@ public class CoordinateTransformer {
); );
} }
} }
/** /**
* 验证坐标是否在画布范围内 * 验证坐标是否在画布范围内
*/ */
public boolean isInBounds(int x, int y) { public boolean isInBounds(int x, int y) {
return x >= 0 && x <= canvasSize.getWidth() && y >= 0 && y <= canvasSize.getHeight(); return x >= 0 && x <= canvasSize.getWidth() && y >= 0 && y <= canvasSize.getHeight();
} }
/** /**
* 将坐标限制在画布范围内 * 将坐标限制在画布范围内
*/ */
@ -95,7 +96,7 @@ public class CoordinateTransformer {
Math.max(0, Math.min(y, canvasSize.getHeight() - elementHeight)) Math.max(0, Math.min(y, canvasSize.getHeight() - elementHeight))
); );
} }
/** /**
* 画布尺寸 * 画布尺寸
*/ */
@ -105,7 +106,7 @@ public class CoordinateTransformer {
private int height; private int height;
private String name; private String name;
private String description; private String description;
public CanvasSize(int width, int height, String name, String description) { public CanvasSize(int width, int height, String name, String description) {
this.width = width; this.width = width;
this.height = height; this.height = height;
@ -113,7 +114,7 @@ public class CoordinateTransformer {
this.description = description; this.description = description;
} }
} }
/** /**
* 转换配置 * 转换配置
*/ */
@ -124,7 +125,7 @@ public class CoordinateTransformer {
private double offsetX; private double offsetX;
private double offsetY; private double offsetY;
} }
/** /**
* ZPL坐标 * ZPL坐标
*/ */
@ -132,13 +133,13 @@ public class CoordinateTransformer {
public static class ZplCoordinate { public static class ZplCoordinate {
private int x; private int x;
private int y; private int y;
public ZplCoordinate(int x, int y) { public ZplCoordinate(int x, int y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
} }
/** /**
* 画布坐标 * 画布坐标
*/ */
@ -146,10 +147,10 @@ public class CoordinateTransformer {
public static class CanvasCoordinate { public static class CanvasCoordinate {
private int x; private int x;
private int y; private int y;
public CanvasCoordinate(int x, int y) { public CanvasCoordinate(int x, int y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
} }
}
}

11
src/main/java/com/gaotao/modules/base/utils/ZplGenerator.java

@ -6,6 +6,7 @@ import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List; import java.util.List;
import com.google.zxing.*; import com.google.zxing.*;
@ -348,8 +349,12 @@ public class ZplGenerator {
boolean showContent = element.getShowContent() != null ? element.getShowContent() : true; boolean showContent = element.getShowContent() != null ? element.getShowContent() : true;
// 将毫米转换为ZPL单位 // 将毫米转换为ZPL单位
double widthMM = element.getWidth() != null ? element.getWidth().doubleValue() : 2.0;
int width = Math.max(1, Math.min(10, (int) Math.round(widthMM * 1.5)));
BigDecimal width = (element.getWidth() != null ? element.getWidth() : new BigDecimal("0.33"))
.multiply(BigDecimal.valueOf(this.dpi))
.divide(BigDecimal.valueOf(25.4), 2, RoundingMode.HALF_UP)
.setScale(0, RoundingMode.HALF_UP)
.min(BigDecimal.TEN);
// 高度毫米转换为点数 // 高度毫米转换为点数
int height = Math.max(1, Math.round((element.getHeight() != null ? element.getHeight() : 15) * this.dpi / 25.4f)); int height = Math.max(1, Math.round((element.getHeight() != null ? element.getHeight() : 15) * this.dpi / 25.4f));
@ -386,7 +391,7 @@ public class ZplGenerator {
String data = element.getData() != null ? element.getData() : ""; String data = element.getData() != null ? element.getData() : "";
return String.format("^FO%d,%d^BY%d%s,%d%s^FD%s^FS", return String.format("^FO%d,%d^BY%d%s,%d%s^FD%s^FS",
x, y, width, zplCommand, height, additionalParams, data);
x, y, width.setScale(3, RoundingMode.HALF_UP).intValue(), zplCommand, height, additionalParams, data);
} }
/** /**

Loading…
Cancel
Save