-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReaderHelper.m
More file actions
96 lines (73 loc) · 2.94 KB
/
ReaderHelper.m
File metadata and controls
96 lines (73 loc) · 2.94 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
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
91
92
93
94
95
96
//
// ReaderHelper.m
// QRQuickReader
//
// Created by Li, Jonathan on 4/11/15.
// Copyright (c) 2015 It21Learning. All rights reserved.
//
#import "ReaderHelper.h"
@implementation ReaderHelper
-(id)init{
if (self == [super init]){
[self loadBeepSound];
return self;
}
else{
return nil;
}
}
-(void)playSound{
[self.audioPlayer play];
}
-(void)loadBeepSound{
NSString *beepFilePath = [[NSBundle mainBundle] pathForResource:@"beep" ofType:@"mp3"];
NSURL *beepURL = [NSURL URLWithString:beepFilePath];
NSError *error;
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:beepURL error:&error];
if (error) {
NSLog(@"Could not play beep file.");
NSLog(@"%@", [error localizedDescription]);
}
else{
[_audioPlayer prepareToPlay];
}
}
- (void)startRead:(UIView*)preview onCompletion:(void (^)(NSString*))complete {
NSError *error;
self.completionBlock = complete;
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
if (!input) {
NSLog(@"%@", [error localizedDescription]);
}
else{
_captureSession = [[AVCaptureSession alloc] init];
[_captureSession addInput:input];
AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
[_captureSession addOutput:captureMetadataOutput];
dispatch_queue_t dispatchQueue;
dispatchQueue = dispatch_queue_create("myQueue", NULL);
[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
[captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];
_videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
[_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[_videoPreviewLayer setFrame:preview.layer.bounds];
[preview.layer addSublayer:_videoPreviewLayer];
[_captureSession startRunning];
}
}
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
if (metadataObjects != nil && [metadataObjects count] > 0) {
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
dispatch_async(dispatch_get_main_queue(), ^{
[self playSound];
self.completionBlock([metadataObj stringValue]);
[_captureSession stopRunning];
// [_videoPreviewLayer removeFromSuperlayer];
_captureSession = nil;
});
}
}
}
@end