Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. #import <Foundation/Foundation.h>
  2. #import <UIKit/UIKit.h>
  3. #import "RNBluetoothManager.h"
  4. #import "RNBluetoothEscposPrinter.h"
  5. #import "ColumnSplitedString.h"
  6. #import "PrintColumnBleWriteDelegate.h"
  7. #import "ImageUtils.h"
  8. #import "ZXingObjC/ZXingObjC.h"
  9. #import "PrintImageBleWriteDelegate.h"
  10. @implementation RNBluetoothEscposPrinter
  11. int WIDTH_58 = 384;
  12. int WIDTH_80 = 576;
  13. Byte ESC[] = {0x1b};
  14. //NSInteger ESC = 0x1b;
  15. Byte ESC_FS[] = {0x1c};
  16. //NSInteger FS = 0x1C;
  17. Byte ESC_GS[] = {0x1D};
  18. Byte US[] = {0x1F};
  19. Byte DLE[] = {0x10};
  20. Byte DC4[] = {0x14};
  21. Byte DC1[] = {0x11};
  22. Byte SP[] = {0x20};
  23. Byte NL[] = {0x0A};
  24. Byte FF[] = {0x0C};
  25. Byte PIECE[] = {0xFF};
  26. Byte NUL[] = {0x00};
  27. Byte SIGN[] = {0x21};//!
  28. Byte T[] = {0x74};//t
  29. Byte AND[] ={0x26}; //&
  30. Byte M[] = {0x4d};//M
  31. Byte V[] = {0x56};//V
  32. Byte A[] = {0x61};//a
  33. Byte E[] = {0x45};//E
  34. Byte G[] = {0x47};//G
  35. RCTPromiseResolveBlock pendingResolve;
  36. RCTPromiseRejectBlock pendingReject;
  37. -(id)init {
  38. if (self = [super init]) {
  39. self.deviceWidth = WIDTH_58;
  40. }
  41. return self;
  42. }
  43. - (dispatch_queue_t)methodQueue
  44. {
  45. return dispatch_get_main_queue();
  46. }
  47. + (BOOL)requiresMainQueueSetup
  48. {
  49. return YES;
  50. }
  51. /**
  52. * Exports the constants to javascritp.
  53. **/
  54. - (NSDictionary *)constantsToExport
  55. {
  56. return @{ @"width58":[NSString stringWithFormat:@"%i", WIDTH_58],
  57. @"width80":[NSString stringWithFormat:@"%i", WIDTH_80]};
  58. }
  59. RCT_EXPORT_MODULE(BluetoothEscposPrinter);
  60. /**
  61. * Sets the current deivce width
  62. **/
  63. RCT_EXPORT_METHOD(setWidth:(int) width)
  64. {
  65. self.deviceWidth = width;
  66. }
  67. //public void printerInit(final Promise promise){
  68. // if(sendDataByte(PrinterCommand.POS_Set_PrtInit())){
  69. // promise.resolve(null);
  70. // }else{
  71. // promise.reject("COMMAND_NOT_SEND");
  72. // }
  73. //}
  74. RCT_EXPORT_METHOD(printerInit:(RCTPromiseResolveBlock)resolve
  75. rejecter:(RCTPromiseRejectBlock)reject)
  76. {
  77. if(RNBluetoothManager.isConnected){
  78. NSMutableData *data = [[NSMutableData alloc] init];
  79. Byte at[] = {'@'};
  80. [data appendBytes:ESC length:1];
  81. [data appendBytes:at length:1];
  82. pendingResolve = resolve;
  83. pendingReject = reject;
  84. [RNBluetoothManager writeValue:data withDelegate:self];
  85. }else{
  86. reject(@"COMMAND_NOT_SEND",@"COMMAND_NOT_SEND",nil);
  87. }
  88. }
  89. //{GS, 'L', 0x00 , 0x00 }
  90. // data[2] = (byte) (left % 100);
  91. //data[3] = (byte) (left / 100);
  92. RCT_EXPORT_METHOD(printerLeftSpace:(int) sp
  93. withResolver:(RCTPromiseResolveBlock)resolve
  94. rejecter:(RCTPromiseRejectBlock)reject)
  95. {
  96. if(sp>255 || sp<0){
  97. reject(@"COMMAND_NOT_SEND",@"INVALID_VALUE",nil);
  98. return;
  99. }
  100. if(RNBluetoothManager.isConnected){
  101. NSMutableData *data = [[NSMutableData alloc] init];
  102. Byte left[] = {'L'};
  103. Byte sp_up[] = {(sp%100)};
  104. Byte sp_down[] = {(sp/100)};
  105. [data appendBytes:ESC_GS length:1];
  106. [data appendBytes:left length:1];
  107. [data appendBytes:sp_up length:1];
  108. [data appendBytes:sp_down length:1];
  109. pendingResolve = resolve;
  110. pendingReject = reject;
  111. [RNBluetoothManager writeValue:data withDelegate:self];
  112. }else{
  113. reject(@"COMMAND_NOT_SEND",@"COMMAND_NOT_SEND",nil);
  114. }
  115. }
  116. //{ESC, 45, 0x00 };
  117. //{FS, 45, 0x00 };
  118. RCT_EXPORT_METHOD(printerUnderLine:(int)sp withResolver:(RCTPromiseResolveBlock) resolve
  119. rejecter:(RCTPromiseRejectBlock) reject)
  120. {
  121. if(sp<0 || sp>2){
  122. reject(@"COMMAND_NOT_SEND",@"INVALID_VALUE",nil);
  123. return;
  124. }
  125. if(RNBluetoothManager.isConnected){
  126. NSMutableData *data = [[NSMutableData alloc] init];
  127. Byte under_line[] = {45};
  128. Byte spb[] = {sp};
  129. [data appendBytes:ESC length:1];
  130. [data appendBytes:under_line length:1];
  131. [data appendBytes:spb length:1];
  132. [data appendBytes:ESC_FS length:1];
  133. [data appendBytes:under_line length:1];
  134. [data appendBytes:spb length:1];
  135. pendingResolve = resolve;
  136. pendingReject = reject;
  137. [RNBluetoothManager writeValue:data withDelegate:self];
  138. }else{
  139. reject(@"COMMAND_NOT_SEND",@"COMMAND_NOT_SEND",nil);
  140. }
  141. }
  142. RCT_EXPORT_METHOD(printText:(NSString *) text withOptions:(NSDictionary *) options
  143. resolver:(RCTPromiseResolveBlock) resolve rejecter:(RCTPromiseRejectBlock) reject)
  144. {NSLog(@"printing text...with options: %@",options);
  145. if(!RNBluetoothManager.isConnected){
  146. reject(@"COMMAND_NOT_SEND",@"COMMAND_NOT_SEND",nil);
  147. }else{
  148. @try{
  149. //encoding:'GBK',
  150. //codepage:0,
  151. //widthtimes:0,
  152. //heigthtimes:0,
  153. //fonttype:1
  154. NSString *encodig = [options valueForKey:@"encoding"];
  155. if(!encodig) encodig=@"GBK";
  156. NSInteger codePage = [[options valueForKey:@"codepage"] integerValue];NSLog(@"Got codepage from options: %ld",codePage);
  157. if(!codePage) codePage = 0;
  158. NSInteger widthTimes = [[options valueForKey:@"widthtimes"] integerValue];
  159. if(!widthTimes) widthTimes = 0;
  160. NSInteger heigthTime = [[options valueForKey:@"heigthtimes"] integerValue];
  161. if(!heigthTime) heigthTime =0;
  162. NSInteger fontType = [[options valueForKey:@"fontType"] integerValue];
  163. if(!fontType) fontType = 0;
  164. pendingResolve = resolve;
  165. pendingReject = reject;
  166. [self textPrint:text inEncoding:encodig withCodePage:codePage widthTimes:widthTimes heightTimes:heigthTime fontType:fontType delegate:self];
  167. }
  168. @catch (NSException *e){
  169. NSLog(@"print text exception: %@",e);
  170. reject(e.name.description,e.name.description,nil);
  171. }
  172. }
  173. }
  174. -(NSStringEncoding) toNSEncoding:(NSString *)encoding
  175. {NSLog(@"encoding: %@",encoding);
  176. NSStringEncoding nsEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
  177. if([@"UTF-8" isEqualToString:encoding] || [@"utf-8" isEqualToString:encoding] ){
  178. nsEncoding = NSUTF8StringEncoding;
  179. }
  180. return nsEncoding;
  181. }
  182. -(void) textPrint:(NSString *) text
  183. inEncoding:(NSString *) encoding
  184. withCodePage:(NSInteger) codePage
  185. widthTimes:(NSInteger) widthTimes
  186. heightTimes:(NSInteger) heightTimes
  187. fontType:(NSInteger) fontType
  188. delegate:(NSObject<WriteDataToBleDelegate> *) delegate
  189. {
  190. Byte *intToWidth[] = {0x00, 0x10, 0x20, 0x30};
  191. Byte *intToHeight[] = {0x00, 0x01, 0x02, 0x03};
  192. Byte *multTime[] = {intToWidth[widthTimes],intToHeight[heightTimes]};
  193. NSData *bytes = [text dataUsingEncoding:[self toNSEncoding:encoding]];
  194. NSLog(@"Got bytes length:%lu",[bytes length]);
  195. NSMutableData *toSend = [[NSMutableData alloc] init];
  196. //gsExclamationMark:{GS, '!', 0x00 };
  197. [toSend appendBytes:ESC_GS length:sizeof(ESC_GS)];
  198. [toSend appendBytes:SIGN length:sizeof(SIGN)];
  199. [toSend appendBytes:multTime length:sizeof(multTime)];
  200. //escT: {ESC, 't', 0x00 };
  201. [toSend appendBytes:ESC length:sizeof(ESC)];
  202. [toSend appendBytes:T length:sizeof(T)];
  203. [toSend appendBytes:&codePage length:sizeof(codePage)];NSLog(@"codepage: %lu",codePage);
  204. if(codePage == 0){
  205. //FS_and :{FS, '&' };
  206. [toSend appendBytes:ESC_FS length:sizeof(ESC_FS)];
  207. [toSend appendBytes:AND length:sizeof(AND)];
  208. }else{NSLog(@"{FS,46}");
  209. //FS_dot: {FS, 46 };
  210. NSInteger fourtySix= 46;
  211. [toSend appendBytes:ESC_FS length:sizeof(ESC_FS)];
  212. [toSend appendBytes:&fourtySix length:sizeof(fourtySix)];
  213. }
  214. // escM:{ESC, 'M', 0x00 };
  215. [toSend appendBytes:ESC length:sizeof(ESC)];
  216. [toSend appendBytes:M length:sizeof(M)];
  217. [toSend appendBytes:&fontType length:sizeof(fontType)];
  218. // text data
  219. [toSend appendData:bytes];
  220. //LF
  221. // [toSend appendBytes:&NL length:sizeof(NL)];
  222. NSLog(@"Goting to write text : %@",text);
  223. NSLog(@"With data: %@",toSend);
  224. [RNBluetoothManager writeValue:toSend withDelegate:delegate];
  225. }
  226. RCT_EXPORT_METHOD(rotate:(NSInteger *)rotate
  227. withResolver:(RCTPromiseResolveBlock) resolve rejecter:(RCTPromiseRejectBlock) reject)
  228. {
  229. if(RNBluetoothManager.isConnected){
  230. // //取消/选择90度旋转打印
  231. // public static byte[] ESC_V = new byte[] {ESC, 'V', 0x00 };
  232. NSMutableData *data = [[NSMutableData alloc] init];
  233. Byte rotateBytes[] = {(int)rotate};
  234. [data appendBytes:ESC length:1];
  235. [data appendBytes:V length:1];
  236. [data appendBytes:rotateBytes length:1];
  237. pendingReject = reject;
  238. pendingResolve = resolve;
  239. [RNBluetoothManager writeValue:data withDelegate:self];
  240. }else{
  241. reject(@"COMMAND_NOT_SEND",@"COMMAND_NOT_SEND",nil);
  242. }
  243. // if(sendDataByte(PrinterCommand.POS_Set_Rotate(rotate))){
  244. // promise.resolve(null);
  245. // }else{
  246. // promise.reject("COMMAND_NOT_SEND");
  247. // }
  248. }
  249. RCT_EXPORT_METHOD(printerAlign:(NSInteger *) align
  250. withResolver:(RCTPromiseResolveBlock) resolve rejecter:(RCTPromiseRejectBlock) reject)
  251. {
  252. if(RNBluetoothManager.isConnected){
  253. //if ((align < 0 || align > 2) && (align < 48 || align > 50)) return null;
  254. if((align < 0 || align > 2) && (align < 48 || align > 50)){
  255. reject(@"INVALD_PARAMETERS",@"INVALD_PARAMETERS",nil);
  256. }else{
  257. //{ESC, 'a', 0x00 }
  258. NSMutableData *toSend = [[NSMutableData alloc] init];
  259. [toSend appendBytes:ESC length:sizeof(ESC)];
  260. [toSend appendBytes:A length:sizeof(A)];
  261. [toSend appendBytes:&align length:sizeof(align)];
  262. pendingReject =reject;
  263. pendingResolve =resolve;
  264. [RNBluetoothManager writeValue:toSend withDelegate:self];
  265. }
  266. }else{
  267. reject(@"COMMAND_NOT_SEND",@"COMMAND_NOT_SEND",nil);
  268. }
  269. }
  270. RCT_EXPORT_METHOD(printColumn:(NSArray *)columnWidths
  271. withAligns:(NSArray *) columnAligns
  272. texts:(NSArray *) columnTexts
  273. options:(NSDictionary *)options
  274. resolver:(RCTPromiseResolveBlock) resolve
  275. rejecter:(RCTPromiseRejectBlock) reject)
  276. {
  277. if(!RNBluetoothManager.isConnected){
  278. reject(@"COMMAND_NOT_SEND",@"COMMAND_NOT_SEND",nil);
  279. }else{
  280. @try{
  281. NSString *encodig = [options valueForKey:@"encoding"];
  282. if(!encodig) encodig=@"GBK";
  283. NSInteger codePage = [[options valueForKey:@"codepage"] integerValue];NSLog(@"Got codepage from options: %ld",codePage);
  284. if(!codePage) codePage = 0;
  285. NSInteger widthTimes = [[options valueForKey:@"widthtimes"] integerValue];
  286. if(!widthTimes) widthTimes = 0;
  287. NSInteger heigthTime = [[options valueForKey:@"heigthtimes"] integerValue];
  288. if(!heigthTime) heigthTime =0;
  289. NSInteger fontType = [[options valueForKey:@"fontType"] integerValue];
  290. if(!fontType) fontType = 0;
  291. /**
  292. * [column1-1,
  293. * column1-2,
  294. * column1-3 ... column1-n]
  295. * ,
  296. * [column2-1,
  297. * column2-2,
  298. * column2-3 ... column2-n]
  299. *
  300. * ...
  301. *
  302. */
  303. NSMutableArray *table =[[NSMutableArray alloc] init];
  304. /**splits the column text to few rows and applies the alignment **/
  305. int padding = 1;
  306. for(int i=0;i< [columnWidths count];i++){
  307. NSInteger width =[[columnWidths objectAtIndex:i ] integerValue] - padding;//1 char padding
  308. NSString *text = [columnTexts objectAtIndex:i]; //String.copyValueOf(columnTexts.getString(i).toCharArray());
  309. NSLog(@"Text in column: %@",text);
  310. NSMutableArray<ColumnSplitedString *> *splited = [[NSMutableArray alloc] init];
  311. //List<ColumnSplitedString> splited = new ArrayList<ColumnSplitedString>();
  312. int shorter = 0;
  313. int counter = 0;
  314. NSMutableString *temp = [[NSMutableString alloc] init];
  315. for(int c=0;c<[text length];c++){
  316. unichar ch = [text characterAtIndex:c];
  317. int l = (ch>= 0x4e00 && ch <= 0x9fff)?2:1;
  318. if (l==2){
  319. shorter=shorter+1;
  320. }
  321. [temp appendString:[text substringWithRange:NSMakeRange(c, 1)]];
  322. if(counter+l<width){
  323. counter = counter+l;
  324. }else{
  325. ColumnSplitedString *css = [[ColumnSplitedString alloc] init];
  326. css.str = temp;
  327. css.shorter = shorter;
  328. [splited addObject:css];
  329. temp = [[NSMutableString alloc] init];
  330. counter=0;
  331. shorter=0;
  332. }
  333. }
  334. if([temp length]>0) {
  335. ColumnSplitedString *css = [[ColumnSplitedString alloc] init];
  336. css.str = temp;
  337. css.shorter = shorter;
  338. [splited addObject:css];
  339. }
  340. NSInteger align =[[columnAligns objectAtIndex:i] integerValue];
  341. NSMutableArray *formated = [[NSMutableArray alloc] init];
  342. for(ColumnSplitedString *s in splited){
  343. NSMutableString *empty = [[NSMutableString alloc] init];
  344. for(int w=0;w<(width+padding-s.shorter);w++){
  345. [empty appendString:@" "];
  346. }
  347. int startIdx = 0;
  348. NSString *ss = s.str;
  349. if(align == 1 && [ss length]<(width-s.shorter)){
  350. startIdx = (int)(width-s.shorter-[ss length])/2;
  351. if(startIdx+[ss length]>width-s.shorter){
  352. startIdx--;
  353. }
  354. if(startIdx<0){
  355. startIdx=0;
  356. }
  357. }else if(align==2 && [ss length]<(width-s.shorter)){
  358. startIdx =(int)(width - s.shorter-[ss length]);
  359. }
  360. NSInteger length =[ss length];
  361. // if(length+startIdx>[empty length]){
  362. // length = [empty length]-startIdx;
  363. // }
  364. NSLog(@"empty(length: %lu) replace from %d length %lu with str:%@)",[empty length],startIdx,length,ss);
  365. [empty replaceCharactersInRange:NSMakeRange(startIdx, length) withString:ss];
  366. [formated addObject:empty];
  367. }
  368. [table addObject:formated];
  369. }
  370. /** try to find the max row count of the table **/
  371. NSInteger maxRowCount = 0;
  372. for(int i=0;i<[table count]/*column count*/;i++){
  373. NSArray *rows = [table objectAtIndex:i]; // row data in current column
  374. if([rows count]>maxRowCount){
  375. maxRowCount = [rows count];// try to find the max row count;
  376. }
  377. }
  378. /** loop table again to fill the rows **/
  379. NSMutableArray<NSMutableString *> *rowsToPrint = [[NSMutableArray alloc] init];
  380. for(int column=0;column<[table count]/*column count*/;column++){
  381. NSArray *rows = [table objectAtIndex:column]; // row data in current column
  382. for(int row=0;row<maxRowCount;row++){
  383. if([rowsToPrint count]<=row || [rowsToPrint objectAtIndex:row] ==nil){
  384. [rowsToPrint setObject:[[NSMutableString alloc] init] atIndexedSubscript:row];
  385. }
  386. if(row<[rows count]){
  387. //got the row of this column
  388. [(NSMutableString *)[rowsToPrint objectAtIndex:row] appendString:[rows objectAtIndex:row]];//.append(rows.get(row));
  389. }else{
  390. NSInteger w = [[columnWidths objectAtIndex:column] integerValue]; //columnWidths.getInt(column);
  391. NSMutableString *empty = [[NSMutableString alloc] init];
  392. for(int i=0;i<w;i++){
  393. [empty appendString:@" "]; //empty.append(" ");
  394. }
  395. [(NSMutableString *)[rowsToPrint objectAtIndex:row] appendString:empty];//Append spaces to ensure the format
  396. }
  397. }
  398. }
  399. /** loops the rows and print **/
  400. PrintColumnBleWriteDelegate *delegate = [[PrintColumnBleWriteDelegate alloc] init];
  401. delegate.now = 0;
  402. delegate.error = false;
  403. delegate.pendingReject = reject;
  404. delegate.pendingResolve =resolve;
  405. delegate.canceled = false;
  406. delegate.encodig = encodig;
  407. delegate.widthTimes = widthTimes;
  408. delegate.heightTimes = heigthTime;
  409. delegate.fontType = fontType;
  410. delegate.codePage = codePage;
  411. delegate.printer = self;
  412. [delegate printColumn:rowsToPrint withMaxcount:maxRowCount];
  413. }
  414. @catch(NSException *e){
  415. NSLog(@"print text exception: %@",[e callStackSymbols]);
  416. reject(e.name.description,e.name.description,nil);
  417. }
  418. }
  419. }
  420. RCT_EXPORT_METHOD(setBlob:(NSInteger) sp
  421. withResolver:(RCTPromiseResolveBlock) resolve
  422. rejecter:(RCTPromiseRejectBlock) reject)
  423. {
  424. //\\ //选择/取消加粗指令
  425. // public static byte[] ESC_G = new byte[] {ESC, 'G', 0x00 };
  426. // public static byte[] ESC_E = new byte[] {ESC, 'E', 0x00 };
  427. //E+G
  428. NSMutableData *toSend = [[NSMutableData alloc] init];
  429. [toSend appendBytes:&ESC length:sizeof(ESC)];
  430. [toSend appendBytes:&G length:sizeof(G)];
  431. [toSend appendBytes:&sp length:sizeof(sp)];
  432. [toSend appendBytes:&ESC length:sizeof(ESC)];
  433. [toSend appendBytes:&E length:sizeof(E)];
  434. [toSend appendBytes:&sp length:sizeof(sp)];
  435. pendingReject =reject;
  436. pendingResolve = resolve;
  437. [RNBluetoothManager writeValue:toSend withDelegate:self];
  438. }
  439. RCT_EXPORT_METHOD(printPic:(NSString *) base64encodeStr withOptions:(NSDictionary *) options
  440. resolver:(RCTPromiseResolveBlock) resolve
  441. rejecter:(RCTPromiseRejectBlock) reject)
  442. {
  443. if(RNBluetoothManager.isConnected){
  444. @try{
  445. NSInteger nWidth = [[options valueForKey:@"width"] integerValue];
  446. if(!nWidth) nWidth = _deviceWidth;
  447. //TODO:need to handel param "left" in the options.
  448. NSInteger paddingLeft = [[options valueForKey:@"left"] integerValue];
  449. if(!paddingLeft) paddingLeft = 0;
  450. NSData *decoded = [[NSData alloc] initWithBase64EncodedString:base64encodeStr options:0 ];
  451. UIImage *srcImage = [[UIImage alloc] initWithData:decoded scale:1];
  452. NSData *jpgData = UIImageJPEGRepresentation(srcImage, 1);
  453. UIImage *jpgImage = [[UIImage alloc] initWithData:jpgData];
  454. //mBitmap.getHeight() * width / mBitmap.getWidth();
  455. NSInteger imgHeight = jpgImage.size.height;
  456. NSInteger imagWidth = jpgImage.size.width;
  457. NSInteger width = nWidth;//((int)(((nWidth*0.86)+7)/8))*8-7;
  458. CGSize size = CGSizeMake(width, imgHeight*width/imagWidth);
  459. UIImage *scaled = [ImageUtils imageWithImage:jpgImage scaledToFillSize:size];
  460. if(paddingLeft>0){
  461. scaled = [ImageUtils imagePadLeft:paddingLeft withSource:scaled];
  462. size =[scaled size];
  463. }
  464. unsigned char * graImage = [ImageUtils imageToGreyImage:scaled];
  465. unsigned char * formatedData = [ImageUtils format_K_threshold:graImage width:size.width height:size.height];
  466. NSData *dataToPrint = [ImageUtils eachLinePixToCmd:formatedData nWidth:size.width nHeight:size.height nMode:0];
  467. PrintImageBleWriteDelegate *delegate = [[PrintImageBleWriteDelegate alloc] init];
  468. delegate.pendingResolve = resolve;
  469. delegate.pendingReject = reject;
  470. delegate.width = width;
  471. delegate.toPrint = dataToPrint;
  472. delegate.now = 0;
  473. [delegate print];
  474. }
  475. @catch(NSException *e){
  476. NSLog(@"ERROR IN PRINTING IMG: %@",[e callStackSymbols]);
  477. reject(@"COMMAND_NOT_SEND",@"COMMAND_NOT_SEND",nil);
  478. }
  479. }else{
  480. reject(@"COMMAND_NOT_SEND",@"COMMAND_NOT_SEND",nil);
  481. }
  482. }
  483. RCT_EXPORT_METHOD(printQRCode:(NSString *)content
  484. withSize:(NSInteger) size
  485. correctionLevel:(NSInteger) correctionLevel
  486. andResolver:(RCTPromiseResolveBlock) resolve
  487. rejecter:(RCTPromiseRejectBlock) reject)
  488. {
  489. NSLog(@"QRCODE TO PRINT: %@",content);
  490. NSError *error = nil;
  491. ZXEncodeHints *hints = [ZXEncodeHints hints];
  492. hints.encoding=NSUTF8StringEncoding;
  493. hints.margin=0;
  494. hints.errorCorrectionLevel = [self findCorrectionLevel:correctionLevel];
  495. ZXMultiFormatWriter *writer = [ZXMultiFormatWriter writer];
  496. ZXBitMatrix *result = [writer encode:content
  497. format:kBarcodeFormatQRCode
  498. width:(int)size
  499. height:(int)size
  500. hints:hints
  501. error:&error];
  502. if(error || !result){
  503. reject(@"ERROR_IN_CREATE_QRCODE",@"ERROR_IN_CREATE_QRCODE",nil);
  504. }else{
  505. CGImageRef image = [[ZXImage imageWithMatrix:result] cgimage];
  506. uint8_t * graImage = [ImageUtils imageToGreyImage:[UIImage imageWithCGImage:image]];
  507. unsigned char * formatedData = [ImageUtils format_K_threshold:graImage width:size height:size];
  508. NSData *dataToPrint = [ImageUtils eachLinePixToCmd:formatedData nWidth:size nHeight:size nMode:0];
  509. PrintImageBleWriteDelegate *delegate = [[PrintImageBleWriteDelegate alloc] init];
  510. delegate.pendingResolve=resolve;
  511. delegate.pendingReject = reject;
  512. delegate.width = size;
  513. delegate.toPrint = dataToPrint;
  514. delegate.now = 0;
  515. [delegate print];
  516. }
  517. }
  518. RCT_EXPORT_METHOD(printBarCode:(NSString *) str withType:(NSInteger)
  519. nType width:(NSInteger) nWidth heigth:(NSInteger) nHeight
  520. hriFontType:(NSInteger) nHriFontType hriFontPosition:(NSInteger) nHriFontPosition
  521. andResolver:(RCTPromiseResolveBlock) resolve
  522. rejecter:(RCTPromiseRejectBlock) reject)
  523. {
  524. if (nType < 0x41 | nType > 0x49 | nWidth < 2 | nWidth > 6
  525. | nHeight < 1 | nHeight > 255 | (!str||[str length]<1))
  526. {
  527. reject(@"INVALID_PARAMETER",@"INVALID_PARAMETER",nil);
  528. return;
  529. }
  530. NSData *conentData = [str dataUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)];
  531. NSMutableData *toPrint = [[NSMutableData alloc] init];
  532. int8_t * command = malloc(16);
  533. command[0] = 29 ;//GS
  534. command[1] = 119;//W
  535. command[2] = nWidth;
  536. command[3] = 29;//GS
  537. command[4] = 104;//h
  538. command[5] = nHeight;
  539. command[6] = 29;//GS
  540. command[7] = 102;//f
  541. command[8] = (nHriFontType & 0x01);
  542. command[9] = 29;//GS
  543. command[10] = 72;//H
  544. command[11] = (nHriFontPosition & 0x03);
  545. command[12] = 29;//GS
  546. command[13] = 107;//k
  547. command[14] = nType;
  548. command[15] = [conentData length];
  549. [toPrint appendBytes:command length:16];
  550. [toPrint appendData:conentData];
  551. pendingReject = reject;
  552. pendingResolve = resolve;
  553. [RNBluetoothManager writeValue:toPrint withDelegate:self];
  554. }
  555. // L:1,
  556. //M:0,
  557. //Q:3,
  558. //H:2
  559. -(ZXQRCodeErrorCorrectionLevel *)findCorrectionLevel:(NSInteger)level
  560. {
  561. switch (level) {
  562. case 1:
  563. return [[ZXQRCodeErrorCorrectionLevel alloc] initWithOrdinal:0 bits:0x01 name:@"L"];
  564. break;
  565. case 2:
  566. return [[ZXQRCodeErrorCorrectionLevel alloc] initWithOrdinal:3 bits:0x02 name:@"H"];
  567. case 3:
  568. return [[ZXQRCodeErrorCorrectionLevel alloc] initWithOrdinal:2 bits:0x03 name:@"Q"];
  569. default:
  570. return [[ZXQRCodeErrorCorrectionLevel alloc] initWithOrdinal:1 bits:0x00 name:@"M"];
  571. break;
  572. }
  573. }
  574. - (void) didWriteDataToBle: (BOOL)success{
  575. if(success){
  576. pendingResolve(nil);
  577. }else{NSLog(@"REJECT<REJECT<REJECT<REJECT<REJECT<");
  578. pendingReject(@"COMMAND_NOT_SEND",@"COMMAND_NOT_SEND",nil);
  579. }
  580. pendingReject = nil;
  581. pendingResolve = nil;
  582. [NSThread sleepForTimeInterval:0.05f];//slow down
  583. }
  584. @end