react-native-bluetooth-escpos-printer-fork 0.0.21 → 0.0.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ios/ImageUtils.m +17 -42
- package/ios/PrintImageBleWriteDelegate.m +2 -11
- package/ios/RNBluetoothEscposPrinter.m +17 -21
- package/package.json +1 -1
package/ios/ImageUtils.m
CHANGED
|
@@ -190,24 +190,12 @@ int p6[] = { 0, 0x02 };
|
|
|
190
190
|
**/
|
|
191
191
|
+ (NSData *)eachLinePixToCmd:(unsigned char *)src nWidth:(NSInteger) nWidth nHeight:(NSInteger) nHeight nMode:(NSInteger) nMode
|
|
192
192
|
{
|
|
193
|
+
NSLog(@"SIZE OF SRC: %lu",sizeof(&src));
|
|
193
194
|
NSInteger nBytesPerLine = (int)nWidth/8;
|
|
194
195
|
unsigned char * data = malloc(nHeight*(8+nBytesPerLine));
|
|
196
|
+
// const char* srcData = (const char*)[src bytes];
|
|
195
197
|
NSInteger k = 0;
|
|
196
|
-
|
|
197
|
-
// Log first few lines to see actual pixel distribution
|
|
198
|
-
int totalBlackPixelsInFirstLines = 0;
|
|
199
|
-
for(int line = 0; line < MIN(10, nHeight); line++) {
|
|
200
|
-
int blackInLine = 0;
|
|
201
|
-
for(int x = 0; x < nWidth; x++) {
|
|
202
|
-
if(src[line * nWidth + x] == 1) blackInLine++;
|
|
203
|
-
}
|
|
204
|
-
if(blackInLine > 0) {
|
|
205
|
-
NSLog(@"Line %d: %d/%d pixels are black", line, blackInLine, (int)nWidth);
|
|
206
|
-
}
|
|
207
|
-
totalBlackPixelsInFirstLines += blackInLine;
|
|
208
|
-
}
|
|
209
|
-
NSLog(@"First 10 lines: %d total black pixels", totalBlackPixelsInFirstLines);
|
|
210
|
-
|
|
198
|
+
// NSMutableString *toLog = [[NSMutableString alloc] init];
|
|
211
199
|
for(int i=0;i<nHeight;i++){
|
|
212
200
|
NSInteger var10 = i*(8+nBytesPerLine);
|
|
213
201
|
//GS v 0 m xL xH yL yH d1....dk 打印光栅位图
|
|
@@ -219,31 +207,19 @@ int p6[] = { 0, 0x02 };
|
|
|
219
207
|
data[var10 + 5] = (unsigned char)(nBytesPerLine / 256);//xH
|
|
220
208
|
data[var10 + 6] = 1;//yL
|
|
221
209
|
data[var10 + 7] = 0;//yH
|
|
210
|
+
// for(int l=0;l<8;l++){
|
|
211
|
+
// NSInteger d =data[var10 + l];
|
|
212
|
+
// [toLog appendFormat:@"%ld,",(long)d];
|
|
213
|
+
// }
|
|
222
214
|
|
|
223
215
|
for (int j = 0; j < nBytesPerLine; ++j) {
|
|
224
216
|
data[var10 + 8 + j] = (int) (p0[src[k]] + p1[src[k + 1]] + p2[src[k + 2]] + p3[src[k + 3]] + p4[src[k + 4]] + p5[src[k + 5]] + p6[src[k + 6]] + src[k + 7]);
|
|
225
217
|
k =k+8;
|
|
218
|
+
// [toLog appendFormat:@"%ld,",(long)data[var10+8+j]];
|
|
226
219
|
}
|
|
220
|
+
// [toLog appendString:@"\n\r"];
|
|
227
221
|
}
|
|
228
|
-
|
|
229
|
-
// Find and log first line with actual black pixels
|
|
230
|
-
for(int i = 0; i < nHeight; i++) {
|
|
231
|
-
NSInteger var10 = i*(8+nBytesPerLine);
|
|
232
|
-
BOOL hasBlack = NO;
|
|
233
|
-
for(int j = 0; j < MIN(20, nBytesPerLine); j++) {
|
|
234
|
-
if(data[var10 + 8 + j] != 0) {
|
|
235
|
-
hasBlack = YES;
|
|
236
|
-
break;
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
if(hasBlack) {
|
|
240
|
-
NSLog(@"First line with black pixels is line %d, first 10 bytes: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
|
|
241
|
-
i, data[var10+8], data[var10+9], data[var10+10], data[var10+11], data[var10+12],
|
|
242
|
-
data[var10+13], data[var10+14], data[var10+15], data[var10+16], data[var10+17]);
|
|
243
|
-
break;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
222
|
+
// NSLog(@"line datas: %@",toLog);
|
|
247
223
|
return [NSData dataWithBytes:data length:nHeight*(8+nBytesPerLine)];
|
|
248
224
|
}
|
|
249
225
|
|
|
@@ -266,25 +242,24 @@ int p6[] = { 0, 0x02 };
|
|
|
266
242
|
}
|
|
267
243
|
|
|
268
244
|
int grayave = graytotal / ysize / xsize;
|
|
269
|
-
NSLog(@"Threshold: average gray = %d (pixels lighter than this become white)", grayave);
|
|
270
|
-
|
|
271
245
|
k = 0;
|
|
272
|
-
|
|
246
|
+
// NSMutableString *logStr = [[NSMutableString alloc]init];
|
|
247
|
+
// int oneCount = 0;
|
|
273
248
|
for(i = 0; i < ysize; ++i) {
|
|
274
249
|
for(j = 0; j < xsize; ++j) {
|
|
275
250
|
gray = orgpixels[k] & 255;
|
|
276
251
|
if(gray > grayave) {
|
|
277
|
-
despixels[k] = 0;
|
|
252
|
+
despixels[k] = 0;
|
|
278
253
|
} else {
|
|
279
|
-
despixels[k] = 1;
|
|
280
|
-
oneCount++;
|
|
254
|
+
despixels[k] = 1;
|
|
255
|
+
// oneCount++;
|
|
281
256
|
}
|
|
282
257
|
|
|
283
258
|
++k;
|
|
259
|
+
// [logStr appendFormat:@"%d,",despixels[k]];
|
|
284
260
|
}
|
|
285
261
|
}
|
|
286
|
-
|
|
287
|
-
oneCount, (int)(xsize*ysize), (oneCount * 100.0 / (xsize*ysize)));
|
|
262
|
+
// NSLog(@"despixels [with 1 count:%d]: %@",oneCount,logStr);
|
|
288
263
|
return despixels;
|
|
289
264
|
}
|
|
290
265
|
+(NSData *)pixToTscCmd:(uint8_t *)src width:(NSInteger) width
|
|
@@ -42,20 +42,11 @@
|
|
|
42
42
|
-(void) print
|
|
43
43
|
{
|
|
44
44
|
@synchronized (self) {
|
|
45
|
-
|
|
46
|
-
NSInteger sizePerLine = 8 + (int)(_width/8);
|
|
45
|
+
NSInteger sizePerLine = (int)(_width/8);
|
|
47
46
|
NSData *subData = [_toPrint subdataWithRange:NSMakeRange(_now, sizePerLine)];
|
|
48
|
-
|
|
49
|
-
// Log first line to debug
|
|
50
|
-
if(_now == 0) {
|
|
51
|
-
NSUInteger logLen = MIN(20, [subData length]);
|
|
52
|
-
NSData *logData = [subData subdataWithRange:NSMakeRange(0, logLen)];
|
|
53
|
-
NSLog(@"First image line (%lu bytes): %@", (unsigned long)[subData length], logData);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
47
|
[RNBluetoothManager writeValue:subData withDelegate:self];
|
|
57
48
|
_now = _now+sizePerLine;
|
|
58
|
-
[NSThread sleepForTimeInterval:0.
|
|
49
|
+
[NSThread sleepForTimeInterval:0.01f];
|
|
59
50
|
}
|
|
60
51
|
}
|
|
61
52
|
@end
|
|
@@ -488,8 +488,7 @@ RCT_EXPORT_METHOD(printPic:(NSString *) base64encodeStr withOptions:(NSDictionar
|
|
|
488
488
|
//mBitmap.getHeight() * width / mBitmap.getWidth();
|
|
489
489
|
NSInteger imgHeight = jpgImage.size.height;
|
|
490
490
|
NSInteger imagWidth = jpgImage.size.width;
|
|
491
|
-
|
|
492
|
-
NSInteger width = ((nWidth + 7) / 8) * 8;
|
|
491
|
+
NSInteger width = nWidth;//((int)(((nWidth*0.86)+7)/8))*8-7;
|
|
493
492
|
CGSize size = CGSizeMake(width, imgHeight*width/imagWidth);
|
|
494
493
|
UIImage *scaled = [ImageUtils imageWithImage:jpgImage scaledToFillSize:size];
|
|
495
494
|
if(paddingLeft>0){
|
|
@@ -498,16 +497,8 @@ RCT_EXPORT_METHOD(printPic:(NSString *) base64encodeStr withOptions:(NSDictionar
|
|
|
498
497
|
}
|
|
499
498
|
|
|
500
499
|
unsigned char * graImage = [ImageUtils imageToGreyImage:scaled];
|
|
501
|
-
NSLog(@"Grayscale conversion complete");
|
|
502
|
-
|
|
503
500
|
unsigned char * formatedData = [ImageUtils format_K_threshold:graImage width:size.width height:size.height];
|
|
504
|
-
NSLog(@"Thresholding complete, generating ESC/POS commands");
|
|
505
|
-
|
|
506
501
|
NSData *dataToPrint = [ImageUtils eachLinePixToCmd:formatedData nWidth:size.width nHeight:size.height nMode:0];
|
|
507
|
-
NSLog(@"Image print: %lux%lu pixels, %lu bytes total, %lu bytes per line",
|
|
508
|
-
(unsigned long)size.width, (unsigned long)size.height,
|
|
509
|
-
(unsigned long)[dataToPrint length], (unsigned long)(8 + size.width/8));
|
|
510
|
-
|
|
511
502
|
PrintImageBleWriteDelegate *delegate = [[PrintImageBleWriteDelegate alloc] init];
|
|
512
503
|
delegate.pendingResolve = resolve;
|
|
513
504
|
delegate.pendingReject = reject;
|
|
@@ -552,9 +543,6 @@ RCT_EXPORT_METHOD(printQRCode:(NSString *)content
|
|
|
552
543
|
uint8_t * graImage = [ImageUtils imageToGreyImage:[UIImage imageWithCGImage:image]];
|
|
553
544
|
unsigned char * formatedData = [ImageUtils format_K_threshold:graImage width:size height:size];
|
|
554
545
|
NSData *dataToPrint = [ImageUtils eachLinePixToCmd:formatedData nWidth:size nHeight:size nMode:0];
|
|
555
|
-
NSLog(@"QR code print: %lux%lu pixels, %lu bytes total, %lu bytes per line",
|
|
556
|
-
(unsigned long)size, (unsigned long)size,
|
|
557
|
-
(unsigned long)[dataToPrint length], (unsigned long)(8 + size/8));
|
|
558
546
|
PrintImageBleWriteDelegate *delegate = [[PrintImageBleWriteDelegate alloc] init];
|
|
559
547
|
delegate.pendingResolve=resolve;
|
|
560
548
|
delegate.pendingReject = reject;
|
|
@@ -572,7 +560,7 @@ RCT_EXPORT_METHOD(printEscPosCommands:(NSString *)base64encodeStr
|
|
|
572
560
|
if(RNBluetoothManager.isConnected){
|
|
573
561
|
@try{
|
|
574
562
|
NSData *decoded = [[NSData alloc] initWithBase64EncodedString:base64encodeStr options:0];
|
|
575
|
-
|
|
563
|
+
|
|
576
564
|
pendingResolve = resolve;
|
|
577
565
|
pendingReject = reject;
|
|
578
566
|
[RNBluetoothManager writeValue:decoded withDelegate:self];
|
|
@@ -646,14 +634,22 @@ RCT_EXPORT_METHOD(printBarCode:(NSString *) str withType:(NSInteger)
|
|
|
646
634
|
}
|
|
647
635
|
|
|
648
636
|
- (void) didWriteDataToBle: (BOOL)success{
|
|
649
|
-
if(
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
637
|
+
if(!pendingResolve && !pendingReject) return;
|
|
638
|
+
@try {
|
|
639
|
+
if(success){
|
|
640
|
+
if(pendingResolve) pendingResolve(nil);
|
|
641
|
+
}else{
|
|
642
|
+
if(pendingReject) pendingReject(@"COMMAND_NOT_SEND",@"COMMAND_NOT_SEND",nil);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
@catch(NSException *e){
|
|
646
|
+
NSLog(@"didWriteDataToBle exception: %@", e);
|
|
647
|
+
}
|
|
648
|
+
@finally {
|
|
649
|
+
pendingReject = nil;
|
|
650
|
+
pendingResolve = nil;
|
|
653
651
|
}
|
|
654
|
-
|
|
655
|
-
pendingResolve = nil;
|
|
656
|
-
[NSThread sleepForTimeInterval:0.05f];//slow down
|
|
652
|
+
[NSThread sleepForTimeInterval:0.05f];
|
|
657
653
|
}
|
|
658
654
|
|
|
659
655
|
@end
|