首先这是网络工具类,通过socket实现,多说一句,网络端口号一般默认的是9100
package com.ieasy.tool; import android.annotation.suppresslint;
import java.io.ioexception ;
import java.io.outputstream ;
import java.io.printwriter ;
import java.net.inetsocketaddress ;
import java.net.socket ;
import java.net.socketaddress ;
import java.nio.charset.charset ; @suppresslint("newapi")
public class netprinter
{
public socket socket; public int pos_open_netport = 9100;// 0x238c
public boolean ifopen = false;
public int net_sendtimeout = 1000;
public int net_receivetimeout = 1500;
public string command = ""; //打印命令字符串
public byte[] outbytes; //传输的命令集 printercmd pcmd = new printercmd(); ///
/// 网络打印机 打开网络打印机
///
///
///
public boolean open(string ipaddress, int netport)
{
if (socket == null)
{
try
{
socketaddress ipe = new inetsocketaddress(ipaddress,netport);
socket = new socket(); //socket(ipaddress, netport,true);
socket.connect(ipe);
socket.setsotimeout(net_receivetimeout);
//socket.sendtimeout = net_sendtimeout;
ifopen = true;
//system.out.print("连接成功");
}
catch(exception e)
{
//messagebox.show("连接不成功");
e.printstacktrace();
ifopen = false;
}
}
else
{
try
{
socket.close();
socketaddress ipe = new inetsocketaddress(ipaddress,netport);
socket = new socket(); //socket(ipaddress, netport,true);
socket.connect(ipe);
socket.setsotimeout(net_receivetimeout);
//socket.sendtimeout = net_sendtimeout;
ifopen = true;
}
catch(exception e)
{
e.printstacktrace();
ifopen = false;
}
}
return ifopen;
} ///
/// 网络打印机 关闭
///
///
///
public void close()
{
try
{
socket.close();
socket = null;
}
catch(exception e)
{
e.printstacktrace();
}
} ///
/// 网络打印机 初始化打印机
///
public void set()
{
try
{
command = pcmd.cmd_setpos();
outputstream stream = socket.getoutputstream();
outbytes = command.getbytes(charset.forname("ascii"));
stream.write(outbytes);
}
catch (ioexception e)
{
// todo auto-generated catch block
e.printstacktrace();
}
} ///
/// 网络打印机 打印的文本
///
///
/// 0:居左 1:居中 2:居右
/// 字体大小0:正常大小 1:两倍高 2:两倍宽 3:两倍大小 4:三倍高 5:三倍宽 6:三倍大小 7:四倍高 8:四倍宽 9:四倍大小 10:五倍高 11:五倍宽 12:五倍大小
/// 0:非针打 1:针打
public void printtext(string pszstring, int nfontalign, int nfontsize,int ifzhenda)
{
try
{
outputstream stream = socket.getoutputstream();
command = pcmd.cmd_textalign(nfontalign);
outbytes = command.getbytes(charset.forname("ascii"));
stream.write(outbytes); if (ifzhenda == 1)
{
command = pcmd.cmd_fontsize_btp_m280(nfontsize);
outbytes = command.getbytes(charset.forname("ascii"));
stream.write(outbytes); command = pcmd.cmd_fontsize_btp_m2801(nfontsize);
outbytes = command.getbytes(charset.forname("ascii"));
stream.write(outbytes);
}
else
{
command = pcmd.cmd_fontsize(nfontsize);
outbytes = command.getbytes(charset.forname("ascii"));
stream.write(outbytes);
} command = pszstring;// cmd_enter();
outbytes = command.getbytes(charset.forname("gb2312")); //charset.defaultcharset()); //forname("utf-8")
stream.write(outbytes);
}
catch (ioexception e)
{
// todo auto-generated catch block
e.printstacktrace();
return;
} } ///
/// 网络打印机 回车
///
public void printenter()
{
try
{
command = pcmd.cmd_enter();
outputstream stream = socket.getoutputstream();
outbytes = command.getbytes(charset.forname("ascii"));
stream.write(outbytes);
}
catch (ioexception e)
{
// todo auto-generated catch block
e.printstacktrace();
}
} ///
/// 网络打印机 切割
///
/// 切割时,走纸行数
public void cutpage(int pagenum)
{
try
{
outputstream stream = socket.getoutputstream(); command = pcmd.cmd_pagego(pagenum) pcmd.cmd_enter();
outbytes = command.getbytes(charset.forname("ascii"));
stream.write(outbytes); command = pcmd.cmd_cutpage() pcmd.cmd_enter();
outbytes = command.getbytes(charset.forname("ascii"));
stream.write(outbytes); }
catch (ioexception e)
{
// todo auto-generated catch block
e.printstacktrace();
}
} ///
/// 网络打印机换行
///
///
public void printnewlines(int lines)
{
try
{
outputstream stream = socket.getoutputstream(); command = pcmd.cmd_fontsize(0);
outbytes = command.getbytes(charset.forname("ascii"));
stream.write(outbytes);
for (int i = 0; i < lines; i )
{
command = "" pcmd.cmd_enter();
outbytes = command.getbytes(charset.forname("ascii"));
stream.write(outbytes);
}
}
catch (ioexception e)
{
// todo auto-generated catch block
e.printstacktrace();
}
} ///
/// 打开钱箱
///
public void openqianxiang()
{
try
{
outputstream stream = socket.getoutputstream();
command = "" pcmd.cmc_qianxiang();
outbytes = command.getbytes(charset.forname("ascii"));
stream.write(outbytes);
}
catch (ioexception e)
{
// todo auto-generated catch block
e.printstacktrace();
}
} ///
/// 打印条码
///
///
///
///
/// 1:上方 2:下方 0:不打印数字
/// 0:居左 1:居中 2:居右
/// 字体大小0:正常大小 1:两倍高 2:两倍宽 3:两倍大小 4:三倍高 5:三倍宽 6:三倍大小 7:四倍高 8:四倍宽 9:四倍大小 10:五倍高 11:五倍宽 12:五倍大小
public void printtiaoma(string numstr, int height, int width, int numweizi, int fontalign, int fontsize)
{
try
{
outputstream stream = socket.getoutputstream();
command = pcmd.cmd_tiaomaheight(height);
outbytes = command.getbytes(charset.forname("ascii"));
stream.write(outbytes); command = pcmd.cmd_tiaomawidth(width);
outbytes = command.getbytes(charset.forname("ascii"));
stream.write(outbytes); command = pcmd.cmd_tiaomaweizi(numweizi);
outbytes = command.getbytes(charset.forname("ascii"));
stream.write(outbytes); command = pcmd.cmd_textalign(fontalign);
outbytes = command.getbytes(charset.forname("ascii"));
stream.write(outbytes); command = pcmd.cmd_fontsize(fontsize);
outbytes = command.getbytes(charset.forname("ascii"));
stream.write(outbytes); command = pcmd.cmd_tiaomaprint(numstr);
outbytes = command.getbytes(charset.forname("ascii"));
stream.write(outbytes);
}
catch (ioexception e)
{
// todo auto-generated catch block
e.printstacktrace();
}
}
} class printercmd
{
///
/// 初始化打印机
///
///
public string cmd_setpos()
{
return new stringbuffer().append((char)27).append((char)64).tostring();
} ///
/// 换行(回车)
///
///
public string cmd_enter()
{
return new stringbuffer().append((char)10).tostring();
} ///
/// 对齐模式
///
/// 0:左对齐 1:中对齐 2:右对齐
///
public string cmd_textalign(int align)
{
return new stringbuffer().append((char)27).append((char)97).append((char)align).tostring();
} ///
/// 字体的大小
///
/// 0:正常大小 1:两倍高 2:两倍宽 3:两倍大小 4:三倍高 5:三倍宽 6:三倍大小 7:四倍高 8:四倍宽 9:四倍大小 10:五倍高 11:五倍宽 12:五倍大小
///
public string cmd_fontsize(int nfontsize)
{
string _cmdstr = ""; //设置字体大小
switch (nfontsize)
{
case -1:
_cmdstr = new stringbuffer().append((char)29).append((char)33).append((char)0).tostring();//29 33
break; case 0:
_cmdstr = new stringbuffer().append((char)29).append((char)33).append((char)0).tostring();//29 33
break; case 1:
_cmdstr = new stringbuffer().append((char)29).append((char)33).append((char)1).tostring();
break; case 2:
_cmdstr = new stringbuffer().append((char)29).append((char)33).append((char)16).tostring();
break; case 3:
_cmdstr = new stringbuffer().append((char)29).append((char)33).append((char)17).tostring();
break; case 4:
_cmdstr = new stringbuffer().append((char)29).append((char)33).append((char)2).tostring();
break; case 5:
_cmdstr = new stringbuffer().append((char)29).append((char)33).append((char)32).tostring();
break; case 6:
_cmdstr = new stringbuffer().append((char)29).append((char)33).append((char)34).tostring();
break; case 7:
_cmdstr = new stringbuffer().append((char)29).append((char)33).append((char)3).tostring();
break; case 8:
_cmdstr = new stringbuffer().append((char)29).append((char)33).append((char)48).tostring();
break; case 9:
_cmdstr = new stringbuffer().append((char)29).append((char)33).append((char)51).tostring();
break; case 10:
_cmdstr = new stringbuffer().append((char)29).append((char)33).append((char)4).tostring();
break; case 11:
_cmdstr = new stringbuffer().append((char)29).append((char)33).append((char)64).tostring();
break; case 12:
_cmdstr = new stringbuffer().append((char)29).append((char)33).append((char)68).tostring();
break; }
return _cmdstr;
} ///
/// btp-m280(针打) 倍宽倍高
///
/// 0:取消倍宽倍高模式 1:倍高模式 2:倍宽模式 3:两倍大小
///
public string cmd_fontsize_btp_m280(int size)
{
string _cmdstr = "";
//只有0和1两种模式
int fontsize = size; switch (fontsize)
{
case 1:
_cmdstr = new stringbuffer().append((char)28).append((char)33).append((char)8).tostring();
break;
case 2:
_cmdstr = new stringbuffer().append((char)28).append((char)33).append((char)4).tostring();
break;
case 3:
_cmdstr = new stringbuffer().append((char)28).append((char)87).append((char)1).tostring();
break;
default:
_cmdstr = new stringbuffer().append((char)28).append((char)87).append((char)0).tostring();
break;
} return _cmdstr;
} ///
/// btp-m280(针打) 倍宽倍高
///
/// 0:取消倍宽倍高模式 1:倍高模式 2:倍宽模式 3:两倍大小
///
public string cmd_fontsize_btp_m2801(int size)
{
string _cmdstr = "";
//只有0和1两种模式
int fontsize = size; switch (fontsize)
{
case 1:
_cmdstr = new stringbuffer().append((char)27).append((char)33).append((char)17).tostring();
break;
case 2:
_cmdstr = new stringbuffer().append((char)27).append((char)33).append((char)33).tostring();
break;
case 3:
_cmdstr = new stringbuffer().append((char)27).append((char)33).append((char)49).tostring();
break;
default:
_cmdstr = new stringbuffer().append((char)27).append((char)33).append((char)1).tostring();
break;
} return _cmdstr;
} ///
/// 走纸
///
/// 走纸的行数
///
public string cmd_pagego(int line)
{
return new stringbuffer().append((char)27).append((char)100).append((char)line).tostring();
} ///
/// 切割
///
///
public string cmd_cutpage()
{
return new stringbuffer().append((char)27).append((char)109).tostring();
} ///
/// 返回状态(返回8位的二进制)
///
/// 1:打印机状态 2:脱机状态 3:错误状态 4:传送纸状态
/// 返回打印机状态如下:
/// 第一位:固定为0
/// 第二位:固定为1
/// 第三位:0:一个或两个钱箱打开 1:两个钱箱都关闭
/// 第四位:0:联机 1:脱机
/// 第五位:固定为1
/// 第六位:未定义
/// 第七位:未定义
/// 第八位:固定为0
///
/// 返回脱机状态如下:
/// 第一位:固定为0
/// 第二位:固定为1
/// 第三位:0:上盖关 1:上盖开
/// 第四位:0:未按走纸键 1:按下走纸键
/// 第五位:固定为1
/// 第六位:0:打印机不缺纸 1: 打印机缺纸
/// 第七位:0:没有出错情况 1:有错误情况
/// 第八位:固定为0
///
/// 返回错误状态如下:
/// 第一位:固定为0
/// 第二位:固定为1
/// 第三位:未定义
/// 第四位:0:切刀无错误 1:切刀有错误
/// 第五位:固定为1
/// 第六位:0:无不可恢复错误 1: 有不可恢复错误
/// 第七位:0:打印头温度和电压正常 1:打印头温度或电压超出范围
/// 第八位:固定为0
///
/// 返回传送纸状态如下:
/// 第一位:固定为0
/// 第二位:固定为1
/// 第三位:0:有纸 1:纸将尽
/// 第四位:0:有纸 1:纸将尽
/// 第五位:固定为1
/// 第六位:0:有纸 1:纸尽
/// 第七位:0:有纸 1:纸尽
/// 第八位:固定为0
///
public string cmd_returnstatus(int num)
{
return new stringbuffer().append((char)16).append((char)4).append((char)num).tostring();
} ///
/// 条码高宽
///
///
///
public string cmd_tiaomaheight(int num)
{
//return ((char)29).append"h" ((char)num).tostring();
return new stringbuffer().append((char)29).append((char)104).append((char)num).tostring();
} ///
/// 条码宽度
///
///
///
public string cmd_tiaomawidth(int num)
{
//return ((char)29).append"w" ((char)num).tostring();
return new stringbuffer().append((char)29).append((char)119).append((char)num).tostring();
} ///
/// 条码数字打印的位置
///
/// 1:上方 2:下方 0:不打印数字
///
public string cmd_tiaomaweizi(int num)
{
return new stringbuffer().append((char)29).append("h").append((char)num).tostring();
} ///
/// 开始打印(条码类型为code39)
///
///
///
public string cmd_tiaomaprint(string numstr)
{
//return ((char)29).append"k" ((char)4).appendnumstr ((char)0).tostring();
return new stringbuffer().append((char)29).append((char)107).append((char)4).append(numstr).append ((char)0).tostring();
} ///
/// 打开钱箱
///
///
public string cmc_qianxiang()
{
return new stringbuffer().append((char)27).append((char)112).append((char)0).append((char)60).append((char)255).tostring();
}
}
具体怎么调用,每个方法注释写得都很清楚了,至于怎么排版,要看纸的宽度了,然后自己计算字符的长度进行设置,下面是一个异步调用的例子,下面方法仅仅是一个例子,请无视其他无用代码。
页面排版实现,首先得知道打印纸的宽度,还有你要在纸上显示几列,譬如我这个方法 我显示了4列,所有我先根据打印纸的宽度 先设置好每一列的大致宽度,如果该列的字符长度大于设置好的 可以设置换行,如果小于,为了排版整齐 可以补空格,大致实现原理就是这样。
// 粗略计算打印纸上每个字段占的长度
private int wlname = 18;
private int wlnum = 6;
private int wlunit = 7;
private int wlprice = 9;
/**
* 网络打印机
*
*
*/
class netprint extends asynctask{
private string localyhid;
private listlistwz = new arraylist ();
private string rr = "";
private string gysname = "";
private string title = "";
private string printtime = "";
private string errinfo = "";
private string totleprice = "";
private netprinter printer;
private string md_name;
private string date_time; public netprint(string localyhid, string gysname,string date_time) {
this.localyhid = localyhid;
this.gysname = gysname;
this.date_time = date_time;
isclick = true;
} @override
protected string doinbackground(string... params) {
// todo auto-generated method stub
try {
printer = new netprinter();
if(!utils.isempty(const.print_port)){
printer.open(const.print_ip, integer.parseint(const.print_port));
}
if(printer.ifopen){
jsonarray arrayitems = new jsonarray();
arrayitems = usedatabase.getlocaldata(localyhid);
totleprice = usedatabase.gettotleprice(localyhid);
listwz = json.parsearray(arrayitems.tostring(), saveenity.class);
mylogger.showlogwithlinenum(5, listwz.size() "长度");
collections.sort(listwz, new sacomparator()); listselect = new arraylist ();
listendselect = new arraylist ();
for (int i = 0; i < listwz.size(); i ) {
saveenity save = listwz.get(i);
select.add(save.djx_ck_name);
}
for (string s : select) {
if (collections.frequency(endselect, s) < 1) endselect.add(s);
}
for (int i = 0; i < endselect.size(); i ) {
rr = formatsstest(endselect.get(i), listwz);
}
string name = "物料名";
string num = "数量";
string unit = "单位";
string price = "单价";
string spc = "";
for (int j = 0; j < wlname - getwordcount("物料名"); j ) {
spc = spc " ";
}
log.e("物料名称长度" wlname "补空格", wlname-getwordcount("物料名") "");
name = name spc;
spc = "";
for (int i = 0; i < wlnum - getwordcount("数量"); i ) {
spc = spc " ";
}
num = spc num;
log.e("数量长度" wlnum "补空格", wlnum-getwordcount("数量") "");
spc = ""; for (int i = 0; i < wlunit - getwordcount("单位"); i ) {
spc = spc " ";
}
unit = spc unit;
spc = "";
log.e("单位长度" wlunit "补空格", wlunit-getwordcount("单位") "");
for (int i = 0; i < wlprice - getwordcount("单价"); i ) {
spc = spc " ";
}
price = spc price;
spc = "";
log.e("长度" wlprice "补空格", wlprice-getwordcount("单价") "");
string printstr = name price num unit;
title = "供应商: " gysname "\n" printstr;
printtime = "打印时间:" date_time "\n\n";
md_name = const.md_name; }
} catch (exception e) {
e.printstacktrace();
}
return null;
} @override
protected void onpreexecute() {
// todo auto-generated method stub
super.onpreexecute();
} @override
protected void onpostexecute(string result) {
// todo auto-generated method stub
super.onpostexecute(result);
if(printer.ifopen){
string[] aaray = rr.split("#");
printer.printtext(title, 0, 0, 0);
printer.printenter();
for (string s : aaray) {
printer.printtext(s, 0, 0, 0);
}
printer.printenter();
printer.printtext(totleprice, 0, 0, 0);
printer.printenter();
printer.printenter();
printer.printtext(printtime, 0, 0, 0);
printer.printenter();
printer.printtext(md_name, 1,1,0);
printer.printnewlines(5);
printer.cutpage(0);
printer.close();
isclick = false;
mytoast.mylogo(history.this, "打印完成");
}else{
mytoast.mylogo(history.this, "未能连接打印机");
}
}
}