Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

RNBluetoothTscPrinter.m 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // RNBluetoothTscPrinter.m
  3. // RNBluetoothEscposPrinter
  4. //
  5. // Created by januslo on 2018/10/1.
  6. // Copyright © 2018年 Facebook. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "RNBluetoothTscPrinter.h"
  10. #import "RNTscCommand.h"
  11. #import "RNBluetoothManager.h"
  12. @implementation RNBluetoothTscPrinter
  13. NSData *toPrint;
  14. RCTPromiseRejectBlock _pendingReject;
  15. RCTPromiseResolveBlock _pendingResolve;
  16. NSInteger now;
  17. - (dispatch_queue_t)methodQueue
  18. {
  19. return dispatch_get_main_queue();
  20. }
  21. + (BOOL)requiresMainQueueSetup
  22. {
  23. return YES;
  24. }
  25. RCT_EXPORT_MODULE(BluetoothTscPrinter);
  26. //printLabel(final ReadableMap options, final Promise promise)
  27. RCT_EXPORT_METHOD(printLabel:(NSDictionary *) options withResolve:(RCTPromiseResolveBlock)resolve
  28. rejecter:(RCTPromiseRejectBlock)reject)
  29. {
  30. NSInteger width = [[options valueForKey:@"width"] integerValue];
  31. NSInteger height = [[options valueForKey:@"height"] integerValue];
  32. NSInteger gap = [[options valueForKey:@"gap"] integerValue];
  33. NSInteger home = [[options valueForKey:@"home"] integerValue];
  34. NSString *tear = [options valueForKey:@"tear"];
  35. if(!tear || ![@"ON" isEqualToString:tear]) tear = @"OFF";
  36. NSArray *texts = [options objectForKey:@"text"];
  37. NSArray *qrCodes = [options objectForKey:@"qrcode"];
  38. NSArray *barCodes = [options objectForKey:@"barcode"];
  39. NSArray *images = [options objectForKey:@"image"];
  40. NSArray *reverses = [options objectForKey:@"revers"];
  41. NSInteger direction = [[options valueForKey:@"direction"] integerValue];
  42. NSInteger density = [[options valueForKey:@"density"] integerValue];
  43. NSArray* reference = [options objectForKey:@"reference"];
  44. NSInteger sound = [[options valueForKey:@"sound"] integerValue];
  45. NSInteger speed = [[options valueForKey:@"speed"] integerValue];
  46. RNTscCommand *tsc = [[RNTscCommand alloc] init];
  47. if(speed){
  48. [tsc addSpeed:[tsc findSpeedValue:speed]];
  49. }
  50. if(density){
  51. [tsc addDensity:density];
  52. }
  53. [tsc addSize:width height:height];
  54. [tsc addGap:gap];
  55. [tsc addDirection:direction];
  56. if(reference && [reference count] ==2){
  57. NSInteger x = [[reference objectAtIndex:0] integerValue];
  58. NSInteger y = [[reference objectAtIndex:1] integerValue];
  59. NSLog(@"refernce %ld y:%ld ",x,y);
  60. [tsc addReference:x y:y];
  61. }else{
  62. [tsc addReference:0 y:0];
  63. }
  64. [tsc addTear:tear];
  65. if(home && home == 1){
  66. [tsc addBackFeed:16];
  67. [tsc addHome];
  68. }
  69. [tsc addCls];
  70. //Add Texts
  71. for(int i=0; texts && i<[texts count];i++){
  72. NSDictionary * text = [texts objectAtIndex:i];
  73. NSString *t = [text valueForKey:@"text"];
  74. NSInteger x = [[text valueForKey:@"x"] integerValue];
  75. NSInteger y = [[text valueForKey:@"y"] integerValue];
  76. NSString *fontType = [text valueForKey:@"fonttype"];
  77. NSInteger rotation = [[text valueForKey:@"rotation"] integerValue];
  78. NSInteger xscal = [[text valueForKey:@"xscal"] integerValue];
  79. NSInteger yscal = [[text valueForKey:@"yscal"] integerValue];
  80. Boolean bold = [[text valueForKey:@"bold"] boolValue];
  81. [tsc addText:x y:y fontType:fontType rotation:rotation xscal:xscal yscal:yscal text:t];
  82. if(bold){
  83. [tsc addText:x+1 y:y fontType:fontType
  84. rotation:rotation xscal:xscal yscal:yscal text:t];
  85. [tsc addText:x y:y+1 fontType:fontType
  86. rotation:rotation xscal:xscal yscal:yscal text:t];
  87. }
  88. }
  89. //images
  90. for (int i = 0; images && i < [images count]; i++) {
  91. NSDictionary *img = [images objectAtIndex:i];
  92. NSInteger x = [[img valueForKey:@"x"] integerValue];
  93. NSInteger y = [[img valueForKey:@"y"] integerValue];
  94. NSInteger imgWidth = [[img valueForKey:@"width"] integerValue];
  95. NSInteger mode = [[img valueForKey:@"mode"] integerValue];
  96. NSString *image = [img valueForKey:@"image"];
  97. NSData *imageData = [[NSData alloc] initWithBase64EncodedString:image options:0];
  98. UIImage *uiImage = [[UIImage alloc] initWithData:imageData];
  99. [tsc addBitmap:x y:y bitmapMode:mode width:imgWidth bitmap:uiImage];
  100. }
  101. //QRCode
  102. for (int i = 0; qrCodes && i < [qrCodes count]; i++) {
  103. NSDictionary *qr = [qrCodes objectAtIndex:i];
  104. NSInteger x = [[qr valueForKey:@"x"] integerValue];
  105. NSInteger y = [[qr valueForKey:@"y"] integerValue];
  106. NSInteger qrWidth = [[qr valueForKey:@"width"] integerValue];
  107. NSString *level = [qr valueForKey:@"level"];
  108. if(!level)level = @"M";
  109. NSInteger rotation = [[qr valueForKey:@"rotation"] integerValue];
  110. NSString *code = [qr valueForKey:@"code"];
  111. [tsc addQRCode:x y:y errorCorrectionLevel:level width:qrWidth rotation:rotation code:code];
  112. }
  113. //BarCode
  114. for (int i = 0; barCodes && i < [barCodes count]; i++) {
  115. NSDictionary *bar = [barCodes objectAtIndex:i];
  116. NSInteger x = [[bar valueForKey:@"x"] integerValue];
  117. NSInteger y = [[bar valueForKey:@"y"] integerValue];
  118. NSInteger barWide =[[bar valueForKey:@"wide"] integerValue];
  119. if(!barWide) barWide = 2;
  120. NSInteger barHeight = [[bar valueForKey:@"height"] integerValue];
  121. NSInteger narrow = [[bar valueForKey:@"narrow"] integerValue];
  122. if(!narrow) narrow = 2;
  123. NSInteger rotation = [[bar valueForKey:@"rotation"] integerValue];
  124. NSString *code = [bar valueForKey:@"code"];
  125. NSString *type = [bar valueForKey:@"type"];
  126. NSInteger readable = [[bar valueForKey:@"readable"] integerValue];
  127. [tsc add1DBarcode:x y:y barcodeType:type height:barHeight wide:barWide narrow:narrow readable:readable rotation:rotation content:code];
  128. }
  129. for(int i=0; reverses&& i < [reverses count]; i++){
  130. NSDictionary *area = [reverses objectAtIndex:i];
  131. NSInteger ax = [[area valueForKey:@"x"] integerValue];
  132. NSInteger ay = [[area valueForKey:@"y"] integerValue];
  133. NSInteger aWidth = [[area valueForKey:@"width"] integerValue];
  134. NSInteger aHeight = [[area valueForKey:@"height"] integerValue];
  135. [tsc addReverse:ax y:ay xwidth:aWidth yheigth:aHeight];
  136. }
  137. [tsc addPrint:1 n:1];
  138. if (sound) {
  139. [tsc addSound:2 interval:100];
  140. }
  141. _pendingReject = reject;
  142. _pendingResolve = resolve;
  143. toPrint = tsc.command;
  144. now = 0;
  145. [RNBluetoothManager writeValue:toPrint withDelegate:self];
  146. }
  147. - (void) didWriteDataToBle: (BOOL)success{
  148. if(success){
  149. if(_pendingResolve){
  150. _pendingResolve(nil);
  151. }
  152. }else if(_pendingReject){
  153. _pendingReject(@"PRINT_ERROR",@"PRINT_ERROR",nil);
  154. }
  155. }
  156. @end