-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathRCTQRCodeLocalImage.m
More file actions
executable file
·53 lines (47 loc) · 1.7 KB
/
RCTQRCodeLocalImage.m
File metadata and controls
executable file
·53 lines (47 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// RCTQRCodeLocalImage.m
// RCTQRCodeLocalImage
//
// Created by fangyunjiang on 15/11/4.
// Copyright (c) 2015年 remobile. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <React/RCTLog.h>
#import <React/RCTUtils.h>
#import "RCTQRCodeLocalImage.h"
@implementation RCTQRCodeLocalImage
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(decode:(NSString *)path callback:(RCTResponseSenderBlock)callback)
{
UIImage *srcImage;
if ([path hasPrefix:@"http://"] || [path hasPrefix:@"https://"] || [path hasPrefix:@"file://"]) {
srcImage = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString: path]]];
} else {
srcImage = [[UIImage alloc] initWithContentsOfFile:path];
}
if (nil==srcImage){
NSLog(@"PROBLEM! IMAGE NOT LOADED\n");
callback(@[RCTMakeError(@"IMAGE NOT LOADED!", nil, nil)]);
return;
}
NSLog(@"OK - IMAGE LOADED\n");
NSDictionary *detectorOptions = @{@"CIDetectorAccuracy": @"CIDetectorAccuracyHigh"};
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:detectorOptions];
CIImage *image = [CIImage imageWithCGImage:srcImage.CGImage];
NSArray *features = [detector featuresInImage:image];
if (0==features.count) {
NSLog(@"PROBLEM! Feature size is zero!\n");
callback(@[RCTMakeError(@"Feature size is zero!", nil, nil)]);
return;
}
CIQRCodeFeature *feature = [features firstObject];
NSString *result = feature.messageString;
NSLog(@"result: %@", result);
if (result) {
callback(@[[NSNull null], result]);
} else {
callback(@[RCTMakeError(@"QR Parse failed!", nil, nil)]);
return;
}
}
@end