You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RNXNetprinter.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package com.mirfalahtech.xprinter;
  2. import android.content.ComponentName;
  3. import android.content.Intent;
  4. import android.content.ServiceConnection;
  5. import android.graphics.Matrix;
  6. import android.os.IBinder;
  7. import android.util.Log;
  8. import java.util.Set;
  9. import android.graphics.Bitmap;
  10. import android.os.Handler;
  11. import android.os.Message;
  12. import android.graphics.BitmapFactory;
  13. import android.util.Base64;
  14. import com.facebook.react.bridge.Arguments;
  15. import com.facebook.react.bridge.Promise;
  16. import com.facebook.react.bridge.ReactApplicationContext;
  17. import com.facebook.react.bridge.ReactContextBaseJavaModule;
  18. import com.facebook.react.bridge.ReactMethod;
  19. import com.facebook.react.bridge.WritableArray;
  20. import com.facebook.react.bridge.WritableMap;
  21. import net.posprinter.posprinterface.IMyBinder;
  22. import com.mirfalahtech.xprinter.StringUtils;
  23. import com.zxy.tiny.Tiny;
  24. import com.zxy.tiny.callback.BitmapCallback;
  25. import net.posprinter.posprinterface.ProcessData;
  26. import net.posprinter.posprinterface.UiExecute;
  27. import net.posprinter.service.PosprinterService;
  28. import net.posprinter.utils.BitmapToByteData;
  29. import net.posprinter.utils.DataForSendToPrinterPos58;
  30. import net.posprinter.utils.DataForSendToPrinterPos80;
  31. import net.posprinter.utils.PosPrinterDev;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. import static android.content.Context.BIND_AUTO_CREATE;
  35. public class RNXNetprinter extends ReactContextBaseJavaModule {
  36. private String LOG_TAG = "RNXNetprinter";
  37. private ReactApplicationContext context;
  38. private byte[] mBuffer = new byte[0];
  39. // Net
  40. public static IMyBinder binder;
  41. public static boolean ISCONNECT;
  42. //bindService connection
  43. ServiceConnection conn= new ServiceConnection() {
  44. @Override
  45. public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
  46. //Bind successfully
  47. binder= (IMyBinder) iBinder;
  48. Log.e("binder","connected");
  49. }
  50. @Override
  51. public void onServiceDisconnected(ComponentName componentName) {
  52. Log.e("disbinder","disconnected");
  53. }
  54. };
  55. public RNXNetprinter(ReactApplicationContext reactContext) {
  56. super(reactContext);
  57. this.context = reactContext;
  58. Intent intent=new Intent(this.context, PosprinterService.class);
  59. this.context.bindService(intent, conn, BIND_AUTO_CREATE);
  60. Log.v(LOG_TAG, "RNXNetprinter alloc");
  61. }
  62. @Override
  63. public String getName() {
  64. return "RNXNetprinter";
  65. }
  66. @ReactMethod
  67. public void connectPrinter(String address, final Promise promise){
  68. binder.connectNetPort(address,9100, new UiExecute() {
  69. @Override
  70. public void onsucess() {
  71. ISCONNECT=true;
  72. //in this ,you could call acceptdatafromprinter(),when disconnect ,will execute onfailed();
  73. binder.acceptdatafromprinter(new UiExecute() {
  74. @Override
  75. public void onsucess() {
  76. promise.resolve(true);
  77. }
  78. @Override
  79. public void onfailed() {
  80. ISCONNECT=false;
  81. promise.reject("-105", "Device address not exist.");
  82. }
  83. });
  84. }
  85. @Override
  86. public void onfailed() {
  87. ISCONNECT=false;
  88. promise.reject("-105", "Device address not exist.");
  89. }
  90. });
  91. }
  92. @ReactMethod
  93. public void pushText(String text, final int size){
  94. final String tempText = text;
  95. binder.writeDataByYouself(
  96. new UiExecute() {
  97. @Override
  98. public void onsucess() {
  99. Log.v(LOG_TAG, "pushText onsucess");
  100. }
  101. @Override
  102. public void onfailed() {
  103. Log.v(LOG_TAG, "pushText onfailed");
  104. }
  105. }, new ProcessData() {
  106. @Override
  107. public List<byte[]> processDataBeforeSend() {
  108. List<byte[]> list=new ArrayList<byte[]>();
  109. //creat a text ,and make it to byte[],
  110. String str=tempText;
  111. if (str.equals(null)||str.equals("")){
  112. }else {
  113. //initialize the printer
  114. // list.add( DataForSendToPrinterPos58.initializePrinter());
  115. list.add(DataForSendToPrinterPos80.initializePrinter());
  116. byte[] data1= StringUtils.strTobytes(str);
  117. list.add(PrinterCommands.ESC_ALIGN_CENTER);
  118. list.add(DataForSendToPrinterPos80.selectCharacterSize(size));
  119. list.add(data1);
  120. //should add the command of print and feed line,because print only when one line is complete, not one line, no print
  121. list.add(DataForSendToPrinterPos80.printAndFeedLine());
  122. return list;
  123. }
  124. return null;
  125. }
  126. }
  127. );
  128. }
  129. private Bitmap b1;//grey-scale bitmap
  130. private Bitmap b2;//compress bitmap
  131. @ReactMethod
  132. private void printImage(String base64img, final int width){
  133. byte [] bytes = Base64.decode(base64img, Base64.DEFAULT);
  134. Bitmap b = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
  135. if (b != null) {
  136. b1=convertGreyImg(b);
  137. Message message=new Message();
  138. message.what=1;
  139. handler.handleMessage(message);
  140. //compress the bitmap
  141. Tiny.BitmapCompressOptions options = new Tiny.BitmapCompressOptions();
  142. Tiny.getInstance().source(b1).asBitmap().withOptions(options).compress(new BitmapCallback() {
  143. @Override
  144. public void callback(boolean isSuccess, Bitmap bitmap) {
  145. if (isSuccess){
  146. // Toast.makeText(PosActivity.this,"bitmap: "+bitmap.getByteCount(),Toast.LENGTH_LONG).show();
  147. b2=bitmap;
  148. b2=resizeImage(b2,width,false);
  149. Message message=new Message();
  150. message.what=2;
  151. handler.handleMessage(message);
  152. }
  153. }
  154. });
  155. }
  156. }
  157. public static Bitmap resizeImage(Bitmap bitmap, int w,boolean ischecked)
  158. {
  159. Bitmap BitmapOrg = bitmap;
  160. Bitmap resizedBitmap = null;
  161. int width = BitmapOrg.getWidth();
  162. int height = BitmapOrg.getHeight();
  163. if (width<=w) {
  164. return bitmap;
  165. }
  166. if (!ischecked) {
  167. int newWidth = w;
  168. int newHeight = height*w/width;
  169. float scaleWidth = ((float) newWidth) / width;
  170. float scaleHeight = ((float) newHeight) / height;
  171. Matrix matrix = new Matrix();
  172. matrix.postScale(scaleWidth, scaleHeight);
  173. // if you want to rotate the Bitmap
  174. // matrix.postRotate(45);
  175. resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
  176. height, matrix, true);
  177. }else {
  178. resizedBitmap=Bitmap.createBitmap(BitmapOrg, 0, 0, w, height);
  179. }
  180. return resizedBitmap;
  181. }
  182. private void printpicCode(final Bitmap printBmp){
  183. binder.writeDataByYouself(new UiExecute() {
  184. @Override
  185. public void onsucess() {
  186. Log.v(LOG_TAG, "printpicCode onsucess");
  187. }
  188. @Override
  189. public void onfailed() {
  190. Log.v(LOG_TAG, "printpicCode onfailed");
  191. }
  192. }, new ProcessData() {
  193. @Override
  194. public List<byte[]> processDataBeforeSend() {
  195. List<byte[]> list=new ArrayList<byte[]>();
  196. list.add(DataForSendToPrinterPos80.initializePrinter());
  197. list.add(PrinterCommands.ESC_ALIGN_CENTER);
  198. list.add(DataForSendToPrinterPos80.printRasterBmp(
  199. 0,printBmp, BitmapToByteData.BmpType.Threshold, BitmapToByteData.AlignType.Left,576));
  200. // list.add(DataForSendToPrinterPos80.printAndFeedForward(3));
  201. return list;
  202. }
  203. });
  204. }
  205. public Handler handler=new Handler(){
  206. @Override
  207. public void handleMessage(Message msg) {
  208. super.handleMessage(msg);
  209. switch (msg.what){
  210. case 2:
  211. printpicCode(b2);
  212. break;
  213. }
  214. }
  215. };
  216. public Bitmap convertGreyImg(Bitmap img) {
  217. int width = img.getWidth();
  218. int height = img.getHeight();
  219. int[] pixels = new int[width * height];
  220. img.getPixels(pixels, 0, width, 0, 0, width, height);
  221. //The arithmetic average of a grayscale image; a threshold
  222. double redSum=0,greenSum=0,blueSun=0;
  223. double total=width*height;
  224. for(int i = 0; i < height; i++) {
  225. for(int j = 0; j < width; j++) {
  226. int grey = pixels[width * i + j];
  227. int red = ((grey & 0x00FF0000 ) >> 16);
  228. int green = ((grey & 0x0000FF00) >> 8);
  229. int blue = (grey & 0x000000FF);
  230. redSum+=red;
  231. greenSum+=green;
  232. blueSun+=blue;
  233. }
  234. }
  235. int m=(int) (redSum/total);
  236. //Conversion monochrome diagram
  237. for(int i = 0; i < height; i++) {
  238. for(int j = 0; j < width; j++) {
  239. int grey = pixels[width * i + j];
  240. int alpha1 = 0xFF << 24;
  241. int red = ((grey & 0x00FF0000 ) >> 16);
  242. int green = ((grey & 0x0000FF00) >> 8);
  243. int blue = (grey & 0x000000FF);
  244. if (red>=m) {
  245. red=green=blue=255;
  246. }else{
  247. red=green=blue=0;
  248. }
  249. grey = alpha1 | (red << 16) | (green << 8) | blue;
  250. pixels[width*i+j]=grey;
  251. }
  252. }
  253. Bitmap mBitmap=Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
  254. mBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
  255. return mBitmap;
  256. }
  257. @ReactMethod
  258. public void pushCutPaper(){
  259. binder.writeDataByYouself(new UiExecute() {
  260. @Override
  261. public void onsucess() {
  262. Log.v(LOG_TAG, "pushCutPaper onsucess");
  263. }
  264. @Override
  265. public void onfailed() {
  266. Log.v(LOG_TAG, "pushCutPaper onfailed");
  267. }
  268. }, new ProcessData() {
  269. @Override
  270. public List<byte[]> processDataBeforeSend() {
  271. List<byte[]> list=new ArrayList<byte[]>();
  272. list.add(DataForSendToPrinterPos80.selectCutPagerModerAndCutPager(66,1));
  273. return list;
  274. }
  275. });
  276. }
  277. }