選擇需要識別帶有二維碼的圖片,利用系統CIQRCodeFeature*feature = [featuresobjectAtIndex:0];
NSString*scannedResult = feature.messageString;能快速識別出圖片中二維碼的信息。
主要實現方法:
#pragma mark - 相冊
- (void)alumbBtnEvent {
self.detector= [CIDetectordetectorOfType:CIDetectorTypeQRCodecontext:niloptions:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
if(![UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {//判斷設備是否支持相冊
if(IOS8) {
UIAlertController*alert = [UIAlertControlleralertControllerWithTitle:@"提示"message:@"未開啟訪問相冊權限,請在設置->隱私->照片中進行設置!"preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction*cancel = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction*_Nonnullaction) {
}];
UIAlertAction*confirm = [UIAlertActionactionWithTitle:@"確定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
}];
[alert addAction:cancel];
[alert addAction:confirm];
[self presentViewController:alertanimated:YEScompletion:nil];
}
else{
UIAlertController*alert = [UIAlertControlleralertControllerWithTitle:@"提示"message:@"您的系統暫不支持此功能!"preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction*confirm = [UIAlertActionactionWithTitle:@"確定"style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnullaction) {
}];
[alert addAction:confirm];
[self presentViewController:alertanimated:YEScompletion:nil];
}
return;
}
UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
mediaUI.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
mediaUI.mediaTypes= [UIImagePickerControlleravailableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
mediaUI.allowsEditing=NO;
mediaUI.delegate=self;
[selfpresentViewController:mediaUIanimated:YEScompletion:^{
[[UIApplicationsharedApplication]setStatusBarStyle:UIStatusBarStyleDefaultanimated:YES];
}];
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
UIImage*image = [infoobjectForKey:UIImagePickerControllerEditedImage];
if(!image){
image = [infoobjectForKey:UIImagePickerControllerOriginalImage];
}
NSArray*features = [self.detectorfeaturesInImage:[CIImageimageWithCGImage:image.CGImage]];
if(features.count>=1) {
[picker dismissViewControllerAnimated:YEScompletion:^{
[[UIApplicationsharedApplication]setStatusBarStyle:UIStatusBarStyleLightContentanimated:YES];
CIQRCodeFeature*feature = [featuresobjectAtIndex:0];
NSString*scannedResult = feature.messageString;
//播放掃描二維碼的聲音
SystemSoundIDsoundID;
//AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
NSString*strSoundFile = [[NSBundlemainBundle]pathForResource:@"qrcode_found"ofType:@"wav"];
AudioServicesCreateSystemSoundID((__bridgeCFURLRef)[NSURLfileURLWithPath:strSoundFile],&soundID);
AudioServicesPlaySystemSound(soundID);
[selfaccordingQcode:scannedResult];
}];
}
else{
[pickerdismissViewControllerAnimated:YEScompletion:^{
[[UIApplicationsharedApplication]setStatusBarStyle:UIStatusBarStyleLightContentanimated:YES];
UIAlertController*alert = [UIAlertControlleralertControllerWithTitle:@"提示"message:@"該圖片沒有包含一個二維碼!"preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction*confirm = [UIAlertActionactionWithTitle:@"知道了"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
}];
[alert addAction:confirm];
[self presentViewController:alertanimated:YEScompletion:nil];
}];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker {
[pickerdismissViewControllerAnimated:YEScompletion:^{
[[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContentanimated:YES];
}];
}