AVCaptureDevice.h主要用来获取iPhone一些关于相机设备的属性。
①前置和后置摄像头
enum {
AVCaptureDevicePositionBack = 1,
AVCaptureDevicePositionFront = 2
};
typedef NSInteger AVCaptureDevicePosition;
判断设备是否有摄像头
-(BOOL) isCameraAvailable{
return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}
判断前摄像头是否有用
- (BOOL) isFrontCameraAvailable{
return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
}
判断后摄像头是否有用
- (BOOL) isRearCameraAvailable{
return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
}
切换摄像头
- (BOOL) hasMultipleCameras {
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
if (devices != nil && [devices count] > 1) return YES;
return NO;
}
- (AVCaptureDevice *)cameraWithPosition : (AVCaptureDevicePosition)position{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices ){
if ( device.position == position )return device;
return nil ;
}
}
- (void) swapFrontAndBackCameras {
if (![self hasMultipleCameras]) return;
NSArray *inputs = self.captureSession.inputs;
for ( AVCaptureDeviceInput *captureDeviceInput in inputs ) {
AVCaptureDevice *device = captureDeviceInput.device ;
if ( [device hasMediaType:AVMediaTypeVideo ] ) {
AVCaptureDevicePosition position = device.position ;
AVCaptureDevice *newCamera = nil ;
AVCaptureDeviceInput *newInput = nil ;
if (position == AVCaptureDevicePositionFront){
newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack];
}else{
newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront];
}
newInput = [AVCaptureDeviceInput deviceInputWithDevice:newCamera error:nil];
[self.captureSession beginConfiguration ];
[self.captureSession removeInput:captureDeviceInput]; //remove current
[self.captureSession addInput:newInput];
[self.captureSession commitConfiguration];
break ;
}
}
}
②闪光灯开关
enum {
AVCaptureFlashModeOff = 0,
AVCaptureFlashModeOn = 1,
AVCaptureFlashModeAuto = 2
};
typedef NSInteger AVCaptureFlashMode;
开启闪光灯
AVCaptureDevice* device = nil;
NSArray* allDevices = [AVCaptureDevice devices];
for (AVCaptureDevice* currentDevice in allDevices) {
if (currentDevice.position == AVCaptureDevicePositionBack) {
device = currentDevice;
}
}
NSError* err = nil;
BOOL lockAcquired = [device lockForConfiguration:&err];
if (!lockAcquired) {
// 此设备不支持或其他原因
} else {
// 打开闪光模式
if ([device hasFlash] && [device isFlashModeSupported:AVCaptureFlashModeOn] ) {
[device setFlashMode:AVCaptureFlashModeOn];
}
[device unlockForConfiguration];
}
③手电筒开关
enum {
AVCaptureTorchModeOff = 0,
AVCaptureTorchModeOn = 1,
AVCaptureTorchModeAuto = 2,
};
typedef NSInteger AVCaptureTorchMode;
切换手电筒开关
- (void)isOpenFlashLight:(UIButton *)btn{
self.isOpenFlashlight = !self.isOpenFlashlight;
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (self.isOpenFlashlight) {
if (device.torchMode == AVCaptureTorchModeOff) {//torch手电筒模式
[device lockForConfiguration:nil];//设备锁
[device setTorchMode:AVCaptureTorchModeOn];//手电筒打开
[device unlockForConfiguration];//不需要自动调用
}
}else{
if (device.torchMode == AVCaptureTorchModeOn) {
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOff];
[device unlockForConfiguration];
}
}
}
④焦距
enum {
AVCaptureFocusModeLocked = 0,
AVCaptureFocusModeAutoFocus = 1,
AVCaptureFocusModeContinuousAutoFocus = 2,
};
typedef NSInteger AVCaptureFocusMode;
调整焦距
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MobileCoreServices/MobileCoreServices.h>
#define XBScreenWidth [UIScreen mainScreen].bounds.size.width
#define XBScreenHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<AVCaptureVideoDataOutputSampleBufferDelegate>
@property (strong, nonatomic) AVCaptureSession *avSession;
@property (strong, nonatomic) AVCaptureDevice *backCameraDevice;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupSession];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (!_avSession.isRunning) {
[_avSession startRunning];
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if (_avSession.isRunning) {
[_avSession stopRunning];
}
}
- (AVCaptureDevice *)backCamera
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if (device.position == AVCaptureDevicePositionBack) {
return device;
}
}
return nil;
}
- (void)setupSession
{
//创建session会话
_avSession = [[AVCaptureSession alloc] init];
[_avSession beginConfiguration];
_avSession.sessionPreset = AVCaptureSessionPreset640x480;
//通过capture对象创建输入设备对象
NSError *error = nil;
_backCameraDevice = [self backCamera];
[_backCameraDevice lockForConfiguration:&error];
_backCameraDevice.focusMode = AVCaptureFocusModeLocked;
[_backCameraDevice unlockForConfiguration];
AVCaptureDeviceInput *inputDevice = [AVCaptureDeviceInput deviceInputWithDevice:_backCameraDevice error:&error];
//将输入设备添加到会话
if ([_avSession canAddInput:inputDevice]) {
[_avSession addInput:inputDevice];
}else{
NSLog(@"不能添加视频输入设备");
return;
}
//添加一个输出设备
AVCaptureVideoDataOutput *videoOutput = [[AVCaptureVideoDataOutput alloc] init];
videoOutput.videoSettings = @{(__bridge NSString *)kCVPixelBufferPixelFormatTypeKey : [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]};
videoOutput.alwaysDiscardsLateVideoFrames = YES;
if ([_avSession canAddOutput:videoOutput]) {
[_avSession addOutput:videoOutput];
}else{
NSLog(@"不能添加视频输出设备");
return;
}
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_avSession];
//只有设置GravityResizeAspectFill或GravityResize,然后设置frame才有效,图像不会按照frame的大小显示
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
previewLayer.frame = CGRectMake(0, 20, 320, 240);
[self.view.layer addSublayer:previewLayer];
[_avSession commitConfiguration];
}
//UISlider 绑定的方法
- (IBAction)foucsChange:(UISlider *)sender {
NSError *error = nil;
[_backCameraDevice lockForConfiguration:&error];
[_backCameraDevice setFocusModeLockedWithLensPosition:sender.value completionHandler:nil];
[_backCameraDevice unlockForConfiguration];
}
⑤曝光量调整
enum {
AVCaptureExposureModeLocked = 0,
AVCaptureExposureModeAutoExpose = 1,
AVCaptureExposureModeContinuousAutoExposure = 2,
};
typedef NSInteger AVCaptureExposureMode;
⑥白平衡
enum {
AVCaptureWhiteBalanceModeLocked = 0,
AVCaptureWhiteBalanceModeAutoWhiteBalance = 1,
AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance = 2,
};
typedef NSInteger AVCaptureWhiteBalanceMode;