资源简介
将打印指令放到txt文档中,读取文档中的指令,发送到斑马打印机中,实现打印。此代码已经非常精简,项目需要用到ts24.lib,资源中也付带这个文件,并且附带一个C.txt文档的指令作为实验用。C.txt和ts24.lib请放在代码中的指定路径,打印前记得安装驱动哦,斑马的驱动资源中也附带哦!欢迎各位苦逼的程序猿下载哦!
代码片段和文件信息
package print;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
public class SimplePrint {
byte[] dotfont;
/**
* 读取指令txt文件
* @return
* @throws Exception
*/
public static String input() throws Exception {
File inputStream = new File(“e:/C.txt“);
InputStreamReader read = new InputStreamReader(new FileInputStream(
inputStream));
BufferedReader reader = new BufferedReader(read);
String string = ““;
StringBuffer buffer = new StringBuffer();
while ((string = reader.readLine()) != null) {
buffer.append(string);
}
read.close();
return buffer.toString();
}
public static void main(String[] args) {
try {
SimplePrint a = new SimplePrint();
String str = input();
a.print(str);//将打印指令发送到打印机
} catch (Exception e) {
e.printStackTrace();
}
}
public SimplePrint() throws Exception {
File file = new File(“c:/d/ts24.lib“);
FileInputStream fis = new FileInputStream(file);
dotfont = new byte[fis.available()];
fis.read(dotfont);
fis.close();
}
public void print(String str) throws PrintException {
PrintService psZebra = PrintServiceLookup.lookupDefaultPrintService();
if (psZebra == null) {
return;
}
DocPrintJob job = psZebra.createPrintJob();
byte[] by = str.getBytes();
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(by flavor null);
job.print(doc null);
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 6517208 2015-03-30 11:16 Zebra_4.6.67.03.exe
文件 62366 2015-04-15 14:31 C.txt
目录 0 2015-04-15 16:24 Print\
文件 301 2015-04-15 15:51 Print\.classpath
文件 381 2015-04-15 15:51 Print\.project
目录 0 2015-04-15 16:24 Print\.settings\
文件 629 2015-04-15 15:51 Print\.settings\org.eclipse.jdt.core.prefs
目录 0 2015-04-15 16:24 Print\bin\
目录 0 2015-04-15 16:24 Print\bin\print\
文件 2790 2015-04-15 15:57 Print\bin\print\SimplePrint.class
目录 0 2015-04-15 16:24 Print\src\
目录 0 2015-04-15 16:24 Print\src\print\
文件 1875 2015-04-15 15:57 Print\src\print\SimplePrint.java
文件 487296 2014-01-24 11:13 ts24.lib
- 上一篇:AssetStudio
- 下一篇:基于HTML5的网页视频播放器
评论
共有 条评论