I am facing this issue where I am generating a barcode using code128 and saving it in a PNG file. The same PNG file when supplied to a printer Job with required Document properties for printing on a Label of required size, the size gets reduced and does not get scanned.
Actual Size for print supplied - 40mm x 15mm. Size printed on the label - 20mm x 10mm
I am adding this attribute to PrintRequestAttributeSet - pras.add(MediaSize.findMedia(40, 15, Size2DSyntax.MM));
But it is not effected accurately, I tried to play around the x and y parameter value there, still, the size printed falls within 25mm x 10mm.
Any inputs to print the barcode of the required size is highly appreciated. I have given the complete code details below.
(PS: I am using "Honeywell PC42t Plus" Thermal Printer to print and currently my labels are of 700mm x 280mm in size, I am waiting to receive the actual labels of 40mm x 15mm size. So this is to test that, I can print an actual 40mm x 15mm barcode utilizing whole label space once I have the actual labels received)
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();
}
}
}