不忘流星 2015-06-17 10:00 采纳率: 0%
浏览 2337
已结题

为什么我用ios7 系统的AVFoundation 不能扫描条形码,只能扫描二维码

//
// ViewController.m
// 条形码三
//
// Created by mac on 15/6/17.
// Copyright (c) 2015年 郑氏集团. All rights reserved.
//

#import "ViewController.h"
#import

@interface ViewController ()
@property (nonatomic, strong) AVCaptureSession *captureSession;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;
@property (weak, nonatomic) IBOutlet UIView *viewPreview;
@property (weak, nonatomic) IBOutlet UILabel *lblStatus;
@property (weak, nonatomic) IBOutlet UIButton *bbitemStart;
@property (nonatomic) BOOL isReading;
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    _isReading = NO;

    _captureSession = nil;
    [self loadBeepSound];
    // Do any additional setup after loading the view, typically from a nib.
    }

  • (IBAction)startStopReading:(id)sender {
    if (!_isReading) {
    if ([self stopReading]) {
    // [_bbitemStart setTitle:@"Stop"];
    [_lblStatus setText:@"Scanning for QR Code..."];
    }
    }
    else{
    [self startReading];
    // [_bbitemStart setTitle:@"Start!"];
    }

    _isReading = !_isReading;
    }

  • (BOOL)startReading {
    NSError *error;

    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
    if (!input) {
    NSLog(@"%@", [error localizedDescription]);
    return NO;
    }

    _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.metadataObjectTypes=@[ AVMetadataObjectTypeQRCode,AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode128Code,AVMetadataObjectTypeCode39Mod43Code,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode93Code];

    _videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
    [_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    [_videoPreviewLayer setFrame:_viewPreview.layer.bounds];
    [_viewPreview.layer addSublayer:_videoPreviewLayer];

    [_captureSession startRunning];

    return YES;
    }
    -(BOOL)stopReading{
    [_captureSession stopRunning];
    _captureSession = nil;

    [_videoPreviewLayer removeFromSuperlayer];
    return YES;
    }
    -(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)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]) {
[_lblStatus performSelectorOnMainThread:@selector(setText:) withObject:[metadataObj stringValue] waitUntilDone:NO];

        [self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
        [_bbitemStart performSelectorOnMainThread:@selector(setTitle:) withObject:@"Start!" waitUntilDone:NO];
        _isReading = NO;
        if (_audioPlayer) {
            [_audioPlayer play];
    }
}

}

}

  • (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }

@end

  • 写回答

1条回答 默认 最新

  • 苹果里的虫 2016-11-03 09:56
    关注

    我也遇到这个问题,解决了吗?

    评论

报告相同问题?

悬赏问题

  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程