2013年4月16日 星期二
AVCaptureDevice對flash light的操作
//改變FlashLightStatus
-(void)changeFlashLightStatus{
// check if flashlight available
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
[device lockForConfiguration:nil];
if (device.torchMode == AVCaptureTorchModeOff) {
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
//torchIsOn = YES; //define as a variable/property if you need to know status
} else {
[device setTorchMode:AVCaptureTorchModeOff];
[device setFlashMode:AVCaptureFlashModeOff];
//torchIsOn = NO;
}
[device unlockForConfiguration];
}
}
以下轉自http://www.helmsmansoft.com/index.php/archives/1525
在appstore的眾多的軟件之中,相信很多人都用過一款軟件 “手電筒” , 在iPhone中,利用設備上的閃光燈來開發手電筒功能,下面就說下具體的實現方法:
開啟閃光燈的API封裝在框架AVFoundation.framework中,所以要想實現此功能,首先就要在程序中加入此框架,然後引入頭
#import
先來看下這個框架內的一些內容:
AVCaptureDevice.h主要用來獲取iphone一些關於相機設備的屬性。
前置和後置攝像頭
enum {
AVCaptureDevicePositionBack = 1,
AVCaptureDevicePositionFront = 2
};
typedef NSInteger AVCaptureDevicePosition;
閃光燈開關
enum {
AVCaptureFlashModeOff = 0,
AVCaptureFlashModeOn = 1,
AVCaptureFlashModeAuto = 2
};
typedef NSInteger AVCaptureFlashMode;
手電筒開關
enum {
AVCaptureTorchModeOff = 0,
AVCaptureTorchModeOn = 1,
AVCaptureTorchModeAuto = 2,
};
typedef NSInteger AVCaptureTorchMode;
焦距調整
enum {
AVCaptureFocusModeLocked = 0,
AVCaptureFocusModeAutoFocus = 1,
AVCaptureFocusModeContinuousAutoFocus = 2,
};
typedef NSInteger AVCaptureFocusMode;
曝光量調節
enum {
AVCaptureExposureModeLocked = 0,
AVCaptureExposureModeAutoExpose = 1,
AVCaptureExposureModeContinuousAutoExposure = 2,
};
typedef NSInteger AVCaptureExposureMode;
白平衡
enum {
AVCaptureWhiteBalanceModeLocked = 0,
AVCaptureWhiteBalanceModeAutoWhiteBalance = 1,
AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance = 2,
};
typedef NSInteger AVCaptureWhiteBalanceMode;
下面實現一個最簡單的手電筒功能:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(100, 100, 100, 40);
btn.backgroundColor = [UIColor clearColor];
[btn setTitle:@"打開手電筒" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(flashMode:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)flashMode:(id)sender
{
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; //This method returns the default device of the given media type currently available on the system.
UIButton *btn = (UIButton *)sender;
if([device hasTorch]){ //如果設備有手電筒功能
if (device.torchMode == AVCaptureTorchModeOff) //如果手電筒關閉
{
[btn setTitle:@"關閉手電筒" forState:UIControlStateNormal];
[device lockForConfiguration:nil]; //鎖定配置
[device setTorchMode:AVCaptureTorchModeOn]; //開啟手電筒
[device unlockForConfiguration]; //解鎖
}else{
[btn setTitle:@"打開手電筒" forState:UIControlStateNormal];
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOff];
[device unlockForConfiguration];
}
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"您的設備不支持此功能" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
其他Media Type的聲明
NSString *const AVMediaTypeVideo;
NSString *const AVMediaTypeAudio;
NSString *const AVMediaTypeText;
NSString *const AVMediaTypeClosedCaption;
NSString *const AVMediaTypeSubtitle;
NSString *const AVMediaTypeTimecode;
NSString *const AVMediaTypeTimedMetadata;
NSString *const AVMediaTypeMuxed;
若是要檢測裝置是否提供該功能,可以透過
- (BOOL)hasMediaType:(NSString *)mediaType
來取得
取得攝像頭後,我們可以透過
@property(nonatomic, readonly) BOOL hasTorch
@property(nonatomic, readonly) BOOL hasFlash
來判斷該攝像頭是否有提供閃光燈
我是要持續開啟所以使用Torch Mode
lockForConfiguration跟unlockForConfiguration是配對的API
呼叫lockForConfiguration就可以控制硬件了
控制完畢後要呼叫unlockForConfiguration
[device setTorchMode: AVCaptureTorchModeOn];
[device setTorchMode: AVCaptureTorchModeOff];
這兩行代碼,就是開關閃光燈的代碼
注意此代碼要在真機下作用
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言