iOS 二維碼的產生和掃描,ios掃描

來源:互聯網
上載者:User

iOS 二維碼的產生和掃描,ios掃描
屬性

@property (strong,nonatomic)AVCaptureDevice * device;@property (strong,nonatomic)AVCaptureDeviceInput * input;@property (strong,nonatomic)AVCaptureMetadataOutput * output;@property (strong,nonatomic)AVCaptureSession * session;@property (strong,nonatomic)AVCaptureVideoPreviewLayer * layer;@property (nonatomic, strong)UIImageView *imageView;

 

二維碼的產生
 // 1.建立過濾器    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];    // 2.恢複預設    [filter setDefaults];    // 3.給過濾器添加資料(Regex/帳號和密碼)    NSString *dataString = @"http://www.520it.com";    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];    [filter setValue:data forKeyPath:@"inputMessage"];    // 4.擷取輸出的二維碼    CIImage *outputImage = [filter outputImage];    //因為產生的二維碼模糊,所以通過createNonInterpolatedUIImageFormCIImage:outputImage來獲得高清的二維碼圖片    // 5.顯示二維碼    self.imageView.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:200];

 * createNonInterpolatedUIImageFormCIImage:outputImage方法的實現

/** *  根據CIImage產生指定大小的UIImage * *  @param image CIImage *  @param size  圖片寬度 */- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size{    CGRect extent = CGRectIntegral(image.extent);    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));    // 1.建立bitmap;    size_t width = CGRectGetWidth(extent) * scale;    size_t height = CGRectGetHeight(extent) * scale;    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);    CIContext *context = [CIContext contextWithOptions:nil];    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);    CGContextScaleCTM(bitmapRef, scale, scale);    CGContextDrawImage(bitmapRef, extent, bitmapImage);    // 2.儲存bitmap到圖片    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);    CGContextRelease(bitmapRef);    CGImageRelease(bitmapImage);    return [UIImage imageWithCGImage:scaledImage];}

 二維碼的掃描

 // 1.建立捕捉會話    AVCaptureSession *session = [[AVCaptureSession alloc] init];    self.session = session;    // 2.添加輸入裝置(資料從網路攝影機輸入)    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];    [session addInput:input];    // 3.添加輸出資料(樣本對象-->類對象-->元類對象-->根元類對象)    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];    [session addOutput:output];    // 3.1.設定輸入中繼資料的類型(類型是二維碼資料)    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];    // 4.添加掃描圖層    AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:session];    layer.frame = self.view.bounds;    [self.view.layer addSublayer:layer];    self.layer = layer;    // 5.開始掃描    [session startRunning];

 *掃描到結果後會調用的方法

// 當掃描到資料時就會執行該方法- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{    if (metadataObjects.count > 0) {        //獲得掃描資料,最後一個時最新掃描的資料        AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject];        NSLog(@"%@", object.stringValue);        // 停止掃描        [self.session stopRunning];        // 將預覽圖層移除        [self.layer removeFromSuperlayer];    } else {        NSLog(@"沒有掃描到資料");    }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.