Skip to content

Commit d99e98e

Browse files
DNVADNVA
authored andcommitted
change iOS callback logic; update README;
1 parent b02819f commit d99e98e

File tree

2 files changed

+59
-22
lines changed

2 files changed

+59
-22
lines changed

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@ The plugin aims to be used with iOS version >= 7.
1616

1717
## Usage
1818
The plugin offers two functions `isDetecting` and `setPattern`.
19-
`isDetecting` callback on success function if detecting, on error function if it's not.
20-
`setPattern` sets the new pattern target to be detected. Calls on success if the pattern is set on error if no pattern set.
21-
19+
`isDetecting` - the plugin will callback on success function if detecting the pattern or on error function if it's not.
20+
```javascript
21+
isDetecting(successCallback, errorCallback);
22+
```
23+
24+
`setPattern` - sets the new pattern target to be detected. Calls on success if the pattern is set and on error if no pattern set. The input pattern must be a base64 image.
25+
```javascript
26+
setPattern(base64image, successCallback, errorCallback);
27+
```
28+
2229
## Usage example
2330
```javascript
24-
setInterval(function(){
25-
ImageDetectionPlugin.isDetecting(function(success){console.log(success);}, function(error){console.log(error);});
26-
}, 1000);
31+
ImageDetectionPlugin.isDetecting(function(success){console.log(success);}, function(error){console.log(error);});
2732

2833
var img = new Image();
2934
img.crossOrigin = "Anonymous";

src/ios/ImageDetectionPlugin.mm

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ @interface ImageDetectionPlugin()
1010
{
1111
Mat patt, desc1;
1212
vector<KeyPoint> kp1;
13-
bool debug, thread_over;
13+
bool debug, thread_over, called_success_detection, called_failed_detection;
1414
NSMutableArray *detection;
15+
NSString *callbackID;
1516
}
1617

1718
@end
@@ -39,20 +40,23 @@ - (void)greet:(CDVInvokedUrlCommand*)command
3940

4041
-(void)isDetecting:(CDVInvokedUrlCommand*)command
4142
{
42-
[self.commandDelegate runInBackground:^{
43-
CDVPluginResult* plugin_result = nil;
44-
NSString* msg;
45-
46-
if ([self getState]) {
47-
msg = @"pattern detected";
48-
plugin_result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:msg];
49-
} else {
50-
msg = @"pattern not detected";
51-
plugin_result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:msg];
52-
}
53-
54-
[self.commandDelegate sendPluginResult:plugin_result callbackId:command.callbackId];
55-
}];
43+
callbackID = command.callbackId;
44+
// [self.commandDelegate runInBackground:^{
45+
// CDVPluginResult* plugin_result = nil;
46+
// NSString* msg;
47+
//
48+
// if ([self getState]) {
49+
// msg = @"pattern detected";
50+
// plugin_result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:msg];
51+
// [plugin_result setKeepCallbackAsBool:YES];
52+
// } else {
53+
// msg = @"pattern not detected";
54+
// plugin_result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:msg];
55+
// [plugin_result setKeepCallbackAsBool:YES];
56+
// }
57+
//
58+
// [self.commandDelegate sendPluginResult:plugin_result callbackId:command.callbackId];
59+
// }];
5660
}
5761

5862
- (void)setPattern:(CDVInvokedUrlCommand*)command;
@@ -126,6 +130,8 @@ - (void)pluginInitialize {
126130

127131
debug = false;
128132
thread_over = true;
133+
called_success_detection = false;
134+
called_failed_detection = true;
129135

130136
detection = [[NSMutableArray alloc] init];
131137

@@ -291,7 +297,7 @@ - (void)backgroundImageProcessing:(const Mat &)image
291297

292298
-(void)updateState:(BOOL) state
293299
{
294-
if(detection.count > 15)
300+
if(detection.count > 3)
295301
{
296302
[detection removeObjectAtIndex:0];
297303
}
@@ -302,6 +308,31 @@ -(void)updateState:(BOOL) state
302308
} else {
303309
[detection addObject:[NSNumber numberWithBool:NO]];
304310
}
311+
312+
if([self getState] && called_failed_detection && !called_success_detection) {
313+
[self.commandDelegate runInBackground:^{
314+
CDVPluginResult* plugin_result = nil;
315+
NSString* msg = @"pattern detected";
316+
plugin_result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:msg];
317+
[plugin_result setKeepCallbackAsBool:YES];
318+
319+
[self.commandDelegate sendPluginResult:plugin_result callbackId:callbackID];
320+
}];
321+
called_success_detection = true;
322+
called_failed_detection = false;
323+
}
324+
if(![self getState] && !called_failed_detection && called_success_detection) {
325+
[self.commandDelegate runInBackground:^{
326+
CDVPluginResult* plugin_result = nil;
327+
NSString* msg = @"pattern not detected";
328+
plugin_result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:msg];
329+
[plugin_result setKeepCallbackAsBool:YES];
330+
331+
[self.commandDelegate sendPluginResult:plugin_result callbackId:callbackID];
332+
}];
333+
called_success_detection = false;
334+
called_failed_detection = true;
335+
}
305336
}
306337

307338
-(BOOL)getState
@@ -316,4 +347,5 @@ -(BOOL)getState
316347
return false;
317348
}
318349
}
350+
319351
@end

0 commit comments

Comments
 (0)