iOS7提供的API进行二维码扫描解码

来源:互联网 发布:java 方法名拼写出来 编辑:程序博客网 时间:2024/05/16 07:39
01.<code style="margin:0px;padding:0px;border:0px;vertical-align:baseline;font-family:Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;">- (void)initCameraLayer
02. 
03.{
04. 
05._session = [[AVCaptureSession alloc] init];
06. 
07.[self.session setSessionPreset:AVCaptureSessionPresetHigh];;
08. 
09. 
10. 
11.AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
12. 
13. 
14. 
15.NSError *error;
16. 
17.AVCaptureDeviceInput* deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
18. 
19.if ([self.session canAddInput:deviceInput]) {
20. 
21.[self.session addInput:deviceInput];
22. 
23.}
24. 
25. 
26. 
27.if (error) {
28. 
29.NSLog(@"error: %@", error.description);
30. 
31.}
32. 
33. 
34. 
35.AVCaptureMetadataOutput* output = [[AVCaptureMetadataOutput alloc] init];
36. 
37.[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
38. 
39.if ([self.session canAddOutput:output]) {
40. 
41.[self.session addOutput:output];
42. 
43.}
44. 
45. 
46. 
47.// 条码类型
48. 
49.//    output.metadataObjectTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];
50. 
51. 
52. 
53.output.metadataObjectTypes = @[AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code, AVMetadataObjectTypeCode128Code];
54. 
55. 
56. 
57._previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
58. 
59.[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
60. 
61.CALayer *rootLayer = self.rootScanView.layer;
62. 
63.[rootLayer setMasksToBounds:YES];
64. 
65.[rootLayer insertSublayer:self.previewLayer atIndex:0];
66. 
67. 
68. 
69.[self.previewLayer setFrame:self.rootScanView.bounds];
70. 
71.[self.previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
72. 
73.}</code>

view source
print?
01.- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
02. 
03.{
04. 
05.NSString *stringValue;
06. 
07. 
08. 
09.if ([metadataObjects count] > 0) {
10. 
11.AVMetadataMachineReadableCodeObject *metadataObject = [metadataObjects objectAtIndex:0];
12. 
13.stringValue = metadataObject.stringValue;
14. 
15.NSLog(@"string value....%@", stringValue);
16. 
17.}
18. 
19.}
0 0
原创粉丝点击