您的位置:首页 > 移动开发 > IOS开发

IOS图片来源的几种选择及简单的人脸识别

2016-03-02 16:25 411 查看
在iOS中要拍照和录制视频最简单的方式就是调用UIImagePickerController,UIImagePickerController继承与UINavigationController,需要使用代理方法时需要同时遵守这两个协议,以前可能比较多的是使用UIImagePickerController来选择相册图片或者拍摄图片,其实它的功能还能用来拍摄视频。

使用UIImagePickerController拍照或者拍视频主要以下几个步骤:

创建一个全局的UIImagePickerController对象。

指定UIImagePickerController的来源sourceType,

是来自UIImagePickerControllerSourceTypeCamera相机,

还是来自UIImagePickerControllerSourceTypePhotoLibrary相册。

然后是设置mediaTypes媒体类型,这是录制视频必须设置的选项,默认情况下是kUTTypeImage(注意:mediaTypes的设置是在MobileCoreServices框架下),同还可以设置一些其他视频相关的属性,例如:videoQuality视频的质量、videoMaximumDuration视频的最大录制时长(默认为10s),cameraDevice摄像头的方向(默认为后置相机)。

指定相机的捕获模式cameraCaptureMode,设置mediaTypes后在设置捕获模式,注意的是捕获模式需要在相机来源sourceType为相机时设置,否则会出现crash。

下面还是上代码吧,更加清晰明了…首先需要导入以下用到的几个头文件,同时遵守两个代理方法

1 )UIImagePickerControllerDelegate,

2)UINavigationControllerDelegate

整个效果图:



主要代码:

#import "ViewController.h"

@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
UIImagePickerController *imagePicker;
}

@end

@implementation ViewController
@synthesize imageView;
@synthesize viewShow;
- (void)viewDidLoad {
[super viewDidLoad];
[self loadImage];
}
-(void)loadImage{
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 600 ,300)];
[self.view addSubview:imageView];
imageView.backgroundColor = [UIColor redColor];
label1 = [[UIButton alloc]initWithFrame:CGRectMake(100,500, 130, 40)];
label2 = [[UIButton alloc]initWithFrame:CGRectMake(250, 500, 100, 40)];
[self.view addSubview:label1];
[self.view addSubview:label2];
label2.backgroundColor = [UIColor purpleColor];
label1.backgroundColor = [UIColor purpleColor];
[label2 setTitle:@"人脸识别" forState:UIControlStateNormal];
[label1 setTitle:@"选择识别图片" forState:UIControlStateNormal];
[label1 addTarget:self action:@selector(Change:) forControlEvents:UIControlEventTouchUpInside];
[label2 addTarget:self action:@selector(show:) forControlEvents:UIControlEventTouchUpInside];

}
//该方法用于人脸识别
//- (void)show:(id)sender {
//   [self showProgressIndicator:@""];
//}

- (void)Change:(id)sender {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"请选择图像来源" message:nil delegate:self cancelButtonTitle:@"Canel" otherButtonTitles:@"照片库",@"相册",@"照相机", nil];
[alert show];

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger) buttonIndex{
//判断是否了选择Delete按钮
NSString *str=[alertView buttonTitleAtIndex:buttonIndex];
if ([str isEqualToString:@"照片库"]) {
imagePicker=[[UIImagePickerController alloc]init];
imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate=self;
imagePicker.allowsEditing=YES;
// label1.hidden=YES;
//  label2.hidden=YES;
[self removeAllMarkViews];
[self presentViewController:imagePicker animated:YES completion:NULL];
}else if ([str isEqualToString:@"相册"]) {
imagePicker=[[UIImagePickerController alloc]init];
imagePicker.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePicker.delegate=self;
imagePicker.allowsEditing=YES;
//label1.hidden=YES;
//label2.hidden=YES;
[self removeAllMarkViews];
[self presentViewController:imagePicker animated:YES completion:NULL];
}else{
if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePicker=[[UIImagePickerController alloc]init];
imagePicker.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePicker.delegate=self;

imagePicker.allowsEditing=YES;
//label1.hidden=YES;
//label2.hidden=YES;
[self removeAllMarkViews];
[self presentViewController:imagePicker animated:YES completion:NULL];

}else{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"设备不支持自定义" delegate:nil cancelButtonTitle:@"Canel" otherButtonTitles: nil];
imagePicker.allowsEditing=YES;
//label1.hidden=YES;
//label2.hidden=YES;
[alert show];
}
}

}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[imagePicker dismissViewControllerAnimated:YES completion:nil];
UIImage *im=[info objectForKey:UIImagePickerControllerEditedImage];
imageView.image=im;

}


// 人脸识别代码

