81,087
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args)
{
try
{
DocFlavor dof = DocFlavor.INPUT_STREAM.GIF;
Barcode b = BarcodeFactory.createCode128("HOS18051100012222");
BufferedImage bi = BarcodeImageHandler.getImage(b);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bi, "gif", os);
InputStream is = new ByteArrayInputStream(os.toByteArray());
PrintService ps = PrintServiceLookup.lookupDefaultPrintService();
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(OrientationRequested.PORTRAIT);
pras.add(new Copies(1));
pras.add(PrintQuality.HIGH);
DocAttributeSet das = new HashDocAttributeSet();
// 设置打印纸张的大小(以毫米为单位)
das.add(new MediaPrintableArea(0, 0, 46, 30, MediaPrintableArea.MM));
Doc doc = new SimpleDoc(is, dof, das);
DocPrintJob job = ps.createPrintJob();
job.print(doc, pras);
is.close();
//PrinterJob job = PrinterJob.getPrinterJob();
//job.setPrintable(b);
//if (job.printDialog())
//{
// job.print();
//}
}
catch (Exception e)
{
e.printStackTrace();
}
}