我已经实现了EPSON SDK(用于蓝牙)并且工作正常,但是没有在其他打印机上工作,是否有任何通用的方法来实现它。什么是ESC命令,它是如何工作的?
请找this one.It会帮助你解决问题。 ESC / POS命令参考提供有关ESC / POS命令的详细信息,例如标准命令语法和协议。它针对想要使用ESC / POS命令控制打印机的程序员。
提供ESC / POS命令参考作为纸卷打印机的ESC / POS APG的替换。因此,用于纸卷打印机的ESC / POS APG将不再进行修订。 ESC / POS命令参考包含标准模型(如ANK模型或日本模型)的命令信息,可能包含中国模型或南亚模型。其他模型(如自定义)可能支持不同的命令或具有不同的范围或命令参数的不同默认值。请参阅每个产品规格。
使用以下代码 注意:您可以使用OutPutStream对象来编写打印机,无论是蓝牙还是以太网或wifi
public class PrinterConstants {
public static int PORT=9100,TOTAL_CHAR=45,DIV1=10,DIV2=5,DIV3=10,DIV4=10,DIV5=10;
public static String IP="192.168.1.35";
private OutputStream printer;
public static final String UUID="00001101-0000-1000-8000-00805f9b34fb";
public PrinterConstants(OutputStream printer){
this.printer=printer;
}
public void printString(String str) throws IOException {
Log.i("PRINTER_PRE",str);
printer.write(str.getBytes());
printer.write(0xA);
printer.flush();
}
public void storeString(String str) throws IOException {
printer.write(str.getBytes());
printer.flush();
}
public void printStorage() throws IOException {
printer.write(0xA);
printer.flush();
}
public void feed(int feed) throws IOException {
//escInit();
printer.write(0x1B);
printer.write("d".getBytes());
printer.write(feed);printer.flush();
}
public void printAndFeed(String str, int feed) throws IOException {
//escInit();
printer.write(str.getBytes());
printer.write(0x1B);
printer.write("d".getBytes());
printer.write(feed);printer.flush();
}
public void setBold(Boolean bool) throws IOException {
printer.write(0x1B);
printer.write("E".getBytes());
printer.write((int)(bool?1:0));printer.flush();
}
/**
* Sets white on black printing
* */
public void setInverse(Boolean bool) throws IOException {
bool=false;
printer.write(0x1D);
printer.write("B".getBytes());
printer.write( (int)(bool?1:0) );printer.flush();
}
public void resetToDefault() throws IOException {
setInverse(false);
setBold(false);
setUnderline(0);
setJustification(0);printer.flush();
}
/**
* Sets underline and weight
*
* @param val
* 0 = no underline.
* 1 = single weight underline.
* 2 = double weight underline.
* */
public void setUnderline(int val) throws IOException {
printer.write(0x1B);
printer.write("-".getBytes());
printer.write(val);printer.flush();
}
/**
* Sets left, center, right justification
*
* @param val
* 0 = left justify.
* 1 = center justify.
* 2 = right justify.
* */
public void setJustification(int val) throws IOException {
printer.write(0x1B);
printer.write("a".getBytes());
printer.write(val);
printer.flush();
}
public void setLeftRight(String left,String right) throws IOException {
printer.write(0x1B);
printer.write("a".getBytes());
printer.write(0);
printString(left);
printer.write(0x1B);
printer.write("a".getBytes());
printer.write(2);
printString(right);
printer.flush();
}
public void printBarcode(String code, int type, int h, int w, int font, int pos) throws IOException {
//need to test for errors in length of code
//also control for input type=0-6
//GS H = HRI position
printer.write(0x1D);
printer.write("H".getBytes());
printer.write(pos); //0=no print, 1=above, 2=below, 3=above & below
//GS f = set barcode characters
printer.write(0x1D);
printer.write("f".getBytes());
printer.write(font);
//GS h = sets barcode height
printer.write(0x1D);
printer.write("h".getBytes());
printer.write(h);
//GS w = sets barcode width
printer.write(0x1D);
printer.write("w".getBytes());
printer.write(w);//module = 1-6
//GS k
printer.write(0x1D); //GS
printer.write("k".getBytes()); //k
printer.write(type);//m = barcode type 0-6
printer.write(code.length()); //length of encoded string
printer.write(code.getBytes());//d1-dk
printer.write(0);//print barcode
printer.flush();
}
public void beep() throws IOException {
printer.write(0x1B);
printer.write("(A".getBytes());
printer.write(4);
printer.write(0);
printer.write(48);
printer.write(55);
printer.write(3);
printer.write(15);printer.flush();
}
public void setLineSpacing(int spacing) throws IOException {
//function ESC 3
printer.write(0x1B);
printer.write("3".getBytes());
printer.write(spacing);
}
public void cut() throws IOException {
printer.write(0x1D);
printer.write("V".getBytes());
printer.write(48);
printer.write(0);printer.flush();
}
}
通过上面的使用,您可以直接将ESC / POS命令写入输出流
您已经找到并创建了几种不同版本的打印机的实现,大多数都与另一种兼容,所以它不会那么难(另外你将复制供应商的SDK)。
然后创建一个实现将使用的接口,如initialize,scan,printText,printImage,printBarCode
像这样阅读设备......
static String getDeviceName() {
String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;
if (model.startsWith(manufacturer)) {
return capitalize(model);
} else {
return capitalize(manufacturer) + " " + model;
}
}
并且在默认为适用于大多数设备的实现之前,使用结果来确定要使用的实现。界面会让你很快忘记你遇到的麻烦。
一个ESC命令,只是对打印机的指令,它们几乎与大多数设备相同......它们用于启动新行,对齐文本,粗体等等。将它们视为html标记(强,h1,中心) )因为你将它们与要打印的文本混合在一起,这使得创建酷炫的打印效果非常容易。