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 cc684f8..e2a051e 100644 --- a/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java +++ b/android/src/main/java/net/infoxication/reactstarprnt/RNStarPrntModule.java @@ -13,6 +13,7 @@ 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; @@ -32,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; @@ -162,15 +162,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); @@ -364,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; } @@ -571,6 +576,8 @@ 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"); boolean diffusion = (command.hasKey("diffusion")) ? command.getBoolean("diffusion") : true; @@ -590,6 +597,29 @@ else if (command.hasKey("appendBytes")) { } catch (Exception 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; + 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 (Exception e) { + promise.reject("APPEND_BITMAP_BASE64_EXCEPTION", e.getMessage()); + } } else if (command.hasKey("appendBitmapText")){ int fontSize = (command.hasKey("fontSize")) ? command.getInt("fontSize") : 25; boolean diffusion = (command.hasKey("diffusion")) ? command.getBoolean("diffusion") : true; 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; } 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;