-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQrCodeScanner.m
90 lines (75 loc) · 2.72 KB
/
QrCodeScanner.m
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
@import AVFoundation;
@import UIKit;
@interface PreviewView: UIView
@property(nonatomic, readonly, strong) AVCaptureVideoPreviewLayer* previewLayer;
@property(nonatomic, retain, nullable) AVCaptureSession* session;
@end
@implementation PreviewView
+(Class)layerClass {
return AVCaptureVideoPreviewLayer.class;
}
-(AVCaptureVideoPreviewLayer*)previewLayer {
return (AVCaptureVideoPreviewLayer*)super.layer;
}
-(AVCaptureSession*)session {
return self.previewLayer.session;
}
-(void)setSession:(AVCaptureSession*)newValue {
self.previewLayer.session = newValue;
}
@end
@interface QrCodeScanner: NSObject <AVCaptureVideoDataOutputSampleBufferDelegate> {
AVCaptureSession* captureSession;
AVCaptureDevice* device;
AVCaptureVideoDataOutput* videoOutput;
uintptr_t scanner;
uint32_t(*onImageBuffer)(uintptr_t, CMSampleBufferRef);
void(*onQrcodeScanned)(uintptr_t);
}
-(AVCaptureSession*)session;
-(void)captureOutput:(AVCaptureOutput*)output
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection*)connection;
@end
@implementation QrCodeScanner
-(id)initWithScanner:(uintptr_t)scanner2
onImageBuffer:(uint32_t(*)(uintptr_t, CMSampleBufferRef))onImageBuffer2
onQrcodeScanned:(void(*)(uintptr_t))onQrcodeScanned2
{
self = [super init];
scanner = scanner2;
onImageBuffer = onImageBuffer2;
onQrcodeScanned = onQrcodeScanned2;
captureSession = [[AVCaptureSession alloc] init];
captureSession.sessionPreset = AVCaptureSessionPresetMedium;
device = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo];
NSError* outError = nil;
AVCaptureDeviceInput *cameraInput = [AVCaptureDeviceInput deviceInputWithDevice: device error:&outError];
if (outError != nil) {
@throw outError;
}
[captureSession addInput: cameraInput];
videoOutput = [[AVCaptureVideoDataOutput alloc] init];
NSString *key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber *value = [NSNumber numberWithInt: kCVPixelFormatType_32BGRA];
videoOutput.videoSettings = @{key : value};
dispatch_queue_t queue = dispatch_queue_create("dioxus-wallet.qrcodescanner", DISPATCH_QUEUE_SERIAL);
[videoOutput setSampleBufferDelegate:self queue: queue];
[videoOutput setAlwaysDiscardsLateVideoFrames: YES];
[captureSession addOutput: videoOutput];
[captureSession startRunning];
return self;
}
-(AVCaptureSession*)session {
return captureSession;
}
-(void)captureOutput:(AVCaptureOutput*)output
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection*)connection
{
if (onImageBuffer(scanner, sampleBuffer) > 0) {
[captureSession stopRunning];
onQrcodeScanned(scanner);
}
}
@end