zoukankan      html  css  js  c++  java
  • IOS开发调用系统相机和打开闪光灯

    今天给大家分享一下如何调用iphone的拍照功能和打开闪光灯,有些代码我也不太理解,很多是在网上借鉴其他人的。IOS有两种的拍照和视频的方式:1.直接使用UIImagePickerController,这个类提供了一个简单便捷的拍照与选择图片库里图片的功能。2.另一种是通过AVFoundation.framework框架完全自定义拍照的界面和选择图片库界面。我只做了第一种,就先给大家介绍第一种做法:
    一、首先调用接口前,我们需要先判断当前设备是否支持UIImagePickerController,用isSourceTypeAvailable:来判断是否可用
    二、查看符合的媒体类型,这个时候我们调用availableMediaTypeForSourceType:判断
    在调用UIImagePickerController时我们需要加入他的两个代理方法:
    UINavigationControllerDelegate和UIImagePickerControllerDelegate,在调用摄像头的时候还可以调闪光灯,一会代码里有。

    要调用闪光灯需要先建一个AVCaptureSession类的实例对象:

    [java] view plaincopy
    1. #import <UIKit/UIKit.h>  
    2. //调用闪光灯调用框架  
    3. #import <AVFoundation/AVFoundation.h>  
    4.    
    5. @interface CameraViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>  
    6. {  
    7.     AVCaptureSession * _AVSession;//调用闪光灯的时候创建的类  
    8. }  
    9.    
    10. @property(nonatomic,retain)AVCaptureSession * AVSession;  
    11.    
    12. @end  
    在.m的- (void)viewDidLoad里建立4Button,Camera调用相机、Library调用图片库、flashlight打开闪光灯、close关闭闪光灯

    [java] view plaincopy
    1. //打开相机  
    2. -(void)addCarema  
    3. {  
    4.     //判断是否可以打开相机,模拟器此功能无法使用  
    5.     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {  
    6.            
    7.         UIImagePickerController * picker = [[UIImagePickerController alloc]init];  
    8.         picker.delegate = self;  
    9.         picker.allowsEditing = YES;  //是否可编辑  
    10.         //摄像头  
    11.         picker.sourceType = UIImagePickerControllerSourceTypeCamera;  
    12.         [self presentModalViewController:picker animated:YES];  
    13.         [picker release];  
    14.     }else{  
    15.         //如果没有提示用户  
    16.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];  
    17.         [alert show];  
    18.     }  
    19. }  

    打开相机后,然后需要调用UIImagePickerControllerDelegate里的方法,拍摄完成后执行的方法和点击Cancel之后执行的方法:

    [java] view plaincopy
    1. //拍摄完成后要执行的方法  
    2. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  
    3. {  
    4.     //得到图片  
    5.     UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];  
    6.     //图片存入相册  
    7.     UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);  
    8.     [self dismissModalViewControllerAnimated:YES];  
    9.        
    10. }  
    11. //点击Cancel按钮后执行方法  
    12. -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker  
    13. {  
    14.     [self dismissModalViewControllerAnimated:YES];  
    15. }  


    调用相机照片和保存到图片库已经完成。
    接着介绍打开照片库:
    [java] view plaincopy
    1. -(void)openPicLibrary  
    2. {  
    3.     //相册是可以用模拟器打开的  
    4.     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {  
    5.         UIImagePickerController * picker = [[UIImagePickerController alloc]init];  
    6.         picker.delegate = self;  
    7.         picker.allowsEditing = YES;//是否可以编辑  
    8.    
    9.         //打开相册选择照片  
    10.         picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
    11.         [self presentModalViewController:picker  animated:YES];  
    12.         [picker release];  
    13.     }else{  
    14.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];  
    15.         [alert show];  
    16.     }  
    17.        
    18. }  
    19.    
    20. //选中图片进入的代理方法  
    21. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo  
    22. {  
    23.     [self dismissModalViewControllerAnimated:YES];  
    24. }  

    调用闪光灯的代码

    [java] view plaincopy
    1. -(void)openFlashlight  
    2. {  
    3.     AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];  
    4.     if (device.torchMode == AVCaptureTorchModeOff) {  
    5.         //Create an AV session  
    6.         AVCaptureSession * session = [[AVCaptureSession alloc]init];  
    7.            
    8.         // Create device input and add to current session  
    9.         AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];  
    10.         [session addInput:input];  
    11.            
    12.         // Create video output and add to current session   
    13.         AVCaptureVideoDataOutput * output = [[AVCaptureVideoDataOutput alloc]init];  
    14.         [session addOutput:output];  
    15.            
    16.         // Start session configuration  
    17.         [session beginConfiguration];  
    18.         [device lockForConfiguration:nil];  
    19.            
    20.         // Set torch to on  
    21.         [device setTorchMode:AVCaptureTorchModeOn];  
    22.            
    23.         [device unlockForConfiguration];  
    24.         [session commitConfiguration];  
    25.            
    26.         // Start the session  
    27.         [session startRunning];  
    28.            
    29.         // Keep the session around  
    30.         [self setAVSession:self.AVSession];  
    31.            
    32.         [output release];  
    33.     }  
    34. }  
    35.    
    36. -(void)closeFlashlight  
    37. {  
    38.     [self.AVSession stopRunning];  
    39.     [self.AVSession release];  
    40. }  
  • 相关阅读:
    使用submit异步提交,阻止表单默认提交
    EasyDarwin流媒体服务器实现关键帧推送功能
    EasyDarwin开源手机直播方案:EasyPusher手机直播推送,EasyDarwin流媒体服务器,EasyPlayer手机播放器
    EasyDarwin开源手机直播方案:EasyPusher手机直播推送,EasyDarwin流媒体服务器,EasyPlayer手机播放器
    EasyDarwin流媒体云平台架构
    EasyDarwin流媒体云平台架构
    EasyDarwin流媒体服务器高性能优化方向
    EasyDarwin流媒体服务器高性能优化方向
    我们将要建立的EasyDarwin开源社区
    我们将要建立的EasyDarwin开源社区
  • 原文地址:https://www.cnblogs.com/yuhaojishuboke/p/5155855.html
Copyright © 2011-2022 走看看