ios 2-D code scanning (WeChat 2-D code scanning sound effect)

Thank you very much for using your valuable time to read my articles. Today you have brought a demo scanned by native QR codes. The code written a long time ago is used by recent projects. When you think there is a great possibility to use it in the future, you turn it out and sort out a demo. If you need it, you can make a reference.If you need help, of course, you are welcome to correct any inappropriate situation.You can watch what you like My Blog,My Short Book

Old rules, show results first



Bedroom, why is this gif so big...Regardless, the demo address ZQScanTool


Simple to use, drag the ZQScanTool folder into the project and give the ScanViewController to Push when a skip scan is required

ScanViewController *vc = [[ScanViewController alloc] init];

    [self.navigationController pushViewController:vc animated:YES];


Replace the scan results display controller ScanResultViewController with your flip page

You don't have to watch it here. Go demo.

Main Code:

1. Camera settings

- (void)instanceDevice{



    line_tag = 1872637;

    //Acquire camera equipment

    AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    //Create Input Stream

    AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

    //Create Output Stream

    AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init];

    //Set proxy to refresh in main thread

    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];



    //Initialize Linked Object

    session = [[AVCaptureSession alloc]init];

    //High quality collection rate

    [session setSessionPreset:AVCaptureSessionPresetHigh];

    if (input) {

        [session addInput:input];

    }

    if (output) {

        [session addOutput:output];

        //Set the encoding formats supported by the scanner (as follows, set bar code and QR code compatibility)

        NSMutableArray *a = [[NSMutableArray alloc] init];

        if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeQRCode]) {

            [a addObject:AVMetadataObjectTypeQRCode];

        }

        if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeEAN13Code]) {

            [a addObject:AVMetadataObjectTypeEAN13Code];

        }

        if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeEAN8Code]) {

            [a addObject:AVMetadataObjectTypeEAN8Code];

        }

        if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeCode128Code]) {

            [a addObject:AVMetadataObjectTypeCode128Code];

        }

        output.metadataObjectTypes=a;

    }

    AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:session];

    layer.videoGravity=AVLayerVideoGravityResizeAspectFill;

    layer.frame=self.view.layer.bounds;

    [self.view.layer insertSublayer:layer atIndex:0];



    [self setOverlayPickerView];



    [session addObserver:self forKeyPath:@"running" options:NSKeyValueObservingOptionNew context:nil];



    //Start capturing

    [session startRunning];

}

2. Scan UI

- (void)setOverlayPickerView

{

    //view on the left

    UIImageView *leftView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, ScreenHeight)];

    leftView.alpha = 0.5;

    leftView.backgroundColor = [UIColor blackColor];

    [self.view addSubview:leftView];

    //view on the right

    UIImageView *rightView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth-30, 0, 30, ScreenHeight)];

    rightView.alpha = 0.5;

    rightView.backgroundColor = [UIColor blackColor];

    [self.view addSubview:rightView];

    //Topmost view

    UIImageView* upView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 0, ScreenWidth - 60, (self.view.center.y-(ScreenWidth-60)/2))];

    upView.alpha = 0.5;

    upView.backgroundColor = [UIColor blackColor];

    [self.view addSubview:upView];

    UIButton *cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 20, 44, 44)];

    [cancleBtn setImage:[UIImage imageNamed:@"nav_backButton_image"] forState:UIControlStateNormal];

    [cancleBtn addTarget:self action:@selector(cancleBtnClick) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:cancleBtn];

    //Bottom view

    UIImageView * downView = [[UIImageView alloc] initWithFrame:CGRectMake(30, (self.view.center.y+(ScreenWidth-60)/2), (ScreenWidth-60), (ScreenHeight-(self.view.center.y-(ScreenWidth-60)/2)))];

    downView.alpha = 0.5;

    downView.backgroundColor = [UIColor blackColor];

    [self.view addSubview:downView];

    UIImageView *centerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth-60, ScreenHeight-60)];

    centerView.center = self.view.center;

    centerView.image = [UIImage imageNamed:@"scan_circle"];

    centerView.contentMode = UIViewContentModeScaleAspectFit;

    centerView.backgroundColor = [UIColor clearColor];

    [self.view addSubview:centerView];

    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(upView.frame), ScreenWidth-60, 2)];

    line.tag = line_tag;

    line.image = [UIImage imageNamed:@"scan_line"];

    line.contentMode = UIViewContentModeScaleAspectFill;

    line.backgroundColor = [UIColor clearColor];

    [self.view addSubview:line];

    UILabel *msg = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMinY(downView.frame), ScreenWidth-60, 60)];

    msg.backgroundColor = [UIColor clearColor];

    msg.textColor = [UIColor whiteColor];

    msg.textAlignment = NSTextAlignmentCenter;

    msg.font = [UIFont systemFontOfSize:16];

    msg.text = @"Place the QR code in the box,Scan automatically";

    [self.view addSubview:msg];

}

