服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - Java如何调用TSC打印机进行打印详解

Java如何调用TSC打印机进行打印详解

2021-05-16 16:59孤独患者的病态 Java教程

这篇文章主要给大家介绍了关于Java如何调用TSC打印机进行打印的相关资料,文中介绍了三种方法,分别是两种后台打印以及JS打印 ,三种方法都给出了详细的示例代码,需要的朋友可以参考借鉴,下面来一起看看吧

前言

最近项目中用到了打印机,最开始的完全不懂,现在弄好了,所以做了总结,该篇包括后台的调用打印(两种方式)跟前端的js的打印,但是只有ie现在支持打印,而且如果想远程连接打印机,二维码的生成和直接由打印机的命令进行操作,就要把修改浏览器的安全配置,下面再做详细的介绍

第一种后台打印:    

使用javax中的printservicelookup类进行打印,可以直接调用默认的打印机,也可以使用下列的方法进行筛选打印: 

?
1
printservicelookup.lookupmultidocprintservices(flavors, attributes);

可执行代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public static void main(string[] args) {
 fileinputstream textstream = null;
 try {
 textstream = new fileinputstream("地址");
 } catch (filenotfoundexception e) {
 e.printstacktrace();
 }
 if (textstream != null) // 当打印内容不为空时
 {
 // 指定打印输出格式
 docflavor flavor = docflavor.input_stream.pdf;//service_formatted.printable
 // 定位默认的打印服务
 printservice printservice = printservicelookup.lookupdefaultprintservice();
 // 创建打印作业
 docprintjob job = printservice.createprintjob();
 // 设置打印属性
 printrequestattributeset pras = new hashprintrequestattributeset();
 // 设置纸张大小,也可以新建mediasize类来自定义大小
 pras.add(mediasizename.iso_a4);
 docattributeset das = new hashdocattributeset();
 // 指定打印内容
 doc doc = new simpledoc(textstream, flavor, das);
 // 不显示打印对话框,直接进行打印工作
 try {
  job.print(doc, pras); // 进行每一页的具体打印操作
 } catch (printexception pe) {
  pe.printstacktrace();
 }
 } else {
 // 如果打印内容为空时,提示用户打印将取消
 joptionpane.showconfirmdialog(null,
  "sorry, printer job is empty, print cancelled!",
  "empty", joptionpane.default_option,
  joptionpane.warning_message);
 }
 }

第二种后台打印:

注意:第二种跟第三种打印使用的是打印机的命令进行操作,这里需要jna的jar包,还有jdk要求是32位的,并且要要注册对应的dll,对应不同的系统要在不同的windows下进行注册dll,注册成功之后需要win+r ,调用并运行  regsvr32.exe tscactivex.dll 指令

可执行代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class javademo {
 public interface tsclibdll extends library {
 tsclibdll instance = (tsclibdll) native.loadlibrary("tsclib", tsclibdll.class);
 int about();
 int openport(string pirntername);
 int closeport();
 int sendcommand(string printercommand);
 int setup(string width, string height, string speed, string density, string sensor, string vertical, string offset);
 int downloadpcx(string filename, string image_name);
 int barcode(string x, string y, string type, string height, string readable, string rotation, string narrow, string wide, string code);
 int printerfont(string x, string y, string fonttype, string rotation, string xmul, string ymul, string text);
 int clearbuffer();
 int printlabel(string set, string copy);
 int formfeed();
 int nobackfeed();
 int windowsfont(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, string szfacename, string content);
 }
 
 
 public static void main(string[] args) {
 system.setproperty("jna.encoding", "gbk");// 支持中文
 simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss");
 string time = df.format(new date());
 string qrcode = "pd102011";
 tsclibdll.instance.openport("tsc ttp-244 pro");//打印机型号
 tsclibdll.instance.setup("70","40","5","8","0","2","0");
 tsclibdll.instance.clearbuffer();
 string command = "qrcode 120,90,q,8,a,0,m1,s7,\"" + qrcode+"\""; //打印二维码的参数和内容
 tsclibdll.instance.sendcommand(command); //传送指令
 tsclibdll.instance.sendcommand("text 300 70 36 0 0 0 arial"+ "办公耗材-标签纸");
 //tsclibdll.instance.windowsfont(300, 70, 36, 0, 0, 0, "arial", "办公耗材-标签纸");
 tsclibdll.instance.printlabel("1", "1");
 tsclibdll.instance.closeport();
 }
}

注意:这种打印方式,需要usb连接(ttp-244 pro),如果想在线生成,远程连接,需要使用第三种方式

