打印在 40mm x 15mm 尺寸标签上的条码 png 图像在打印且未被扫描时缩小为 20mm x10mm
Barcode png Image printed on a Label of 40mm x 15mm size is shrunk to 20mm x10mm when printed and not getting scanned
我在使用 code128 生成条形码并将其保存在 PNG 文件中时遇到了这个问题。
将相同的 PNG 文件提供给具有所需文档属性的打印机作业以在所需尺寸的标签上打印时,尺寸会缩小并且不会被扫描。
所提供打印件的实际尺寸 - 40 毫米 x 15 毫米。
标签上印刷的尺寸 - 20mm x 10mm
我正在将此属性添加到 PrintRequestAttributeSet -
pras.add(MediaSize.findMedia(40, 15, Size2DSyntax.MM));
但效果并不准确,我试着绕过那里的 x 和 y 参数值,打印的尺寸仍然在 25mm x 10mm 以内。
非常感谢任何打印所需尺寸条形码的输入。
我在下面给出了完整的代码细节。
(PS: 我正在使用 "Honeywell PC42t Plus" 热敏打印机打印,目前我的标签尺寸为 700mm x 280mm,我正在等待收到 40mm x 15mm 尺寸的实际标签。
所以这是为了测试,一旦收到实际标签,我就可以使用整个标签打印实际的 40mm x 15mm 条形码 space)
public class One_TestMyBarcode {
private static final String MIME_TYPE = "image/x-png";
private static final String DELIMTER = "-";
static String image_name = "NewBarcode_One.png";
public static void main(String[] args) {
FileInputStream textStream = null;
int lastSeqNo = 001;
String roCode= "ERO";
AtomicInteger seqNo = new AtomicInteger(lastSeqNo);
Code128Bean code128 = new Code128Bean();
code128.setHeight(15f);
//code128.setBarHeight(40f);
//code128.setModuleWidth(0.3);
code128.setModuleWidth(0.2);
code128.setQuietZone(10);
code128.doQuietZone(true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BitmapCanvasProvider canvas = new BitmapCanvasProvider(baos, MIME_TYPE, 300, BufferedImage.TYPE_BYTE_BINARY,
false, 0);
StringBuffer codeData = new StringBuffer();
codeData.append(roCode);
codeData.append(DELIMTER);
int currentSeqNo = seqNo.getAndIncrement();
String seq = String.format("%07d", currentSeqNo);
codeData.append(seq);
codeData.append(DELIMTER);
Calendar current = Calendar.getInstance();
String year = Integer.toString(current.get(Calendar.YEAR)).substring(2);
codeData.append(year);
//logger.debug("barcode dimension is ");
code128.calcDimensions(codeData.toString());
code128.generateBarcode(canvas, codeData.toString());
try {
canvas.finish();
} catch (IOException e) {
throw new RuntimeException(e);
}
FileOutputStream fos = null;
try {
//fos = new FileOutputStream("C:\Users\Vinayak\Desktop\barcode\" +image_name);
fos = new FileOutputStream(image_name);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
//textStream = new FileInputStream("C:\Users\Vinayak\Desktop\barcode\" +image_name);
textStream = new FileInputStream(image_name);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
// Position the default print service
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
// Create a print job
DocPrintJob job = printService.createPrintJob();
// Set the print properties
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//printed a very small size (20mm x 10mm) and does not scan
pras.add(MediaSize.findMedia(40, 15, Size2DSyntax.MM));
//DOESN'T Scan Either
//pras.add(OrientationRequested.LANDSCAPE);
//pras.add(OrientationRequested.PORTRAIT);
//pras.add(MediaSizeName.ISO_A10);
//Doesn't print at all
//pras.add(new MediaPrintableArea(0, 0, 40, 15, MediaPrintableArea.MM));
pras.add(new Copies(1));
DocAttributeSet das = new HashDocAttributeSet();
// Specify print content
Doc doc = new SimpleDoc(textStream, flavor, das);
// Do not display the print dialog, print directly
try {
System.err.println("Loop - print");
job.print(doc, pras); // Make specific print operations for each page
} catch (PrintException pe) {
pe.printStackTrace();
}
}
}
您可以使您的程序更短更快,并确保它始终打印 清晰 条码,方法是使用打印机语言而不是像使用条码打印机一样通用打印机。
import javax.print.*;
public class PrintUsingZPL {
public static void main(String[] args) {
StringBuilder codeData = new StringBuilder();
codeData.append("Whosebug"); // example
String printCommand = "^XA^LH0,0^FO50,50^BCN,100,Y,N,N^FD"+
codeData.toString()+
"^FS^XZ";
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
try {
DocPrintJob job = printService.createPrintJob();
Doc doc = new SimpleDoc(printCommand.getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
job.print(doc, null);
}
catch(PrintException e) { /* error handling goes here */ }
}
}
谢谢@Erich。我已经使用了您给定的代码并更新了 ZPL 和 DP 语言的 printCommand。您更新的代码 (^LH) 能够打印条形码,但它没有被扫描(不确定,我尝试了一些变体但没有帮助),所以我在那里尝试了 DPL 命令,如此处的代码所示,它能够打印条形码并成功扫描。非常感谢您的帮助和指导。
package BARCODE;
import javax.print.*;
import javax.print.PrintService;
public class PrintUsingZPL1 {
public static void main(String[] args) {
StringBuilder codeData = new StringBuilder();
//codeData.append("Whosebug"); // example
codeData.append("CRO-0000100-20"); // example
String printData = codeData.toString();
System.out.println("Data for Barcode " +printData);
/*String printCommand = "^XA^LH0,0^FO50,50^BCN,100,Y,N,N^FD"+
codeData.toString()+
"^FS^XZ";*/
/* String printCommand = "PP65,107:AN7\r\n" +
"BARSET \"CODE128B\",2,1,1,71\r\n" +
"PB \"CRO-0000001-20\"\r\n" +
"PP79,37:NASC 8\r\n" +
"FT \"CG Triumvirate Condensed Bold\",8,0,98\r\n" +
"PT \"CRO-0000001-20\"\r\n" +
"LAYOUT RUN \"\"\r\n" +
"PF"; */
String printCommand = "PP35,90:AN7\r\n" +
"BARSET \"CODE39A\",3,1,1,67\r\n" +
"PB " +codeData.toString()+ "\r\n" +
"PP65,23:NASC 9\r\n" +
"FT \"Andale Mono Bold\",8,0\r\n" +
"PT " +codeData.toString()+ "\r\n" +
"LAYOUT RUN \"\"\r\n" +
"PF";
/*String printCommand = "PP65,107:AN7\r\n" +
"BARSET \"CODE128B\",2,1,1,71\r\n" +
"PB "+printData+"\r\n" +
"PP79,37:NASC 8\r\n" +
"FT \"CG Triumvirate Condensed Bold\",8,0,98\r\n" +
"PT "+printData+"\r\n" +
"LAYOUT RUN \"\"\r\n" +
"PF";*/
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
try {
DocPrintJob job = printService.createPrintJob();
Doc doc = new SimpleDoc(printCommand.getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
job.print(doc, null);
}
catch(PrintException e) { /* error handling goes here */ }
}
}
我在使用 code128 生成条形码并将其保存在 PNG 文件中时遇到了这个问题。 将相同的 PNG 文件提供给具有所需文档属性的打印机作业以在所需尺寸的标签上打印时,尺寸会缩小并且不会被扫描。
所提供打印件的实际尺寸 - 40 毫米 x 15 毫米。 标签上印刷的尺寸 - 20mm x 10mm
我正在将此属性添加到 PrintRequestAttributeSet - pras.add(MediaSize.findMedia(40, 15, Size2DSyntax.MM));
但效果并不准确,我试着绕过那里的 x 和 y 参数值,打印的尺寸仍然在 25mm x 10mm 以内。
非常感谢任何打印所需尺寸条形码的输入。 我在下面给出了完整的代码细节。
(PS: 我正在使用 "Honeywell PC42t Plus" 热敏打印机打印,目前我的标签尺寸为 700mm x 280mm,我正在等待收到 40mm x 15mm 尺寸的实际标签。 所以这是为了测试,一旦收到实际标签,我就可以使用整个标签打印实际的 40mm x 15mm 条形码 space)
public class One_TestMyBarcode {
private static final String MIME_TYPE = "image/x-png";
private static final String DELIMTER = "-";
static String image_name = "NewBarcode_One.png";
public static void main(String[] args) {
FileInputStream textStream = null;
int lastSeqNo = 001;
String roCode= "ERO";
AtomicInteger seqNo = new AtomicInteger(lastSeqNo);
Code128Bean code128 = new Code128Bean();
code128.setHeight(15f);
//code128.setBarHeight(40f);
//code128.setModuleWidth(0.3);
code128.setModuleWidth(0.2);
code128.setQuietZone(10);
code128.doQuietZone(true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BitmapCanvasProvider canvas = new BitmapCanvasProvider(baos, MIME_TYPE, 300, BufferedImage.TYPE_BYTE_BINARY,
false, 0);
StringBuffer codeData = new StringBuffer();
codeData.append(roCode);
codeData.append(DELIMTER);
int currentSeqNo = seqNo.getAndIncrement();
String seq = String.format("%07d", currentSeqNo);
codeData.append(seq);
codeData.append(DELIMTER);
Calendar current = Calendar.getInstance();
String year = Integer.toString(current.get(Calendar.YEAR)).substring(2);
codeData.append(year);
//logger.debug("barcode dimension is ");
code128.calcDimensions(codeData.toString());
code128.generateBarcode(canvas, codeData.toString());
try {
canvas.finish();
} catch (IOException e) {
throw new RuntimeException(e);
}
FileOutputStream fos = null;
try {
//fos = new FileOutputStream("C:\Users\Vinayak\Desktop\barcode\" +image_name);
fos = new FileOutputStream(image_name);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
//textStream = new FileInputStream("C:\Users\Vinayak\Desktop\barcode\" +image_name);
textStream = new FileInputStream(image_name);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
// Position the default print service
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
// Create a print job
DocPrintJob job = printService.createPrintJob();
// Set the print properties
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//printed a very small size (20mm x 10mm) and does not scan
pras.add(MediaSize.findMedia(40, 15, Size2DSyntax.MM));
//DOESN'T Scan Either
//pras.add(OrientationRequested.LANDSCAPE);
//pras.add(OrientationRequested.PORTRAIT);
//pras.add(MediaSizeName.ISO_A10);
//Doesn't print at all
//pras.add(new MediaPrintableArea(0, 0, 40, 15, MediaPrintableArea.MM));
pras.add(new Copies(1));
DocAttributeSet das = new HashDocAttributeSet();
// Specify print content
Doc doc = new SimpleDoc(textStream, flavor, das);
// Do not display the print dialog, print directly
try {
System.err.println("Loop - print");
job.print(doc, pras); // Make specific print operations for each page
} catch (PrintException pe) {
pe.printStackTrace();
}
}
}
您可以使您的程序更短更快,并确保它始终打印 清晰 条码,方法是使用打印机语言而不是像使用条码打印机一样通用打印机。
import javax.print.*;
public class PrintUsingZPL {
public static void main(String[] args) {
StringBuilder codeData = new StringBuilder();
codeData.append("Whosebug"); // example
String printCommand = "^XA^LH0,0^FO50,50^BCN,100,Y,N,N^FD"+
codeData.toString()+
"^FS^XZ";
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
try {
DocPrintJob job = printService.createPrintJob();
Doc doc = new SimpleDoc(printCommand.getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
job.print(doc, null);
}
catch(PrintException e) { /* error handling goes here */ }
}
}
谢谢@Erich。我已经使用了您给定的代码并更新了 ZPL 和 DP 语言的 printCommand。您更新的代码 (^LH) 能够打印条形码,但它没有被扫描(不确定,我尝试了一些变体但没有帮助),所以我在那里尝试了 DPL 命令,如此处的代码所示,它能够打印条形码并成功扫描。非常感谢您的帮助和指导。
package BARCODE;
import javax.print.*;
import javax.print.PrintService;
public class PrintUsingZPL1 {
public static void main(String[] args) {
StringBuilder codeData = new StringBuilder();
//codeData.append("Whosebug"); // example
codeData.append("CRO-0000100-20"); // example
String printData = codeData.toString();
System.out.println("Data for Barcode " +printData);
/*String printCommand = "^XA^LH0,0^FO50,50^BCN,100,Y,N,N^FD"+
codeData.toString()+
"^FS^XZ";*/
/* String printCommand = "PP65,107:AN7\r\n" +
"BARSET \"CODE128B\",2,1,1,71\r\n" +
"PB \"CRO-0000001-20\"\r\n" +
"PP79,37:NASC 8\r\n" +
"FT \"CG Triumvirate Condensed Bold\",8,0,98\r\n" +
"PT \"CRO-0000001-20\"\r\n" +
"LAYOUT RUN \"\"\r\n" +
"PF"; */
String printCommand = "PP35,90:AN7\r\n" +
"BARSET \"CODE39A\",3,1,1,67\r\n" +
"PB " +codeData.toString()+ "\r\n" +
"PP65,23:NASC 9\r\n" +
"FT \"Andale Mono Bold\",8,0\r\n" +
"PT " +codeData.toString()+ "\r\n" +
"LAYOUT RUN \"\"\r\n" +
"PF";
/*String printCommand = "PP65,107:AN7\r\n" +
"BARSET \"CODE128B\",2,1,1,71\r\n" +
"PB "+printData+"\r\n" +
"PP79,37:NASC 8\r\n" +
"FT \"CG Triumvirate Condensed Bold\",8,0,98\r\n" +
"PT "+printData+"\r\n" +
"LAYOUT RUN \"\"\r\n" +
"PF";*/
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
try {
DocPrintJob job = printService.createPrintJob();
Doc doc = new SimpleDoc(printCommand.getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
job.print(doc, null);
}
catch(PrintException e) { /* error handling goes here */ }
}
}