3. Scan Animation


/**

*

*  Monitor Scan Status - Modify Scan Animation

*

*/

- (void)observeValueForKeyPath:(NSString *)keyPath

                      ofObject:(id)object

                        change:(NSDictionary *)change

                      context:(void *)context{

    if ([object isKindOfClass:[AVCaptureSession class]]) {

        BOOL isRunning = ((AVCaptureSession *)object).isRunning;

        if (isRunning) {

            [self addAnimation];

        }else{

            [self removeAnimation];

        }

    }

}

* Add Scavenging Animation

- (void)addAnimation{

    UIView *line = [self.view viewWithTag:line_tag];

    line.hidden = NO;

    CABasicAnimation *animation = [ScanViewController moveYTime:2 fromY:[NSNumber numberWithFloat:0] toY:[NSNumber numberWithFloat:ScreenWidth-60-2] rep:OPEN_MAX];

    [line.layer addAnimation:animation forKey:@"LineAnimation"];

}

*Remove Scavenging Animation

- (void)removeAnimation{

    UIView *line = [self.view viewWithTag:line_tag];

    [line.layer removeAnimationForKey:@"LineAnimation"];

    line.hidden = YES;

}
+ (CABasicAnimation *)moveYTime:(float)time fromY:(NSNumber *)fromY toY:(NSNumber *)toY rep:(int)rep

{

    CABasicAnimation *animationMove = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];

    [animationMove setFromValue:fromY];

    [animationMove setToValue:toY];

    animationMove.duration = time;

    animationMove.delegate = self;

    animationMove.repeatCount  = rep;

    animationMove.fillMode = kCAFillModeForwards;

    animationMove.removedOnCompletion = NO;

    animationMove.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    return animationMove;

}

3. Scanning results

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{

    if (metadataObjects.count>0) {

        [session stopRunning];

        NSURL *url=[[NSBundle mainBundle]URLForResource:@"scanSuccess.wav" withExtension:nil];

            //2. Load the sound effect file and create a sound effect ID (SoundID, one ID corresponds to one sound effect file)

            SystemSoundID soundID=8787;

            AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID);

            //3. Play sound files

            //Both of the following functions can be used to play sound files. The first function has a vibration effect

            AudioServicesPlayAlertSound(soundID);

        AudioServicesPlaySystemSound(8787);

        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex :0];

        //Output Scan String

        NSString *data = metadataObject.stringValue;

        ScanResultViewController *resultVC = [[ScanResultViewController alloc] init];

        resultVC.title = @"Scan results";

        resultVC.result = data;

        [self.navigationController pushViewController:resultVC animated:YES];

    }

}

@ #%# &...#@!#!@@%!@#!@#!That's all. Well, that's all.Don't forget to compliment me!



Tags: Session encoding

Posted on Tue, 21 Apr 2020 09:55:06 -0700 by williamg