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

added realtimedimensions on rectangle detection #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 31 additions & 23 deletions ios/DocumentScannerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ - (void) didDetectRectangle:(CIRectangleFeature *)rectangle withType:(IPDFRectan
break;
}
if (self.onRectangleDetect) {
self.onRectangleDetect(@{@"stableCounter": @(self.stableCounter), @"lastDetectionType": @(type)});
NSDictionary *rectangleCoordinates = rectangle ? @{
@"topLeft": @{ @"y": @(rectangle.bottomLeft.x + 30), @"x": @(rectangle.bottomLeft.y)},
@"topRight": @{ @"y": @(rectangle.topLeft.x + 30), @"x": @(rectangle.topLeft.y)},
@"bottomLeft": @{ @"y": @(rectangle.bottomRight.x), @"x": @(rectangle.bottomRight.y)},
@"bottomRight": @{ @"y": @(rectangle.topRight.x), @"x": @(rectangle.topRight.y)},
} : [NSNull null];


self.onRectangleDetect(@{@"stableCounter": @(self.stableCounter), @"lastDetectionType": @(type), @"rectangleCoordinates": rectangleCoordinates});
}

if (self.stableCounter > self.detectionCountBeforeCapture){
Expand All @@ -34,7 +42,7 @@ - (void) didDetectRectangle:(CIRectangleFeature *)rectangle withType:(IPDFRectan

- (void) capture {
[self captureImageWithCompletionHander:^(UIImage *croppedImage, UIImage *initialImage, CIRectangleFeature *rectangleFeature) {
if (self.onPictureTaken) {
if (self.onPictureTaken) {
NSData *croppedImageData = UIImageJPEGRepresentation(croppedImage, self.quality);

if (initialImage.imageOrientation != UIImageOrientationUp) {
Expand All @@ -47,42 +55,42 @@ - (void) capture {
NSData *initialImageData = UIImageJPEGRepresentation(initialImage, self.quality);

/*
RectangleCoordinates expects a rectanle viewed from portrait,
while rectangleFeature returns a rectangle viewed from landscape, which explains the nonsense of the mapping below.
Sorry about that.
RectangleCoordinates expects a rectanle viewed from portrait,
while rectangleFeature returns a rectangle viewed from landscape, which explains the nonsense of the mapping below.
Sorry about that.
*/
NSDictionary *rectangleCoordinates = rectangleFeature ? @{
@"topLeft": @{ @"y": @(rectangleFeature.bottomLeft.x + 30), @"x": @(rectangleFeature.bottomLeft.y)},
@"topRight": @{ @"y": @(rectangleFeature.topLeft.x + 30), @"x": @(rectangleFeature.topLeft.y)},
@"bottomLeft": @{ @"y": @(rectangleFeature.bottomRight.x), @"x": @(rectangleFeature.bottomRight.y)},
@"bottomRight": @{ @"y": @(rectangleFeature.topRight.x), @"x": @(rectangleFeature.topRight.y)},
} : [NSNull null];
@"topLeft": @{ @"y": @(rectangleFeature.bottomLeft.x + 30), @"x": @(rectangleFeature.bottomLeft.y)},
@"topRight": @{ @"y": @(rectangleFeature.topLeft.x + 30), @"x": @(rectangleFeature.topLeft.y)},
@"bottomLeft": @{ @"y": @(rectangleFeature.bottomRight.x), @"x": @(rectangleFeature.bottomRight.y)},
@"bottomRight": @{ @"y": @(rectangleFeature.topRight.x), @"x": @(rectangleFeature.topRight.y)},
} : [NSNull null];
if (self.useBase64) {
self.onPictureTaken(@{
@"croppedImage": [croppedImageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength],
@"initialImage": [initialImageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength],
@"rectangleCoordinates": rectangleCoordinates });
self.onPictureTaken(@{
@"croppedImage": [croppedImageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength],
@"initialImage": [initialImageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength],
@"rectangleCoordinates": rectangleCoordinates });
}
else {
NSString *dir = NSTemporaryDirectory();
if (self.saveInAppDocument) {
dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
}
NSString *croppedFilePath = [dir stringByAppendingPathComponent:[NSString stringWithFormat:@"cropped_img_%i.jpeg",(int)[NSDate date].timeIntervalSince1970]];
NSString *initialFilePath = [dir stringByAppendingPathComponent:[NSString stringWithFormat:@"initial_img_%i.jpeg",(int)[NSDate date].timeIntervalSince1970]];
NSString *croppedFilePath = [dir stringByAppendingPathComponent:[NSString stringWithFormat:@"cropped_img_%i.jpeg",(int)[NSDate date].timeIntervalSince1970]];
NSString *initialFilePath = [dir stringByAppendingPathComponent:[NSString stringWithFormat:@"initial_img_%i.jpeg",(int)[NSDate date].timeIntervalSince1970]];

[croppedImageData writeToFile:croppedFilePath atomically:YES];
[initialImageData writeToFile:initialFilePath atomically:YES];
[croppedImageData writeToFile:croppedFilePath atomically:YES];
[initialImageData writeToFile:initialFilePath atomically:YES];

self.onPictureTaken(@{
@"croppedImage": croppedFilePath,
@"initialImage": initialFilePath,
@"rectangleCoordinates": rectangleCoordinates });
self.onPictureTaken(@{
@"croppedImage": croppedFilePath,
@"initialImage": initialFilePath,
@"rectangleCoordinates": rectangleCoordinates });
}
}

if (!self.captureMultiple) {
[self stop];
[self stop];
}
}];

Expand Down