From a5e020025351930cff9762e01f29fc393544fefe Mon Sep 17 00:00:00 2001 From: Matthew Paravati Date: Wed, 24 Nov 2021 14:34:50 -0500 Subject: [PATCH 1/7] Fix bug print bitmap on android - pulls in changes from the open PR #66 on the forked react-native-star-prnt repo --- .../reactstarprnt/RNStarPrntModule.java | 48 +++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java b/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java index dc409e4..521cf26 100644 --- a/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java +++ b/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java @@ -13,11 +13,14 @@ import android.text.TextPaint; import android.net.Uri; import android.provider.MediaStore; +import androidx.annotation.Nullable; import android.text.StaticLayout; import android.text.Layout; import android.util.Base64; import android.graphics.BitmapFactory; +import com.facebook.common.util.ExceptionWithNoStacktrace; +import com.facebook.react.bridge.Dynamic; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; @@ -162,15 +165,20 @@ public void connect(final String portName, final String emulation, final Boolean String portSettings = getPortSettingsOption(emulation); if (starIoExtManager != null && starIoExtManager.getPort() != null) { starIoExtManager.disconnect(new IConnectionCallback() { - @Override - public void onConnected(ConnectResult connectResult) { - // Do nothing - } + @Override + public void onConnected(ConnectResult connectResult) { + if (connectResult == ConnectResult.Success || connectResult == ConnectResult.AlreadyConnected) { - @Override - public void onDisconnected() { - //Do nothing - } + promise.resolve("Printer Connected"); + + } else { + promise.reject("CONNECT_ERROR", "Error Connecting to the printer"); + } + } + @Override + public void onDisconnected() { + //Do nothing + } }); } starIoExtManager = new StarIoExtManager(hasBarcodeReader ? StarIoExtManager.Type.WithBarcodeReader : StarIoExtManager.Type.Standard, portName, portSettings, 10000, context); @@ -572,23 +580,23 @@ else if (command.hasKey("appendBytes")) { }else builder.appendQrCode(command.getString("appendQrCode").getBytes(encoding), qrCodeModel, qrCodeLevel, cell); } else if (command.hasKey("appendBitmap")){ ContentResolver contentResolver = context.getContentResolver(); - String uriString = command.getString("appendBitmap"); + String _uriString = command.getString("appendBitmap"); + String uriString = _uriString.substring(_uriString.indexOf(",") + 1); boolean diffusion = (command.hasKey("diffusion")) ? command.getBoolean("diffusion") : true; int width = (command.hasKey("width")) ? command.getInt("width") : 576; boolean bothScale = (command.hasKey("bothScale")) ? command.getBoolean("bothScale") : true; ICommandBuilder.BitmapConverterRotation rotation = (command.hasKey("rotation")) ? getConverterRotation(command.getString("rotation")) : getConverterRotation("Normal"); - try { - Uri imageUri = Uri.parse(uriString); - Bitmap bitmap = MediaStore.Images.Media.getBitmap(contentResolver, imageUri); - if(command.hasKey("absolutePosition")){ - int position = command.getInt("absolutePosition"); - builder.appendBitmapWithAbsolutePosition(bitmap, diffusion, width, bothScale, rotation, position); - }else if(command.hasKey("alignment")){ - ICommandBuilder.AlignmentPosition alignmentPosition = getAlignment(command.getString("alignment")); - builder.appendBitmapWithAlignment(bitmap, diffusion, width, bothScale, rotation, alignmentPosition); - }else builder.appendBitmap(bitmap, diffusion, width, bothScale, rotation); - } catch (IOException e) { + final byte[] decodedBytes = Base64.decode(uriString, Base64.DEFAULT); + Bitmap bitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length); + if(command.hasKey("absolutePosition")){ + int position = command.getInt("absolutePosition"); + builder.appendBitmapWithAbsolutePosition(bitmap, diffusion, width, bothScale, rotation, position); + }else if(command.hasKey("alignment")){ + ICommandBuilder.AlignmentPosition alignmentPosition = getAlignment(command.getString("alignment")); + builder.appendBitmapWithAlignment(bitmap, diffusion, width, bothScale, rotation, alignmentPosition); + }else { + builder.appendBitmap(bitmap, diffusion, width, bothScale, rotation); } } else if (command.hasKey("appendBitmapText")){ int fontSize = (command.hasKey("fontSize")) ? command.getInt("fontSize") : 25; From bedc808bec4d7f937af6a4409cb291a491eed986 Mon Sep 17 00:00:00 2001 From: Matthew Paravati Date: Tue, 30 Nov 2021 08:34:11 -0500 Subject: [PATCH 2/7] add try catch back, remove previously removed imports --- .../reactstarprnt/RNStarPrntModule.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java b/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java index 521cf26..4b9b649 100644 --- a/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java +++ b/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java @@ -19,8 +19,6 @@ import android.util.Base64; import android.graphics.BitmapFactory; -import com.facebook.common.util.ExceptionWithNoStacktrace; -import com.facebook.react.bridge.Dynamic; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; @@ -586,17 +584,20 @@ else if (command.hasKey("appendBytes")) { int width = (command.hasKey("width")) ? command.getInt("width") : 576; boolean bothScale = (command.hasKey("bothScale")) ? command.getBoolean("bothScale") : true; ICommandBuilder.BitmapConverterRotation rotation = (command.hasKey("rotation")) ? getConverterRotation(command.getString("rotation")) : getConverterRotation("Normal"); + try { + final byte[] decodedBytes = Base64.decode(uriString, Base64.DEFAULT); + Bitmap bitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length); + if(command.hasKey("absolutePosition")){ + int position = command.getInt("absolutePosition"); + builder.appendBitmapWithAbsolutePosition(bitmap, diffusion, width, bothScale, rotation, position); + }else if(command.hasKey("alignment")){ + ICommandBuilder.AlignmentPosition alignmentPosition = getAlignment(command.getString("alignment")); + builder.appendBitmapWithAlignment(bitmap, diffusion, width, bothScale, rotation, alignmentPosition); + }else { + builder.appendBitmap(bitmap, diffusion, width, bothScale, rotation); + } + } catch (IOException e) { - final byte[] decodedBytes = Base64.decode(uriString, Base64.DEFAULT); - Bitmap bitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length); - if(command.hasKey("absolutePosition")){ - int position = command.getInt("absolutePosition"); - builder.appendBitmapWithAbsolutePosition(bitmap, diffusion, width, bothScale, rotation, position); - }else if(command.hasKey("alignment")){ - ICommandBuilder.AlignmentPosition alignmentPosition = getAlignment(command.getString("alignment")); - builder.appendBitmapWithAlignment(bitmap, diffusion, width, bothScale, rotation, alignmentPosition); - }else { - builder.appendBitmap(bitmap, diffusion, width, bothScale, rotation); } } else if (command.hasKey("appendBitmapText")){ int fontSize = (command.hasKey("fontSize")) ? command.getInt("fontSize") : 25; From b560a888ee8f655922cab32cdfa6c82810da44b9 Mon Sep 17 00:00:00 2001 From: Matthew Paravati Date: Thu, 27 Jan 2022 16:13:14 -0500 Subject: [PATCH 3/7] change to general exception --- .../java/net/infoxication/reactstarprnt/RNStarPrntModule.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java b/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java index 4b9b649..743e541 100644 --- a/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java +++ b/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java @@ -596,7 +596,7 @@ else if (command.hasKey("appendBytes")) { }else { builder.appendBitmap(bitmap, diffusion, width, bothScale, rotation); } - } catch (IOException e) { + } catch (Exception e) { } } else if (command.hasKey("appendBitmapText")){ From cb395e6e1d2381d7743245f07da109b50e5f5c5f Mon Sep 17 00:00:00 2001 From: Matthew Paravati Date: Thu, 27 Jan 2022 17:42:44 -0500 Subject: [PATCH 4/7] remove unused import --- .../java/net/infoxication/reactstarprnt/RNStarPrntModule.java | 1 - 1 file changed, 1 deletion(-) diff --git a/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java b/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java index 743e541..8394b87 100644 --- a/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java +++ b/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java @@ -33,7 +33,6 @@ import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.ReadableType; -import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; From e68366d6348411a1126730e24428707999764037 Mon Sep 17 00:00:00 2001 From: Matthew Paravati Date: Wed, 2 Feb 2022 09:39:41 -0500 Subject: [PATCH 5/7] add new appendBitmapBase64 command, re-add previous appendBitmap command to java code, update documentation with new command --- Documentation.md | 1 + .../reactstarprnt/RNStarPrntModule.java | 26 +++++++++++++++++-- ios/RNStarPrnt.m | 20 ++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Documentation.md b/Documentation.md index 3476d87..47c2c74 100644 --- a/Documentation.md +++ b/Documentation.md @@ -301,6 +301,7 @@ componentDidMount() { | QrCodeLevel: string |Property to be used with the appendQrCode command. Choose the format of the return value defined in [QrCodeLevel](#qrcodelevel) Example: ```{appendQrCode:'{BStar', QrCodeLevel:'H'}``` | | cell: number | Property to be used with the appendQrCode command. QRCode Cell size. Default 4. Example: ```{appendQrCode:'{BStar', cell:8}``` | | appendBitmap: string |Print command of the bitmap is generated and added to the command buffer. Takes a string image URI. This can be obtained via the camera or photo library or as a static resource saved on the phone memory. Additional Properties: diffusion, width, bothScale, rotation, absolutePosition, alignment. Example: ```{appendBitmap:uri, diffusion: true, width:576, bothScale: true}``` Example with absolutePosition: ```{appendBitmap:uri, diffusion: true, width:576, bothScale: true, absolutePosition: 40 }``` Example with alignment: ```{appendBitmap:uri, diffusion: true, width:576, bothScale: true, alignment:"Center" }``` | +| appendBitmapBase64: base64 string |Print command of the bitmap is generated and added to the command buffer. Takes a base64 string image URI. This can be obtained via the camera or photo library or as a static resource saved on the phone memory. Additional Properties: diffusion, width, bothScale, rotation, absolutePosition, alignment. Example: ```{appendBitmap:uri, diffusion: true, width:576, bothScale: true}``` Example with absolutePosition: ```{appendBitmap:uri, diffusion: true, width:576, bothScale: true, absolutePosition: 40 }``` Example with alignment: ```{appendBitmap:uri, diffusion: true, width:576, bothScale: true, alignment:"Center" }``` | | appendBitmapText: string | Property to print text as a bitmap image. Takes a string that you want converted to a bitmap. Additional properties: width, font, fontSize, bothScale, alignment. Example: ```{appendBitmapText:text, fontSize:32, alignment:"Center"}``` | diffusion: boolean | Property to be used with the appendBitmap command. Random dither: true = Valid, false = Invalid. Default true. Example: ```{appendBitmap:uri, diffusion: false } ``` | | bothScale: boolean| Property to be used with the appendBitmap command. Height is changed according to the conversion rate of the width property. true = Valid, false = Invalid. Default true. Example: ```{appendBitmap:uri, bothScale: true }``` | diff --git a/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java b/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java index 043c748..004e8b2 100644 --- a/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java +++ b/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java @@ -576,8 +576,30 @@ else if (command.hasKey("appendBytes")) { builder.appendQrCodeWithAlignment(command.getString("appendQrCode").getBytes(encoding), qrCodeModel, qrCodeLevel, cell, alignmentPosition); }else builder.appendQrCode(command.getString("appendQrCode").getBytes(encoding), qrCodeModel, qrCodeLevel, cell); } else if (command.hasKey("appendBitmap")){ + // This command has caused issues for Android in the past + // Leaving in as legacy but appendBitmapBase64 is added as newer, preferred alternative ContentResolver contentResolver = context.getContentResolver(); - String _uriString = command.getString("appendBitmap"); + String uriString = command.getString("appendBitmap"); + boolean diffusion = (command.hasKey("diffusion")) ? command.getBoolean("diffusion") : true; + int width = (command.hasKey("width")) ? command.getInt("width") : 576; + boolean bothScale = (command.hasKey("bothScale")) ? command.getBoolean("bothScale") : true; + ICommandBuilder.BitmapConverterRotation rotation = (command.hasKey("rotation")) ? getConverterRotation(command.getString("rotation")) : getConverterRotation("Normal"); + try { + Uri imageUri = Uri.parse(uriString); + Bitmap bitmap = MediaStore.Images.Media.getBitmap(contentResolver, imageUri); + if(command.hasKey("absolutePosition")){ + int position = command.getInt("absolutePosition"); + builder.appendBitmapWithAbsolutePosition(bitmap, diffusion, width, bothScale, rotation, position); + }else if(command.hasKey("alignment")){ + ICommandBuilder.AlignmentPosition alignmentPosition = getAlignment(command.getString("alignment")); + builder.appendBitmapWithAlignment(bitmap, diffusion, width, bothScale, rotation, alignmentPosition); + }else builder.appendBitmap(bitmap, diffusion, width, bothScale, rotation); + } catch (IOException e) { + promise.reject("APPEND_BITMAP_EXCEPTION", e.getMessage()); + } + } else if (command.hasKey("appendBitmapBase64")){ + ContentResolver contentResolver = context.getContentResolver(); + String _uriString = command.getString("appendBitmapBase64"); String uriString = _uriString.substring(_uriString.indexOf(",") + 1); boolean diffusion = (command.hasKey("diffusion")) ? command.getBoolean("diffusion") : true; int width = (command.hasKey("width")) ? command.getInt("width") : 576; @@ -596,7 +618,7 @@ else if (command.hasKey("appendBytes")) { builder.appendBitmap(bitmap, diffusion, width, bothScale, rotation); } } catch (Exception e) { - promise.reject("APPEND_BITMAP_EXCEPTION", e.getMessage()); + promise.reject("APPEND_BITMAP_BASE64_EXCEPTION", e.getMessage()); } } else if (command.hasKey("appendBitmapText")){ int fontSize = (command.hasKey("fontSize")) ? command.getInt("fontSize") : 25; diff --git a/ios/RNStarPrnt.m b/ios/RNStarPrnt.m index 01f956c..a9d905f 100644 --- a/ios/RNStarPrnt.m +++ b/ios/RNStarPrnt.m @@ -555,6 +555,26 @@ -(void)appendCommands:(ISCBBuilder *)builder } else [builder appendBitmap:image diffusion:diffusion width:width bothScale:bothScale rotation:rotation]; } + else if ([command valueForKey:@"appendBitmapBase64"]) { + NSString *urlString = [command valueForKey:@"appendBitmapBase64"]; + NSInteger width = ([command valueForKey:@"width"]) ? [[command valueForKey:@"width"] intValue] : 576; + BOOL diffusion = ([[command valueForKey:@"diffusion"] boolValue] == NO) ? NO : YES; + BOOL bothScale = ([[command valueForKey:@"bothScale"] boolValue] == NO) ? NO : YES; + SCBBitmapConverterRotation rotation = [self getBitmapConverterRotation:[command valueForKey:@"rotation"]]; + NSData *imageData = [[NSData alloc] initWithBase64EncodedString:urlString options:0]; + + UIImage *image = [UIImage imageWithData:imageData]; + + if([command valueForKey:@"absolutePosition"]){ + int position = ([[command valueForKey:@"absolutePosition"] intValue]) ? [[command valueForKey:@"absolutePosition"] intValue]: 40; + [builder appendBitmapWithAbsolutePosition:image diffusion:diffusion width:width bothScale:bothScale rotation:rotation position:position]; + } + else if ([command valueForKey:@"alignment"]){ + SCBAlignmentPosition alignment = [self getAlignment:[command valueForKey:@"alignment"]]; + [builder appendBitmapWithAlignment:image diffusion:diffusion width:width bothScale:bothScale rotation:rotation position:alignment]; + } + else [builder appendBitmap:image diffusion:diffusion width:width bothScale:bothScale rotation:rotation]; + } else if ([command valueForKey:@"appendBitmapText"]) { NSString *text = [command valueForKey:@"appendBitmapText"]; NSInteger width = ([command valueForKey:@"width"]) ? [[command valueForKey:@"width"] intValue] : 576; From 2f5721855c659fec67689ae15f24097db747bfab Mon Sep 17 00:00:00 2001 From: Matthew Paravati Date: Wed, 2 Feb 2022 09:57:47 -0500 Subject: [PATCH 6/7] change from IOException to Exception --- .../java/net/infoxication/reactstarprnt/RNStarPrntModule.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java b/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java index 004e8b2..4ae8b8c 100644 --- a/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java +++ b/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java @@ -594,7 +594,7 @@ else if (command.hasKey("appendBytes")) { ICommandBuilder.AlignmentPosition alignmentPosition = getAlignment(command.getString("alignment")); builder.appendBitmapWithAlignment(bitmap, diffusion, width, bothScale, rotation, alignmentPosition); }else builder.appendBitmap(bitmap, diffusion, width, bothScale, rotation); - } catch (IOException e) { + } catch (Exception e) { promise.reject("APPEND_BITMAP_EXCEPTION", e.getMessage()); } } else if (command.hasKey("appendBitmapBase64")){ From 5810472e8abee310948f91ed5c484e0e7133d2e4 Mon Sep 17 00:00:00 2001 From: Matthew Paravati Date: Thu, 3 Feb 2022 11:58:33 -0500 Subject: [PATCH 7/7] update error messaging --- .../net/infoxication/reactstarprnt/RNStarPrntModule.java | 2 +- ios/Communication.m | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java b/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java index 4ae8b8c..e2a051e 100644 --- a/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java +++ b/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java @@ -369,7 +369,7 @@ private boolean sendCommand(byte[] commands, StarIOPort port, Promise promise) { } catch (InterruptedException e) { } if (port == null) { //Not connected or port closed - promise.reject("STARIO_PORT_EXCEPTION", "Unable to Open Port, Please Connect to the printer before sending commands"); + promise.reject("STARIO_PORT_EXCEPTION", "Fail to Open Port, Please connect to the printer before sending commands. If connected, remove the printer and reconnect it."); return false; } diff --git a/ios/Communication.m b/ios/Communication.m index 323b183..fe08179 100644 --- a/ios/Communication.m +++ b/ios/Communication.m @@ -24,6 +24,7 @@ + (BOOL)sendCommands:(NSData *)commands while (YES) { if (port == nil) { title = @"Fail to Open Port"; + message = @"Please connect to the printer before sending commands. If connected, remove the printer and reconnect it."; break; } @@ -98,6 +99,7 @@ + (BOOL)sendCommandsDoNotCheckCondition:(NSData *)commands while (YES) { if (port == nil) { title = @"Fail to Open Port"; + message = @"Please connect to the printer before sending commands. If connected, remove the printer and reconnect it."; break; } @@ -172,6 +174,7 @@ + (BOOL)parseDoNotCheckCondition:(ISCPParser *)parser while (YES) { if (port == nil) { title = @"Fail to Open Port"; + message = @"Please connect to the printer before sending commands. If connected, remove the printer and reconnect it."; break; } @@ -276,6 +279,7 @@ + (BOOL)sendCommands:(NSData *)commands if (port == nil) { title = @"Fail to Open Port"; + message = @"Please connect to the printer before sending commands. If connected, remove the printer and reconnect it."; break; } @@ -375,6 +379,7 @@ + (BOOL)sendCommandsDoNotCheckCondition:(NSData *)commands if (port == nil) { title = @"Fail to Open Port"; + message = @"Please connect to the printer before sending commands. If connected, remove the printer and reconnect it."; break; } @@ -514,6 +519,7 @@ + (BOOL)disconnectBluetooth:(NSString *)modelName if (port == nil) { title = @"Fail to Open Port"; + message = @"Please connect to the printer before sending commands. If connected, remove the printer and reconnect it."; break; } @@ -623,6 +629,7 @@ + (BOOL)confirmSerialNumber:(NSString *)portName if (port == nil) { title = @"Fail to Open Port"; + message = @"Please connect to the printer before sending commands. If connected, remove the printer and reconnect it."; break; }