本文目录一览:
java中的条码打印怎样实现
编程控制条码打印机的有三种方式:1,直接使用条码打印机的驱动程序,把它当成普通打印机使用。条码可以使用条码字体或图形方式打樱2,使用条码打印机语言(脚本)直接送到打印机的端口,如并口,网口或usb口。
求用java代码控制斑马条码打印机打印二维码商标的例子程序
可以参考下.自己写的一个例子
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.Queue;
import com.google.zxing.WriterException;
public class QrPrint implements Printable {
public void print() throws PrinterException {
PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat pf = pj.defaultPage();
Paper paper = new Paper();
double margin = 0.1; // half inch
paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2,paper.getHeight() - margin * 2);
pf.setPaper(paper);
pj.setPrintable(this, pf);
pj.print();
}
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if ("判断如果打印完了就退出") {
return Printable.NO_SUCH_PAGE;
}
try {
Image eanImage = generateEANBufferedImage(content,120,20);
Graphics2D g2 = (Graphics2D) graphics;
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); //设置可成像的X和Y轴的距离
g2.drawImage(eanImage, 0, 80, null); //设置打印的边距
g2.drawString("字符串:",130,20);
} catch (WriterException e) {
e.printStackTrace();
return Printable.NO_SUCH_PAGE;
}
return Printable.PAGE_EXISTS;
}
private BufferedImage generateBufferedImage(String content,int width,int height) throws WriterException {
HashtableEncodeHintType,String hints = new HashtableEncodeHintType,String();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.EAN_13, width, height, hints);
return MatrixToImageWriter.toBufferedImage(bitMatrix);
}
}
java如何驱动条形码打印机并能实现WEB打印?
参考 javax.print.*
大致过程是这样的:
写一个servlet,专门实现打印服务
这个servlet内是这样处理的:
1.获取打印机对象printer
通过枚举 PrintService printservices[] = PrintServiceLookup.lookupPrintServices(...)
2.生成DocPrintJob job=PrintService.createPrintJob();
3.生成文档 Doc doc = new SimpleDoc(...);
需要3个参数
第一个是打印的数据,可以根据条码,生成一幅图像,然把图像作为输入流,作为第一个参数
第二个参数用一个 DocFlavor.INPUT_STREAM 对象
DocFlavor类有相应的静态变量,你根据图像的mime类型选择
第三个参数是一个DocAttributeSet对象
4.打印
job.print(doc, pras);
这是我以前写的练习:
-----------------------------------------------------------------------
package printtest;
import java.io.*;
import javax.print.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class Application1
{
public static void test(String code)
{
try{
DocFlavor flavor=DocFlavor.INPUT_STREAM.JPEG;
//get a printer
PrintService[] printers=PrintServiceLookup.lookupPrintServices( flavor, null);
for( int i=0; iprinters.length; i++ ) System.out.println( printers[i].getName());
PrintService printer=printers[0];
//job
DocPrintJob job=printer.createPrintJob();
//document
BufferedImage img=new BufferedImage( 400,300, BufferedImage.TYPE_USHORT_555_RGB );
Graphics g=img.getGraphics();
g.drawString(code, 100,100);
ByteArrayOutputStream outstream=new ByteArrayOutputStream();
ImageIO.write( img, "jpg", outstream);
byte[] buf=outstream.toByteArray();
InputStream stream=new ByteArrayInputStream(buf);
Doc doc=new SimpleDoc(stream,flavor,null);
job.print(doc, null);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//Main method
public static void main(String[] args)
{
test("12345");
}
}