package com.warehouse.common.util;
import com.google.common.collect.Lists;
import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
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;
// String s_prt_buffer="^XA\n^JMA^LL180^PW660^MD10^RP2^PON^LRN^LH10,0\n", s_prt = "";
String s_prt = "^XA", s_prt_buffer="";
static class Employees{
private String name;
private String companyName;
private String identity;
public Employees(String name,String companyName,String identity){
this.name=name;
this.companyName=companyName;
this.identity=identity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getIdentity() {
return identity;
}
public void setIdentity(String identity) {
this.identity = identity;
}
}
public static void main(String[] args) {
List<Employees> employeesList= Lists.newArrayList();
employeesList.add(new Employees("杨志军","启旭","340321198403266210"));
// employeesList.add(new Employees("王拾红","启旭","61032119960522721X"));
// employeesList.add(new Employees("李岩","启旭","130582198805021210"));
// employeesList.add(new Employees("王爱丽","启旭","411024198811144724"));
for(int i=0;i<employeesList.size();i++){
Employees employees= employeesList.get(i);
empPrint(employees.getIdentity(),employees.getName()+"| "+employees.getCompanyName());
}
}
public static String empPrint(String barcode,String empNameComName){
String resultStr=null;
try{
SimplePrint a = new SimplePrint();
a.setCommand(barcode,empNameComName);
String str = a.getCommand();
System.out.println(str);
resultStr= a.print(str);
}catch (Exception e){
resultStr="SimplePrint empPrint error";
e.printStackTrace();
}
return resultStr;
}
public SimplePrint() throws Exception {
String paths=CommonUtils.getAllMessage("config", "EMPLOYEE_PRINT_ZPL_PATH");
File file = new File(paths);
FileInputStream fis = new FileInputStream(file);
dotfont = new byte[fis.available()];
fis.read(dotfont);
fis.close();
}
public void setCommand(String barcode,String nameCompanyName) {
printCN(nameCompanyName, 100, 15,30,34,1);
printBarcode(barcode, 180, 80);
}
protected String getCommand() {
// return s_prt_buffer+s_prt+"^PQ 1\n^XZ";
return s_prt + s_prt_buffer+"^XZ";
}
public String print(String str) throws PrintException {
PrintService psZebra = PrintServiceLookup.lookupDefaultPrintService();
if (psZebra == null) {
System.out.println("没有发现条码打印机.");
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);
return null;
}
/**
* 打印条形码
* @param barcode 字符串
* @param x x坐标
* @param y y坐标
// * @param h 高度
*/
protected void printBarcode(String barcode,int x, int y) {
// s_prt_buffer += "^FO"+x+","+y+"^BY2,3,50^BCN,180,N,N,N,A^FD" + barcode + "^FS\n";
s_prt_buffer += "^FO"+x+","+y+"^BY2,3,60^BCN,180,N,N,N,A^FD" + barcode + "^FS\n";
}
/**
* 打印英文字符,数字
* @param str
* @param x
* @param y
*/
protected void printChar(String str,int x, int y,int h,int w) {
System.out.println(str);
s_prt_buffer +="^FO"+x+","+y+"^A0,"+h+","+w+"^FD"+str+"^FS";
}
protected void printCharR(String str,int x, int y,int h,int w) {
System.out.println(str);
s_prt_buffer +="^FO"+x+","+y+"^A0R,"+h+","+w+"^FD"+str+"^FS";
}
/**
* 打印中文字符串
* @param strCN
* @param x
* @param y
*/
protected void printCN(String strCN, int x, int y,int h,int w,int b) {
System.out.println(strCN);
byte[] ch = str2bytes(strCN);
System.out.println(ch.length);
for (int off = 0; off < ch.length;) {
if (((int) ch[off] & 0x00ff) >= 0xA0) {
int qcode = ch[off] & 0xff;
int wcode = ch[off + 1] & 0xff;
s_prt_buffer = s_prt_buffer
+ String.format("^FO%d,%d^XG0000%01X%01X,%d,%d^FS\n", x,
y, qcode, wcode,b,b);
s_prt += String.format("~DG0000%02X%02X,00072,003,\n", qcode,
wcode);
qcode = (qcode + 128 - 32) & 0x00ff;
wcode = (wcode + 128 - 32) & 0x00ff;
int offset = ((int) qcode - 16) * 94 * 72 + ((int) wcode - 1)
* 72;
for (int j = 0; j < 72; j += 3) {
qcode = (int) dotfont[j + offset] & 0x00ff;
wcode = (int) dotfont[j + offset + 1] & 0x00ff;
int qcode1 = (int) dotfont[j + offset + 2] & 0x00ff;
s_prt += String.format("%02X%02X%02X\n", qcode, wcode,
qcode1);
}
x = x + 25*b;
off = off + 2;
} else if (((int) ch[off] & 0x00FF) < 0xA0) {
printChar(String.format("%c",ch[off]), x, y, h, w);
// s_prt_buffer += String.format("^FO"+x+","+y+"^A0,"+h+","+w+"^FD%c^FS\n",ch[off]);
// s_prt_buffer += "^FO"+x+","+y+"^A0,"+h+",20^FD"+ch[off]+"^FS\n";
x = x + 15;
off++;
}
}
}
byte[] str2bytes(String s) {
if (null == s || "".equals(s)) {
return null;
}
byte[] abytes = null;
try {
abytes = s.getBytes("gb2312");
} catch (UnsupportedEncodingException ex) {
}
return abytes;
}
}