CCPrintingApi CCPrintingAPI = null;
CCPrintingAPI = new CCPrintingApi();
//獲取所有(yǒu)打印機
GetPrinters(m_OpenId, m_Tokens);
//指定打印機
String printer = "TSC TTP-244 Pro";
//指定傳入的數(shù)據
String prtdata = "[{\"ImageUrl\":\"static/picture/2315002468301572.jpg\"}]";
//執行(xíng)打印
Print(m_OpenId, m_Tokens, printer, prtdata, 0)
public void GetPrinters(String openid,String token,int itemid){
ConfigPrintSettings cfgData = new ConfigPrintSettings() ;
cfgData.XAction = "Printers";
cfgData.XOpenId = openid;
cfgData.XTokens = token;
cfgData.ItemID = itemid;
CCPrintingAPI.Excute(cfgData, new CCPrintingApiCallBack() {
@Override
public void OnFinished(int status, String[] arg0, String sessionid){
if (arg0 == null || arg0.length ==0 ) return;
if (status == 1) {
System.out.println( String.format("ALL Printers:\r\n%s ", CCPrintingAPI.join(",", arg0) ));
} else{
System.out.println( String.format("GetPrinters Fail: %s", arg0[0]));
}
}
});
}
private String GetSelfDraw(){
CCSelfdraw vSelfdraw = new CCSelfdraw();
vSelfdraw.XPageSettings.PrinterPaperAutoSet =1;
vSelfdraw.XPageSettings.UseQuickSetTool = 1;
//标簽尺寸
vSelfdraw.XLabelDetail .Width = 70;
vSelfdraw.XLabelDetail .Height = 50;
vSelfdraw.XLabelDetail .ColumnSpan = 2;
vSelfdraw.XLabelDetail .RowSpan = 2;
//圖片設置
ImageElement vImageElement = new ImageElement();
vImageElement.Name ="Image1";
vImageElement.X = 0;
vImageElement.Y = 0;
vImageElement.Width = 70;
vImageElement.Height = 50;
vImageElement.IsBind = 1;
vImageElement.BindField = "ImageUrl";
vImageElement.KeepRatio = 1;
vImageElement.ImageColor = 2;
vSelfdraw.XImageElements.add(vImageElement) ;
//數(shù)據字段
String[] FieldNames = {"ImageUrl"};
vSelfdraw.XDataBase.SetFieldNames(FieldNames);
return vSelfdraw.toString();
}
public void Print(String openid,String token,String printer,String prtdata, int ispreview){
//構建模闆
String selfdraw = GetSelfDraw();
//設置打印參數(shù)
ConfigPrintSettings cfgData = GetPrintSettings(openid, token,printer,prtdata,selfdraw,ispreview);
//執行(xíng)打印命令
CCPrintingAPI.Excute(cfgData, new CCPrintingApiCallBack() {
/*打印命令執行(xíng)回調 */
@Override
public void OnFinished(int status, String[] arg0, String sessionid, String[] Data){
if (arg0 == null || arg0.length ==0 ) return;
if (status == 1) {
System.out.println( String.format("Print:%s", CCPrintingAPI.join(",", arg0) ) );
}else{
System.out.println( String.format("Print Fail: %s", arg0[0]));
}
}
});
}
private ConfigPrintSettings GetPrintSettings(String openid,String token,String printer,String prtdata,String selfdraw,int ispreview) {
/*打印參數(shù)配置,打印數(shù)據設置*/
ConfigPrintSettings cfgData = new ConfigPrintSettings();
cfgData.XAction = "Print";
cfgData.XOpenId = openid;
cfgData.XTokens = token;
/*指定打印機名稱,不使用默認打印機時(shí),為(wèi)空(kōng),則使用配置的打印機*/
cfgData.PrinterName = printer;
/*0-打印*/
cfgData.Preview = ispreview;
/*模闆方式,0-客戶端模闆,1-遠程模闆,2-如果本地模闆存在,使用本地,不存在使用遠程模闆,3-不使用模闆*/
cfgData.TemplateMode = 3;
cfgData.SelfDraw = selfdraw;
/*打印數(shù)據的類型,0-數(shù)組,1-Json數(shù)組,元素為(wèi)JSON對象,字段:值 , JSON.stringify(prtData)*/
cfgData.PrintDataType = 1;
/*打印數(shù)據*/
cfgData.PrintData = prtdata;
return cfgData;
}