Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix image size for tsc command #164

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,10 @@ public void onMethodCall(MethodCall call, Result result) {
pendingCall = call;
pendingResult = result;
break;
} else {
startScan(call, result);
}

startScan(call, result);

break;
}
case "stopScan":
Expand Down Expand Up @@ -287,7 +288,9 @@ private void invokeMethodUIThread(final String name, final BluetoothDevice devic
new Runnable() {
@Override
public void run() {
channel.invokeMethod(name, ret);
if (channel != null) {
channel.invokeMethod(name, ret);
}
}
});
}
Expand Down Expand Up @@ -327,8 +330,16 @@ private void connect(MethodCall call, Result result){
Map<String, Object> args = call.arguments();
if (args !=null && args.containsKey("address")) {
final String address = (String) args.get("address");
this.curMacAddress = address;

if(address.equals(curMacAddress)) {
DeviceConnFactoryManager deviceConnFactoryManager = DeviceConnFactoryManager.getDeviceConnFactoryManagers().get(address);
if(deviceConnFactoryManager != null && deviceConnFactoryManager.getConnState()) {
result.success(true);
return;
}
}

this.curMacAddress = address;
disconnect();

new DeviceConnFactoryManager.Build()
Expand Down Expand Up @@ -444,12 +455,9 @@ public void run() {
public boolean onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

if (requestCode == REQUEST_FINE_LOCATION_PERMISSIONS) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startScan(pendingCall, pendingResult);
} else {
pendingResult.error("no_permissions", "this plugin requires location permissions for scanning", null);
pendingResult = null;
}
// The grantResults values seem to depend of the Android version, so we cannot
// really depend on it, let's simply try to scan now.
startScan(pendingCall, pendingResult);
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public static Vector<Byte> mapToReceipt(Map<String,Object> config, List<Map<Stri
public static Vector<Byte> mapToLabel(Map<String,Object> config, List<Map<String,Object>> list) {
LabelCommand tsc = new LabelCommand();

int count = (int)(config.get("count")==null?1:config.get("count"));
int width = (int)(config.get("width")==null?60:config.get("width")); // 单位:mm
int height = (int)(config.get("height")==null?75:config.get("height")); // 单位:mm
int gap = (int)(config.get("gap")==null?0:config.get("gap")); // 单位:mm
Expand Down Expand Up @@ -149,6 +150,7 @@ public static Vector<Byte> mapToLabel(Map<String,Object> config, List<Map<String
String content = (String)m.get("content");
int x = (int)(m.get("x")==null?0:m.get("x")); //dpi: 1mm约为8个点
int y = (int)(m.get("y")==null?0:m.get("y"));
int imageWidth = (int)(m.get("width")==null?300:m.get("width"));

if("text".equals(type)){
// 绘制简体中文
Expand All @@ -164,12 +166,12 @@ public static Vector<Byte> mapToLabel(Map<String,Object> config, List<Map<String
}else if("image".equals(type)){
byte[] bytes = Base64.decode(content, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
tsc.addBitmap(x, y, LabelCommand.BITMAP_MODE.OVERWRITE, 300, bitmap);
tsc.addBitmap(x, y, LabelCommand.BITMAP_MODE.OVERWRITE, imageWidth, bitmap);
}
}

// 打印标签
tsc.addPrint(1, 1);
tsc.addPrint(1, count);
// 打印标签后 蜂鸣器响
tsc.addSound(2, 100);
//开启钱箱
Expand Down
10 changes: 6 additions & 4 deletions ios/Classes/BluetoothPrintPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
-(NSData *)mapToTscCommand:(NSDictionary *) args {
NSDictionary *config = [args objectForKey:@"config"];
NSMutableArray *list = [args objectForKey:@"data"];


NSNumber *count = ![config objectForKey:@"count"]?@"1" : [config objectForKey:@"count"];
NSNumber *width = ![config objectForKey:@"width"]?@"48" : [config objectForKey:@"width"];
NSNumber *height = ![config objectForKey:@"height"]?@"80" : [config objectForKey:@"height"];
NSNumber *gap = ![config objectForKey:@"gap"]?@"2" : [config objectForKey:@"gap"];
Expand All @@ -161,7 +162,8 @@ -(NSData *)mapToTscCommand:(NSDictionary *) args {
NSString *content = [m objectForKey:@"content"];
NSNumber *x = ![m objectForKey:@"x"]?@0 : [m objectForKey:@"x"];
NSNumber *y = ![m objectForKey:@"y"]?@0 : [m objectForKey:@"y"];

NSNumber *imageWidth = ![m objectForKey:@"width"]?@300 : [m objectForKey:@"width"];

if([@"text" isEqualToString:type]){
[command addTextwithX:[x intValue] withY:[y intValue] withFont:@"TSS24.BF2" withRotation:0 withXscal:1 withYscal:1 withText:content];
}else if([@"barcode" isEqualToString:type]){
Expand All @@ -171,12 +173,12 @@ -(NSData *)mapToTscCommand:(NSDictionary *) args {
}else if([@"image" isEqualToString:type]){
NSData *decodeData = [[NSData alloc] initWithBase64EncodedString:content options:0];
UIImage *image = [UIImage imageWithData:decodeData];
[command addBitmapwithX:[x intValue] withY:[y intValue] withMode:0 withWidth:300 withImage:image];
[command addBitmapwithX:[x intValue] withY:[y intValue] withMode:0 withWidth:imageWidth withImage:image];
}

}

[command addPrint:1 :1];
[command addPrint:1 :count];
return [command getCommand];
}

Expand Down
4 changes: 3 additions & 1 deletion lib/bluetooth_print_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ class LineText {

/// ['text'] double
final int? weight;
final int? width;
final int? height;

/// ['text','image'] double
final int? width;

/// ['text'] absolute position from line begin
final int? absolutePos;

Expand Down