第三种js打印

在使用js进行打印的时候要主要浏览器的限制,目前只有ie浏览器进行支持,在进行打印时,控制台出现automation 服务器不能创建对象的,请调节浏览器的安全中的自定义级别中的activex中相关的设置为启用,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<script type="text/javascript">
 
function printqrcode(){
 
 var tscobj = new activexobject("tscactivex.tsclib");//打印机使用的dll
 //tscobj.activexabout(); // 打印机信息
 tscobj.activexopenport ("tsc ttp-244 pro");//打印机名称,不能写错
 //tscobj.activexdownloadpcx ("d:/myeclipseproject/filesmanager/trunk/doc/02-参考资料/03-二维码打印机/01-开发帮助/php调用范例/tscactivex.dll-php-example/","123.pcx");
 tscobj.activexsetup("70","40","4","15","0","2.5","0");//打印机设置
 //tscobj.activexsetup("标签宽度","标签高度","打印速度","打印浓度(0-15)","感应器类别字串型,0 表示使用垂直間距感測器(gap sensor), 1 表示使用黑標感測器(black mark senso)","gap/black mark垂直间距(mm)","gap/black mark偏移距离(mm)");
 //tscobj.activexformfeed();
 //tscobj.activexnobackfeed();
 tscobj.activexsendcommand("direction 1"); //设置标签方向 ,direction 1 左上角 (x,y)={0,0};direction 0 右下角 (x,y)={0,0};
 tscobj.activexsendcommand ("set tear on");
 tscobj.activexclearbuffer();
 //tscobj.activexsendcommand ("putpcx 0,0,\"123.pcx\"");
 /*
 1、activexprinterfont采用机器内置编码通常用来打英文。
 2、activexwindowsfont可以输出汉字,但是必须是系统中存在的字体。
 tscobj.activexprinterfont ("a","b","c","d","e","f","g");
 a:字符串,文字x方向起始点,以点表示。
 b:字符串,文字y方向起始点,以点表示。
 c:內建字型名称,共12种(1: 8*12 dots 2: 12*20 dots 3: 16*24 dots 4: 24*32 dots 5: 32*48 dots tst24.bf2: 繁體中文 24*24 tst16.bf2: 繁體中文 16*16 ttt24.bf2: 繁體中文 24*24 (電信碼) tss24.bf2: 簡體中文 24*24 tss16.bf2: 簡體中文 16*16 k: 韓文 24*24 l: 韓文 16*16 )
 d:字符串,旋转角度
 e:字符串,x方向放大倍率1-8
 f:字符串,y方向放大倍率1-8
 g:字符串,打印内容
 activexwindowsfont(a,b,c,d,e,f,g,h)
 说明:使用windows ttf字体打印文字。
 参数:
 a:整数类型,文字x方向起始点,以点表示。
 b:整数类型,文字y方向起始点,以点表示。
 c:整数类型,字体高度,以点表示。
 d:整数类型,旋转角度,逆时针方向旋转。0-旋转0°,90-旋转90°,180-旋转180°,270-旋转270°。
 e:整数类型,字体外形。0:标签;1:斜体;2:粗体;3:粗斜体。
 f:整数类型,下划线,0:无下划线;1:加下划线。
 g:字符串类型,字体名称。如:arial,times new roman。
 h:字符串类型,打印文字内容。
 */
// tscobj.activexwindowsfont (500, 200, 48, 90, 0, 0, "arial", "\u7f16\u7801");
 tscobj.activexwindowsfont (260, 60, 60, 0, 2, 0, "arial", "xx专用");
 
 //var cmd = 'qrcode 条码x方向起始点,条码y方向起始点,纠错级别,二维码高度,a(a和m),旋转角度,m2(分为类型1和类型2),s1 (s1-s8,默认s7),\"1231你好2421341325454353\"';
 var cmd = 'qrcode 80,80,h,7,a,0,m2,s1,\"'+"123456789"+'\"';
 
 tscobj.activexsendcommand(cmd);
 tscobj.activexwindowsfont (280, 150, 40, 0, 0, 0, "arial", "123456789");
 tscobj.activexwindowsfont (180, 260, 30, 0, 0, 0, "arial", "xxxxxxxx");
 tscobj.activexprintlabel ("1","1");//(打印份数,每页打印张数)
 tscobj.activexclearbuffer();//清除
 tscobj.activexcloseport();//关闭打印端口
}
</script>

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://www.cnblogs.com/gdhzdbh/p/9300771.html

延伸 · 阅读

精彩推荐