////设置显示的视图框架
//-(void)setShowViewFrame
//{
//    CGFloat scale = 1.;
//    CGSize imgSize = imageView.image.size;
//    CGRect vFrame = self.view.frame;
//    CGRect sFrame = viewShow.frame;
//    if (imgSize.width/CGRectGetWidth(vFrame) > imgSize.height/CGRectGetHeight(vFrame)) {
//        sFrame.size.width = CGRectGetWidth(vFrame);
//        sFrame.size.height = imgSize.height * CGRectGetWidth(vFrame)/imgSize.width;
//        scale = CGRectGetWidth(vFrame)/imgSize.width;
//    }
//    else{
//        sFrame.size.height = CGRectGetHeight(vFrame);
//        sFrame.size.width = imgSize.width * CGRectGetHeight(vFrame)/imgSize.height;
//
//        scale = CGRectGetHeight(vFrame)/imgSize.height;
//    }
//    sFrame.origin.x = (CGRectGetWidth(vFrame)-CGRectGetWidth(sFrame))/2.;
//    sFrame.origin.y = vFrame.origin.y + (CGRectGetHeight(vFrame)-CGRectGetHeight(sFrame))/2.;
//
//    viewShow.frame = sFrame;
//
//    UIImage *newImg = [self scaleImage:imageView.image toScale:scale];//图片根据imgV的frame进行重新缩放生成
//    imageView.image = newImg;
//
//}
////处理图像
//-(void)dealImageWhenItChanged
//{
//
//
//
//    [self setShowViewFrame];
//
//    rectFaceDetect = CGRectZero;
//
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        [self faceDetect:imageView.image];
//    });
//
//
//}
////移除所有标记的视图
//-(void)removeAllMarkViews
//{
//    //清除原来标记的View
//    for (UIView *vv in self.viewShow.subviews) {
//        if (vv.tag == 100) {
//                // [vv removeFromSuperview];
//        }
//    }
//}
////人脸检测
//- (void)faceDetect:(UIImage *)aImage
//{
//
//    //Create a CIImage version of your photo
//    CIImage* image = [CIImage imageWithCGImage:aImage.CGImage];
//
//    //create a face detector
//    //此处是CIDetectorAccuracyHigh,若用于real-time的人脸检测,则用CIDetectorAccuracyLow,更快
//    NSDictionary  *opts = [NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh
//                                                      forKey:CIDetectorAccuracy];
//    CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace
//                                              context:nil
//                                              options:opts];
//
//    //Pull out the features of the face and loop through them
//    NSArray* features = [detector featuresInImage:image];
//
//    if ([features count]==0) {
//        NSLog(@">>>>> 人脸监测【失败】啦 ~!!!");
//    }
//    NSLog(@">>>>> 人脸监测【成功】~!!!>>>>>> ");
//    dispatch_async(dispatch_get_main_queue(), ^{
//        [self markAfterFaceDetect:features];
//    });
//
//}
////人脸标识
//-(void)markAfterFaceDetect:(NSArray *)features
//{
//    [self hideProgressIndicator];
//    [self setShowViewFrame];
//    if ([features count]==0) {
//        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"失败"message:@"请不要使用后脑勺识别"delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
//        [alert show];
//        return;
//    }
//    for (CIFaceFeature *f in features){
//        //旋转180,仅y
//        CGRect aRect = f.bounds;
//        aRect.origin.y = self.viewShow.bounds.size.height - aRect.size.height - aRect.origin.y;//self.bounds.size
//
//        UIView *vv = [[UIView alloc]initWithFrame:aRect];
//        vv.tag = 100;
//        [vv setTransform:CGAffineTransformMakeScale(1, -1)];
//        vv.backgroundColor = [UIColor redColor];
//        vv.alpha = 0.6;
//        [self.viewShow addSubview:vv];
//
//
//        rectFaceDetect = aRect;
//
//
//
//        if (f.hasLeftEyePosition){
//
//
//            UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
//            vv.tag = 100;
//            //旋转180,仅y
//            CGPoint newCenter =  f.leftEyePosition;
//            newCenter.y = self.viewShow.bounds.size.height-newCenter.y;
//            vv.center = newCenter;
//
//            vv.backgroundColor = [UIColor yellowColor];
//            [vv setTransform:CGAffineTransformMakeScale(1, -1)];
//            vv.alpha = 0.6;
//            [self.viewShow addSubview:vv];
//
//        }
//        if (f.hasRightEyePosition) {
//
//            UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
//            vv.tag = 100;
//            //旋转180,仅y
//            CGPoint newCenter =  f.rightEyePosition;
//            newCenter.y = self.viewShow.bounds.size.height-newCenter.y;
//            vv.center = newCenter;
//            vv.backgroundColor = [UIColor blueColor];
//            [vv setTransform:CGAffineTransformMakeScale(1, -1)];
//            vv.alpha = 0.6;
//            [self.viewShow addSubview:vv];
//
//        }
//        if (f.hasMouthPosition) {
//            UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
//            vv.tag = 100;
//            //旋转180,仅y
//            CGPoint newCenter =  f.mouthPosition;
//            newCenter.y = self.viewShow.bounds.size.height-newCenter.y;
//            vv.center = newCenter;
//
//            vv.backgroundColor = [UIColor greenColor];
//            [vv setTransform:CGAffineTransformMakeScale(1, -1)];
//            vv.alpha = 0.6;
//            [self.viewShow addSubview:vv];
//        }
//    }
//}
//
//- (UIImage *) scaleImage:(UIImage *)image toScale:(float)scaleSize {
//    if (image) {
//        UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
//        [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
//        UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
//        UIGraphicsEndImageContext();
//        return scaledImage;
//    }
//    return nil;
//}
////显示指示器
//- (void)showProgressIndicator:(NSString *)text {
//
////    self.view.userInteractionEnabled = FALSE;
////    if(!progressHUD) {
////        CGFloat w = 160.0f, h = 120.0f;
////        progressHUD = [[UIProgressHUD alloc] initWithFrame:CGRectMake((self.view.frame.size.width-w)/2, (self.view.frame.size.height-h)/2, w, h)];
////
////        [progressHUD showInView:self.view];
////    }
//        // [self performSelector:@selector(dealImageWhenItChanged) withObject:self afterDelay:5];
//}
//
////隐藏指示器
//- (void)hideProgressIndicator {
//    self.view.userInteractionEnabled = TRUE;
//    if(progressHUD) {
//        [progressHUD hide];
//        progressHUD = nil;
//    }
//}
//
//
//- (void)didReceiveMemoryWarning
//{
//    [super didReceiveMemoryWarning];
//
//}


Demo下载路径:http://pan.baidu.com/s/1hrmI8gW
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: