|
|
|
@ -1,10 +1,7 @@
|
|
|
|
|
package com.utopiaindustries.service;
|
|
|
|
|
|
|
|
|
|
import com.google.zxing.BarcodeFormat;
|
|
|
|
|
import com.utopiaindustries.dao.ctp.BundleDAO;
|
|
|
|
|
import com.utopiaindustries.dao.ctp.FinishedItemDAO;
|
|
|
|
|
import com.utopiaindustries.dao.ctp.MasterBundleDAO;
|
|
|
|
|
import com.utopiaindustries.dao.ctp.StitchingOfflineItemDAO;
|
|
|
|
|
import com.utopiaindustries.dao.ctp.*;
|
|
|
|
|
import com.utopiaindustries.model.ctp.*;
|
|
|
|
|
import com.utopiaindustries.util.BarcodeUtils;
|
|
|
|
|
import com.utopiaindustries.util.StringUtils;
|
|
|
|
@ -19,11 +16,23 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.Font;
|
|
|
|
|
import java.awt.geom.AffineTransform;
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
|
|
import java.nio.file.FileSystems;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import com.google.zxing.EncodeHintType;
|
|
|
|
|
import com.google.zxing.qrcode.QRCodeWriter;
|
|
|
|
|
import com.google.zxing.WriterException;
|
|
|
|
|
import com.google.zxing.common.BitMatrix;
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class BarcodeService {
|
|
|
|
@ -35,12 +44,14 @@ public class BarcodeService {
|
|
|
|
|
private int port;
|
|
|
|
|
|
|
|
|
|
private final BundleDAO bundleDAO;
|
|
|
|
|
private final JobCardDAO jobCardDAO;
|
|
|
|
|
private final MasterBundleDAO masterBundleDAO;
|
|
|
|
|
private final FinishedItemDAO finishedItemDAO;
|
|
|
|
|
private final StitchingOfflineItemDAO stitchingOfflineItemDAO;
|
|
|
|
|
|
|
|
|
|
public BarcodeService(BundleDAO bundleDAO, MasterBundleDAO masterBundleDAO, FinishedItemDAO finishedItemDAO, StitchingOfflineItemDAO stitchingOfflineItemDAO) {
|
|
|
|
|
public BarcodeService(BundleDAO bundleDAO, JobCardDAO jobCardDAO, MasterBundleDAO masterBundleDAO, FinishedItemDAO finishedItemDAO, StitchingOfflineItemDAO stitchingOfflineItemDAO) {
|
|
|
|
|
this.bundleDAO = bundleDAO;
|
|
|
|
|
this.jobCardDAO = jobCardDAO;
|
|
|
|
|
this.masterBundleDAO = masterBundleDAO;
|
|
|
|
|
this.finishedItemDAO = finishedItemDAO;
|
|
|
|
|
this.stitchingOfflineItemDAO = stitchingOfflineItemDAO;
|
|
|
|
@ -56,14 +67,16 @@ public class BarcodeService {
|
|
|
|
|
List<? extends InventoryArtifact> list = new ArrayList<>();
|
|
|
|
|
if (StringUtils.compare(artifactType, Bundle.class.getSimpleName())) {
|
|
|
|
|
list = bundleDAO.findByIds(ids);
|
|
|
|
|
getBarcodeImages(list, stickerSize, artifactType);
|
|
|
|
|
} else if (StringUtils.compare(artifactType, MasterBundle.class.getSimpleName())) {
|
|
|
|
|
list = masterBundleDAO.findByIds(ids);
|
|
|
|
|
getBarcodeImages(list, stickerSize, artifactType);
|
|
|
|
|
} else if (StringUtils.compare(artifactType, FinishedItem.class.getSimpleName())) {
|
|
|
|
|
String sizeForStitchItem = BarcodeStickerSize.SIZE_1_X_2.name();
|
|
|
|
|
BarcodeStickerSize stickerSizeForStitchItem = BarcodeStickerSize.getSize(sizeForStitchItem);
|
|
|
|
|
list = stitchingOfflineItemDAO.findByIds(ids);
|
|
|
|
|
getBarcodeImagesForStitchItems(list, stickerSizeForStitchItem, artifactType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getBarcodeImages(list, stickerSize, artifactType);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void getBarcodeImages(List<? extends InventoryArtifact> artifacts,
|
|
|
|
@ -129,6 +142,138 @@ public class BarcodeService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void getBarcodeImagesForStitchItems(List<? extends InventoryArtifact> artifacts,
|
|
|
|
|
BarcodeStickerSize stickerSize,
|
|
|
|
|
String artifactType) throws Exception {
|
|
|
|
|
|
|
|
|
|
for (InventoryArtifact artifact : artifacts) {
|
|
|
|
|
JobCard jobCard = jobCardDAO.find(artifact.getJobCardId());
|
|
|
|
|
List<Bundle> bundles = bundleDAO.findByItemIdAndCardId( artifact.getItemId( ), jobCard.getId( ));
|
|
|
|
|
String concatenatedIds = bundles.stream()
|
|
|
|
|
.map(bundle -> String.valueOf(bundle.getId()))
|
|
|
|
|
.collect(Collectors.joining("\\"));
|
|
|
|
|
// Set Portrait Mode Dimensions
|
|
|
|
|
int stickerWidth = (int) stickerSize.getWidth();
|
|
|
|
|
int stickerHeight = (int) stickerSize.getHeight();
|
|
|
|
|
|
|
|
|
|
BufferedImage stickerImage = new BufferedImage(stickerWidth * 2, stickerHeight * 2, BufferedImage.TYPE_INT_ARGB);
|
|
|
|
|
Graphics2D g2d = stickerImage.createGraphics();
|
|
|
|
|
g2d.scale(2, 2);
|
|
|
|
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
|
|
|
|
|
|
|
g2d.setColor(Color.WHITE);
|
|
|
|
|
g2d.fillRect(0, 0, stickerWidth, stickerHeight);
|
|
|
|
|
|
|
|
|
|
// Draw Stitch Line (Dotted Line at the Top)
|
|
|
|
|
String stitchLine = "|";
|
|
|
|
|
Font fontLine = new Font("Helvetica", Font.BOLD, stickerSize.getTextSize()-2);
|
|
|
|
|
g2d.setFont(fontLine);
|
|
|
|
|
FontMetrics fontMetricsLine = g2d.getFontMetrics();
|
|
|
|
|
int textWidthLine = fontMetricsLine.stringWidth(stitchLine);
|
|
|
|
|
int centerXLine = stickerSize.getMarginLeft();
|
|
|
|
|
g2d.setColor(Color.BLACK);
|
|
|
|
|
for(int i=0;i<16;i++){
|
|
|
|
|
g2d.drawString(stitchLine, centerXLine,5+(i*3));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Draw ID Below Stitch Line
|
|
|
|
|
String id = String.valueOf(artifact.getId());
|
|
|
|
|
g2d.setFont(new Font("Helvetica", Font.BOLD, stickerSize.getTextSize()));
|
|
|
|
|
FontMetrics fontMetricsId = g2d.getFontMetrics();
|
|
|
|
|
int textHeightId = fontMetricsId.getHeight();
|
|
|
|
|
int centerXID = stickerSize.getMarginLeft()+7;
|
|
|
|
|
int centerYID = (stickerHeight / 2) + (textHeightId / 4);
|
|
|
|
|
AffineTransform originalTransform = g2d.getTransform();
|
|
|
|
|
g2d.rotate(Math.toRadians(-90), centerXID, centerYID);
|
|
|
|
|
g2d.drawString(id, centerXID, centerYID);
|
|
|
|
|
g2d.setTransform(originalTransform);
|
|
|
|
|
|
|
|
|
|
// Draw QR Code Below the ID
|
|
|
|
|
byte[] imgBytes = generateQRCodeImageByteArray(artifact.getBarcode(), stickerSize.getImageWidthBarcode(), stickerSize.getImageHeightBarcode());
|
|
|
|
|
BufferedImage qrCodeImage = ImageIO.read(new ByteArrayInputStream(imgBytes));
|
|
|
|
|
|
|
|
|
|
int centerXQR = stickerSize.getMarginLeft()-12;
|
|
|
|
|
int centerYQR = (stickerHeight / 2) + (textHeightId / 2)+1;
|
|
|
|
|
AffineTransform originalTransformQR = g2d.getTransform();
|
|
|
|
|
g2d.rotate(Math.toRadians(-90), centerXID, centerYID);
|
|
|
|
|
g2d.drawImage(qrCodeImage, centerXQR, centerYQR, null);
|
|
|
|
|
g2d.setTransform(originalTransformQR);
|
|
|
|
|
|
|
|
|
|
// Prepare Text Lines Below QR Code
|
|
|
|
|
String sku = artifact.getSku();
|
|
|
|
|
String jobCardCode = jobCard.getCode();
|
|
|
|
|
String combinedText = sku + " \\" + jobCardCode + concatenatedIds;
|
|
|
|
|
|
|
|
|
|
int maxLength = 16; // Max characters per line
|
|
|
|
|
List<String> lines = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
|
int start = i * maxLength;
|
|
|
|
|
if (start < combinedText.length()) {
|
|
|
|
|
String part = combinedText.substring(start, Math.min(start + maxLength, combinedText.length())).trim();
|
|
|
|
|
lines.add(part);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
g2d.rotate(Math.toRadians(-90), stickerWidth / 2, stickerHeight / 2);
|
|
|
|
|
|
|
|
|
|
// Draw Rotated Text Below QR Code
|
|
|
|
|
g2d.setColor(Color.BLACK);
|
|
|
|
|
g2d.setFont(new Font("Helvetica", Font.BOLD, stickerSize.getTextSize()));
|
|
|
|
|
FontMetrics fontMetrics = g2d.getFontMetrics();
|
|
|
|
|
int lineHeight = fontMetrics.getHeight();
|
|
|
|
|
int centerXSKU = stickerSize.getMarginLeft()+4;
|
|
|
|
|
int centerYSKU = (stickerHeight / 2) + (textHeightId / 4)+20;
|
|
|
|
|
|
|
|
|
|
AffineTransform originalTransformSKu = g2d.getTransform();
|
|
|
|
|
g2d.rotate(Math.toRadians(-0.1), centerXSKU, centerYSKU);
|
|
|
|
|
for (int i = 0; i < lines.size(); i++) {
|
|
|
|
|
int lineWidth = fontMetrics.stringWidth(lines.get(i));
|
|
|
|
|
g2d.drawString(lines.get(i), (stickerWidth - lineWidth) / 2, centerYSKU + (i * lineHeight));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g2d.setTransform(originalTransformSKu);
|
|
|
|
|
|
|
|
|
|
g2d.dispose();
|
|
|
|
|
|
|
|
|
|
// Save Image
|
|
|
|
|
Path path = FileSystems.getDefault().getPath("./src/main/resources/static/sample_qr.png");
|
|
|
|
|
File outputFile = path.toFile();
|
|
|
|
|
ImageIO.write(stickerImage, "PNG", outputFile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] generateQRCodeImageByteArray(String data, int width, int height) {
|
|
|
|
|
try {
|
|
|
|
|
QRCodeWriter qrCodeWriter = new QRCodeWriter();
|
|
|
|
|
|
|
|
|
|
// QR Code generation settings
|
|
|
|
|
Map<EncodeHintType, Object> hintMap = new HashMap<>();
|
|
|
|
|
hintMap.put(EncodeHintType.MARGIN, 1); // Optional: control margin size
|
|
|
|
|
|
|
|
|
|
// Generate QR code matrix
|
|
|
|
|
BitMatrix bitMatrix = qrCodeWriter.encode(data, BarcodeFormat.QR_CODE, width, height, hintMap);
|
|
|
|
|
|
|
|
|
|
// Convert BitMatrix to BufferedImage
|
|
|
|
|
BufferedImage qrImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
|
|
qrImage.createGraphics().fillRect(0, 0, width, height);
|
|
|
|
|
|
|
|
|
|
for (int x = 0; x < width; x++) {
|
|
|
|
|
for (int y = 0; y < height; y++) {
|
|
|
|
|
qrImage.setRGB(x, y, bitMatrix.get(x, y) ? 0x000000 : 0xFFFFFF); // Black and white
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
|
ImageIO.write(qrImage, "PNG", baos);
|
|
|
|
|
return baos.toByteArray();
|
|
|
|
|
} catch (WriterException | IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return new byte[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void printLabel( BufferedImage bufferedImage ) throws Exception {
|
|
|
|
|
Connection connection = new TcpConnection( ipAddress, port );
|
|
|
|
|
connection.open();
|
|
|
|
|