IOS QR code scanning and ios Scanning

Source: Internet
Author: User

IOS QR code scanning and ios Scanning

// Import the AVFoundation. framwork framework
# Import "HDCodeViewController. h "# import" HDNormalViewController. h "# import <AVFoundation/AVFoundation. h> @ interface HDCodeViewController () <AVCaptureMetadataOutputObjectsDelegate> @ property (strong, nonatomic) UIView * codeView; @ property (strong, nonatomic) UIImageView * myLine; @ end @ implementation HDCodeViewController {AVCaptureSession * _ session; NSString * _ theString; NSTimer * _ timer; BOOL _ upOrdown; NSInteger _ num;}-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. [self initUI]; [self prepareCamara]; [self initData];}-(void) dealloc {[_ timer invalidate]; _ timer = nil; _ session = nil ;} -(void) initData {_ num = 0 ;}# pragma mark-UI Stuff Methods-(void) initUI {self. codeView = [[UIView alloc] initWithFrame: CGRectMake (0, 44, SCREEN_WIDTH, SCREEN_HEIGHT-44)]; self. codeView. backgroundColor = [UIColor clearColor]; [self. view addSubview: self. codeView]; UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (SCREEN_WIDTH-300)/2, 20,300,300)]; imageView. image = [UIImage imageNamed: @ "code_square"]; [self. codeView addSubview: imageView]; self. myLine = [[UIImageView alloc] initWithFrame: CGRectMake (SCREEN_WIDTH-300)/2, 20,300, 2)]; self. myLine. contentMode = UIViewContentModeScaleToFill; self. myLine. image = [UIImage imageNamed: @ "code_movingBar"]; [self. codeView addSubview: self. myLine]; _ timer = [NSTimer scheduledTimerWithTimeInterval :. 02 target: self selector: @ selector (moveLine) userInfo: nil repeats: YES];}-(void) moveLine {CGFloat height = self. myLine. frame. origin. y; CGFloat width = self. myLine. frame. size. width; if (_ upOrdown = NO) {_ num ++; self. myLine. frame = CGRectMake (SCREEN_WIDTH-300)/2, height + 2, width, 2); if (2 * _ num = 300) {_ upOrdown = YES ;}} else {_ num --; self. myLine. frame = CGRectMake (SCREEN_WIDTH-300)/2, height-2, width, 2); if (_ num = 0) {_ upOrdown = NO ;}}} # pragma Camara Stuff Methods-(void) prepareCamara {AVCaptureDevice * device = [AVCaptureDevice failed: AVMediaTypeVideo]; required * input = [deviceInputWithDevice: device error: nil]; AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc] init]; [output setMetadataObjectsDelegate: self queue: queue ()]; _ session = [[AVCaptureSession alloc] init]; [_ session setSessionPreset: AVCaptureSessionPresetHigh]; [_ session addInput: input]; [_ session addOutput: output]; output. metadataObjectTypes = @ [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code]; required * layer = [javaslayerwithsession: _ session]; layer. videoGravity = AVLayerVideoGravityResizeAspectFill; layer. frame = CGRectMake (SCREEN_WIDTH-300)/2, 20,300,300); [self. codeView. layer insertSublayer: layer atIndex: 0]; [_ session startRunning];}-(void) captureOutput :( AVCaptureOutput *) captureOutput failed :( NSArray *) metadataObjects fromConnection :( AVCaptureConnection *) connection {if (metadataObjects. count> 0) {// [session stopRunning]; AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex: 0]; _ theString = metadataObject. stringValue; NSLog (@ "% @", _ theString); // The scanned string} @ end

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.