問題:
java如何驅(qū)動[url=http://www.chongshang.com.cn/barcodeprinter.shtml]條形碼打印機(jī)[/url]并能實現(xiàn)WEB打印?
如能提供例子,我重分追加
問題補(bǔ)充:一,二樓的可能沒搞懂我的意思,是條形碼專用打印機(jī),不是一般的打印機(jī),等待中...
[url=http://www.chongshang.com.cn/sh_barcodescanner.shtml]上海條碼掃描器[/url]新人解答:
參考 javax.print.*
大致過程是這樣的:
寫一個servlet,專門實現(xiàn)打印服務(wù)
[url=http://www.chongshang.com.cn/barcodeprinter.shtml]條碼打印機(jī)[/url]
[url=http://www.chongshang.com.cn/barcodeprinter.shtml]Zebra條碼打印機(jī)[/url]
[url=http://www.chongshang.com.cn/label_ribbon.shtml]標(biāo)簽[/url]
[url=http://www.chongshang.com.cn/label_ribbon.shtml]碳帶[/url]
[url=http://www.chongshang.com.cn/label_ribbon.shtml]不干膠標(biāo)簽[/url]
這個servlet內(nèi)是這樣處理的:
1.獲取打印機(jī)對象printer
通過枚舉 PrintService printservices[] = PrintServiceLookup.lookupPrintServices(...)
2.生成DocPrintJob job=PrintService.createPrintJob();
3.生成文檔 Doc doc = new SimpleDoc(...);
需要3個參數(shù)
第一個是打印的數(shù)據(jù),可以根據(jù)條碼,生成一幅圖像,然把圖像作為輸入流,作為第一個參數(shù)
第二個參數(shù)用一個 DocFlavor.INPUT_STREAM 對象
DocFlavor類有相應(yīng)的靜態(tài)變量,你根據(jù)圖像的mime類型選擇
第三個參數(shù)是一個DocAttributeSet對象
4.打印
job.print(doc, pras);
[url=http://www.chongshang.com.cn/]條碼[/url]
[url=http://www.chongshang.com.cn/]條形碼[/url]
[url=http://www.chongshang.com.cn/]條碼機(jī)[/url]
這是我以前寫的練習(xí):
-----------------------------------------------------------------------
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; i<printers.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);
//print
job.print(doc, null);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//Main method
public static void main(String[] args)
{
test("12345");
}
}