打印的工具類:java
public class PrintLine {
String TAG = "xxl";
static String PRINT_IP = "192.168.1.206";
static int PRINT_PORT = 9100;
OutputStream socketOut = null;
OutputStreamWriter writer = null;
String[] Colum_Name = new String[] { "ID", "菜名", "份數", "小計金額" };// 設定小票列標題
/**
* @throws IOException
*
*/
public PrintLine() throws IOException {
// TODO Auto-generated constructor stub
// 創建打印機鏈接
//
Socket socket = new Socket( PRINT_IP, PRINT_PORT);
Log.i(TAG, PRINT_PORT+"");
socketOut = socket.getOutputStream();
socket.isClosed();
writer = new OutputStreamWriter(socketOut, "GBK");
}
/**
* @param PRINT_DATA
* 小票主要數據
* @param GS_INFO
* 小票附帶信息
* @param CAIDAN_SN
* 小票號碼
*/
public void print(List<Map<String, Object>> PRINT_DATA,
Map<String, String> GS_INFO, String CAIDAN_SN) {
/*
* 接收小票頁面 公司信息 Map<String,String> map=new HashMap<String,String>();
* map.put("GS_Name", "武漢聯興科技有限公司"); map.put("GS_Address",
* "武漢市解放大道2679號"); map.put("GS_Tel", "13507115045"); map.put("GS_Qq",
* "138027869");
*/
try {
// 條碼打印指令
byte[] PRINT_CODE = new byte[9];
PRINT_CODE[0] = 0x1d;
PRINT_CODE[1] = 0x68;
PRINT_CODE[2] = 120;
PRINT_CODE[3] = 0x1d;
PRINT_CODE[4] = 0x48;
PRINT_CODE[5] = 0x10;
PRINT_CODE[6] = 0x1d;
PRINT_CODE[7] = 0x6B;
PRINT_CODE[8] = 0x02;
// 清除字體放大指令
byte[] CLEAR_FONT = new byte[3];
CLEAR_FONT[0] = 0x1c;
CLEAR_FONT[1] = 0x21;
CLEAR_FONT[2] = 1;
// 字體加粗指令
byte[] FONT_B = new byte[3];
FONT_B[0] = 27;
FONT_B[1] = 33;
FONT_B[2] = 8;
// 字體橫向放大一倍
byte[] FD_FONT = new byte[3];
FD_FONT[0] = 0x1c;
FD_FONT[1] = 0x21;
FD_FONT[2] = 4;
// 計算合計金額
int price = 0;
// 初始化打印機
socketOut.write(27);
socketOut.write(27);
socketOut.write(FD_FONT);// 字體放大
socketOut.write(FONT_B);// 字體加粗
//公司名稱
writer.write(" " + GS_INFO.get("GS_Name") + " \r\n");
writer.flush();// 關鍵,很重要,否則指令一次性輸出,後面指令覆蓋前面指令,致使取消放大指令無效
socketOut.write(CLEAR_FONT);
socketOut.write(10);
//編號
writer.write("NO: " + CAIDAN_SN + " \r\n");
writer.write("-------------------------------------\r\n");
writer.write(Fix_String_Lenth(1,Colum_Name[0], 6)
+ Fix_String_Lenth(0,Colum_Name[1], 14)
+ Fix_String_Lenth(1,Colum_Name[2], 4)
+ Fix_String_Lenth(1,Colum_Name[3], 6) + "\r\n");
for (int i = 0; i < PRINT_DATA.size(); i++) {
int b =PRINT_DATA.get(i).get("cai_name").toString().length();
writer.write(Fix_String_Lenth(1,i + 1 + "", 6)
+ Fix_String_Lenth(0,PRINT_DATA.get(i).get("cai_name")
.toString(), 18-b)
+ Fix_String_Lenth(1,1 + "", 6)
+ Fix_String_Lenth(1,PRINT_DATA.get(i).get("cai_price")
.toString(), 6) + "\r\n");
price += Integer.parseInt(PRINT_DATA.get(i).get("cai_price")
.toString());
}
writer.write("-------------------------------------\r\n");
writer.write("本單共 " + PRINT_DATA.size() + " 道菜品,合計費用: " + price
+ "元\r\n");
writer.write("-------------------------------------\r\n");
writer.write("地址: " + GS_INFO.get("GS_Address") + "\r\n");
writer.write("聯繫電話: " + GS_INFO.get("GS_Tel") + " \r\n");
writer.write("-------------------------------------\r\n");
writer.write(" 歡 迎 品 償 謝 謝 惠 顧\r\n");
// 下面指令爲打印完成後自動走紙
writer.write(27);
writer.write(100);
writer.write(4);
writer.write(10);
writer.close();
socketOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 字符串長度補齊,方便打印時對齊,美化打印頁面,不過中文計算好像有點問題
*
* @param strs
* 源字符
* @param len
* 指定字符長度
* @return
* @throws UnsupportedEncodingException
*/
public String Fix_String_Lenth(int type ,String strs, int len) {
String strtemp = strs;
int len1 = strs.length();
switch (type) {
case 0:
while (strtemp.length() < len) {
strtemp += " ";}
break;
case 1:
while (strtemp.length() < len) {
strtemp += " ";}
break;
default:
break;
} android
return strtemp;
}
}web
調用打印工具類打印的Activityapp
package com.example.airprint;socket
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;ide
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;工具
public class MainActivity extends Activity implements OnClickListener{測試
String TAG = "xxl";
List list;
Map<String,String> map;
Map<String,Object> dishMap;
Map<String,Object> dishMap1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.print);
btn.setOnClickListener(this);
}字體
@Override
public void onClick(View v) {
list = new ArrayList<Map<String, Object>>();
dishMap = new HashMap<String,Object>();
dishMap1 = new HashMap<String,Object>();
dishMap.put("cai_name", "地三鮮");
dishMap.put("cai_price", "20");
dishMap1.put("cai_name", "長豆角肉末蓋澆飯");
dishMap1.put("cai_price", "10");
list.add(dishMap);
list.add(dishMap1);
map =new HashMap<String,String>();
map.put("GS_Name", "杭州友絡軟件有限公司");
map.put("GS_Address",
"浙江-杭州,濱江區南環路3760號保億創藝大廈1203室");
map.put("GS_Tel", "13507115045");
map.put("GS_Qq","138027869");
Log.i(TAG, map.get("GS_Address"));this
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss ");
Date curDate = new Date(System.currentTimeMillis());//獲取當前時間
final String str = formatter.format(curDate); //能夠獲取當前的年月時分,也能夠分開寫:
new Thread(){
@SuppressWarnings("unchecked")
public void run() {
try {
new PrintLine().print(list,map,str);
Log.i(TAG, map.get("GS_Name"));
} catch (IOException e) {
Log.i(TAG, e+"");
e.printStackTrace();
}
};
}.start();
}
}
pos打印機設置字體大小的代碼:
因爲POS打印機的類型不一樣,可能支持的EPSON指令也不一樣。就拿放大字體來講,pos58 打印機和 pos80 打印機指令就不盡相同,那麼如何兼容多種打印機呢。
好比說,有的打印機並非使用 0x1c 做爲放大指令,而是使用 0x1b 做爲放大指令