本帖最后由 竹林风 于 2019-1-15 11:57 编辑
导读
代码 :
1.HomePageVC.m
[Objective-C] 纯文本查看 复制代码 UIButton *button = [MTools createButtonWithTitle:@"扫一扫" Font:kFont(15) TitleColor:Color_fff];
button.frame = CGRectMake(50, 100, ScreenWidth - 50*2, 50);
button.backgroundColor = kColor(orangeColor);
[button addButtonClickBlock:^(UIButton *btn) {
SZQRCodeViewController *qr = [SZQRCodeViewController new];
qr.scanResult = ^(NSString *strResult) {
self->lbl.text = [NSString stringWithFormat:@"扫码结果:%@",strResult];
};
pushToViewController(self, qr, YES);
}];
[self.view addSubview:button];
lbl = [MTools creatLblWithFont:kFont(14) TextColor:Color_333];
lbl.frame = CGRectMake(button.frameX, button.frameBottom + 20, button.frameWidth, 50);
[self.view addSubview:lbl];
2.SZQRCodeVC.m
[Objective-C] 纯文本查看 复制代码 - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = kColor(blackColor);
self.title = @"扫一扫";
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"没有相机权限" message:@"请去设置-隐私-相机中对App授权" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self.navigationController popViewControllerAnimated:YES];
}];
[alertController addAction:okAction];
hasCameraRight = NO;
return;
}
hasCameraRight = YES;
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(Width(30), 100, ScreenWidth - Width(30)*2, 0.8 * self.view.frame.size.width)];
imageView.image = [UIImage imageNamed:@"contact_scanframe"];
[self.view addSubview:imageView];
UILabel * labIntroudction= [[UILabel alloc] initWithFrame:CGRectMake(0, imageView.frameBottom + 10, 290, 30)];
labIntroudction.backgroundColor = [UIColor clearColor];
labIntroudction.textColor=[UIColor whiteColor];
labIntroudction.textAlignment = NSTextAlignmentCenter;
labIntroudction.font = kFont(15);
labIntroudction.text=@"将取景框对准二维码,即自动扫描";
[self.view addSubview:labIntroudction];
upOrdown = NO;
num =0;
_line = [[UIImageView alloc] initWithFrame:CGRectMake(50, 110, 220, 2)];
_line.image = [UIImage imageNamed:@"line"];
[self.view addSubview:_line];
[self setupCamera];
}
-(void)animation1
{
if (upOrdown == NO) {
num ++;
_line.frame = CGRectMake(CGRectGetMinX(_line.frame), 110+2*num, CGRectGetWidth(_line.frame), CGRectGetHeight(_line.frame));
if (2 * num == CGRectGetHeight(imageView.frame) - 20) {
upOrdown = YES;
}
}
else {
num --;
_line.frame = CGRectMake(CGRectGetMinX(_line.frame), 110+2*num, CGRectGetWidth(_line.frame), CGRectGetHeight(_line.frame));
if (num == 0) {
upOrdown = NO;
}
}
}
- (BOOL)navigationShouldPopOnBackButton
{
[timer invalidate];
return YES;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (hasCameraRight) {
if (_session && ![_session isRunning]) {
[_session startRunning];
}
timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation1) userInfo:nil repeats:YES];
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[timer invalidate];
}
- (void)setupCamera
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 耗时的操作
// Device
self->_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// Input
self->_input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
// Output
self->_output = [[AVCaptureMetadataOutput alloc]init];
// [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[self->_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
// Session
self->_session = [[AVCaptureSession alloc]init];
[self->_session setSessionPreset:AVCaptureSessionPresetHigh];
if ([self->_session canAddInput:self.input])
{
[self->_session addInput:self.input];
}
if ([self->_session canAddOutput:self.output])
{
[self->_session addOutput:self.output];
}
// 条码类型 AVMetadataObjectTypeQRCode
self->_output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];
dispatch_async(dispatch_get_main_queue(), ^{
// 更新界面
// Preview
self->_preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];
self->_preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
// _preview.frame =CGRectMake(20,110,280,280);
self->_preview.frame = self.view.bounds;
[self.view.layer insertSublayer:self.preview atIndex:0];
// Start
[self->_session startRunning];
});
});
}
#pragma mark AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
NSString *stringValue;
if ([metadataObjects count] >0)
{
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
stringValue = metadataObject.stringValue;
[_session stopRunning];
[timer invalidate];
NSLog(@"扫码结果===%@",stringValue);
_scanResult(stringValue);
popViewController(self, YES);
}
}
-(NSString *)getParamValueByKey:(NSString *)key Params:(NSArray *)params{
__block NSString *value;
[params enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *string = obj;
if ([string containsString:key]) {
value = [string substringFromIndex:[key length]];
}
}];
return value;
}
附件:
扫一扫.zip
(719.43 KB, 下载次数: 0)
|