//bindService connection ServiceConnection conn= new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { //Bind successfully binder= (IMyBinder) iBinder; Log.e("binder","connected"); } @Override public void onServiceDisconnected(ComponentName componentName) { Log.e("disbinder","disconnected"); } }; import com.posprinter.printdemo.R; import com.posprinter.printdemo.utils.Conts; import com.posprinter.printdemo.utils.DeviceReceiver; public static IMyBinder binder; //bindService connection ServiceConnection conn= new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { //Bind successfully binder= (IMyBinder) iBinder; Log.e("binder","connected"); } @Override public void onServiceDisconnected(ComponentName componentName) { Log.e("disbinder","disconnected"); } }; private DeviceReceiver myDevice; if (ISCONNECT){ binder.disconnectCurrentPort(new UiExecute() { @Override public void onsucess() { showSnackbar(getString(R.string.toast_discon_success)); showET.setText(""); BTCon.setText(getString(R.string.connect)); } @Override public void onfailed() { showSnackbar(getString(R.string.toast_discon_faile)); } }); }else { showSnackbar(getString(R.string.toast_present_con)); } connetNet(); private void connetNet(){ //ipAddress :ip address; portal:9100 binder.connectNetPort(ipAddress,9100, new UiExecute() { @Override public void onsucess() { ISCONNECT=true; showSnackbar(getString(R.string.con_success)); //in this ,you could call acceptdatafromprinter(),when //disconnect ,will execute onfailed(); binder.acceptdatafromprinter(new UiExecute() { @Override public void onsucess() { } @Override public void onfailed() { ISCONNECT=false; showSnackbar(getString(R.string.con_failed)); Intent intent=new Intent(); intent.setAction(DISCONNECT); sendBroadcast(intent); } }); } @Override public void onfailed() { //Execution of the connection in the UI thread after //the failure of the connection ISCONNECT=false; showSnackbar(getString(R.string.con_failed)); BTCon.setText(getString(R.string.con_failed)); } }); } /* BroadcastReceiver of disconnect */ private class Receiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { String action=intent.getAction(); if (action.equals(MainActivity.DISCONNECT)){ Message message=new Message(); message.what=4; handler.handleMessage(message); } } } private void printText(){ MainActivity.binder.writeDataByYouself( new UiExecute() { @Override public void onsucess() { } @Override public void onfailed() { } }, new ProcessData() { @Override public List processDataBeforeSend() { List list=new ArrayList(); //creat a text ,and make it to byte[], String str=text.getText().toString(); if (str.equals(null)||str.equals("")){ showSnackbar(getString(R.string.text_for)); }else { // initialize the printer // list.add( DataForSendToPrinterPos58.initializePrinter()); list.add(DataForSendToPrinterPos80.initializePrinter()); byte[] data1= StringUtils.strTobytes(str); list.add(data1); //should add the command of print and feed line,because print only when one line is complete, not one line, no print list.add(DataForSendToPrinterPos80.printAndFeedLine()); //cut pager list.add(DataForSendToPrinterPos80.selectCutPagerModerAndCutPager(66,1)); return list; } return null; } }); } /* print barcode before you print the barcode ,you should set attribute of the barcode,for example :width ,height ,HRI. */ private void printBarcode(){ MainActivity.binder.writeDataByYouself(new UiExecute() { @Override public void onsucess() { showSnackbar("01234567890"); } @Override public void onfailed() { } }, new ProcessData() { @Override public List processDataBeforeSend() { Listlist=new ArrayList(); //initialize the printer list.add(DataForSendToPrinterPos80.initializePrinter()); //select alignment list.add(DataForSendToPrinterPos80.selectAlignment(1)); //select HRI position list.add(DataForSendToPrinterPos80.selectHRICharacterPrintPosition(02)); //set the width list.add(DataForSendToPrinterPos80.setBarcodeWidth(3)); //set the height ,usually 162 list.add(DataForSendToPrinterPos80.setBarcodeHeight(162)); //print barcode ,attention,there are two method for barcode. //different barcode type,please refer to the programming manual //UPC-A list.add(DataForSendToPrinterPos80.printBarcode(69,10,"B123456789")); list.add(DataForSendToPrinterPos80.printAndFeedLine()); return list; } }); } private void printQRcode(){ MainActivity.binder.writeDataByYouself(new UiExecute() { @Override public void onsucess() { } @Override public void onfailed() { } }, new ProcessData() { @Override public List processDataBeforeSend() { List list=new ArrayList(); //initialize the printer list.add(DataForSendToPrinterPos80.initializePrinter()); //select alignment list.add(DataForSendToPrinterPos80.selectAlignment(1)); //set the size list.add(DataForSendToPrinterPos80.SetsTheSizeOfTheQRCodeSymbolModule(3)); //set the error correction level list.add(DataForSendToPrinterPos80.SetsTheErrorCorrectionLevelForQRCodeSymbol(48)); //store symbol data in the QRcode symbol storage area list.add(DataForSendToPrinterPos80.StoresSymbolDataInTheQRCodeSymbolStorageArea( "Welcome to Printer Technology to create advantages Quality to win in the future" )); //Prints The QRCode Symbol Data In The Symbol Storage Area list.add(DataForSendToPrinterPos80.PrintsTheQRCodeSymbolDataInTheSymbolStorageArea()); //print list.add(DataForSendToPrinterPos80.printAndFeedLine()); //or else you could use the simple encapsulation method //but different ¡ACall the step method above¡Athe storage data din't clean up , //call PrintsTheQRCodeSymbolDataInTheSymbolStorageArea¡Aprint¡Ayou don't have set the content of qrcode again //Equivalent to resetting the qrcode contents in the cache every time //list.add(DataForSendToPrinterPos80.printQRcode(3, 48, "www.xprint.net")); return list; } }); } public Bitmap convertGreyImg(Bitmap img) { int width = img.getWidth(); int height = img.getHeight(); int[] pixels = new int[width * height]; img.getPixels(pixels, 0, width, 0, 0, width, height); //The arithmetic average of a grayscale image; a threshold double redSum=0,greenSum=0,blueSun=0; double total=width*height; for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { int grey = pixels[width * i + j]; int red = ((grey & 0x00FF0000 ) >> 16); int green = ((grey & 0x0000FF00) >> 8); int blue = (grey & 0x000000FF); redSum+=red; greenSum+=green; blueSun+=blue; } } int m=(int) (redSum/total); //Conversion monochrome diagram for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { int grey = pixels[width * i + j]; int alpha1 = 0xFF << 24; int red = ((grey & 0x00FF0000 ) >> 16); int green = ((grey & 0x0000FF00) >> 8); int blue = (grey & 0x000000FF); if (red>=m) { red=green=blue=255; }else{ red=green=blue=0; } grey = alpha1 | (red << 16) | (green << 8) | blue; pixels[width*i+j]=grey; } } Bitmap mBitmap=Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); mBitmap.setPixels(pixels, 0, width, 0, 0, width, height); return mBitmap; } public static byte[] strTobytes(String str){ byte[] b=null,data=null; try { b = str.getBytes("utf-8"); data=new String(b,"utf-8").getBytes("gbk"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return data; } /** * byte[] merger * */ public static byte[] byteMerger(byte[] byte_1, byte[] byte_2){ byte[] byte_3 = new byte[byte_1.length+byte_2.length]; System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length); System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length); return byte_3; } public static byte[] strTobytes(String str ,String charset){ byte[] b=null,data=null; try { b = str.getBytes("utf-8"); data=new String(b,"utf-8").getBytes(charset); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return data; } manifest package="com.posprinter.printdemo" android:versionCode="1" android:versionName="1.0" >