Friday, September 28, 2012

Switching between front and back cameras / camera before and after the code switch


Switching between front and back cameras / camera before and after the code switch

There are some new features of capture available in iOS4
AVCaptureDevice.flashmode

AVCaptureDevice.hasflash

AVCaptureDevice.hasTorch

AVCaptureDevicePositionFront

http://www.cocoachina.com/bbs/read.php?tid-22935.html


# Import 

/ / Switching between cameras Front and Back

- ( AVCaptureDevice * ) cameraWithPosition : ( AVCaptureDevicePosition ) position
{
NSArray * Devices = [ AVCaptureDevice devicesWithMediaType : AVMediaTypeVideo ] ;
for ( AVCaptureDevice * Device in Devices )
if ( Device . position == position )
return Device ;
return nil ;
}

- ( void ) swapFrontAndBackCameras {
/ / Assume the session is already running

NSArray * inputs = self . session.inputs;
for ( AVCaptureDeviceInput * INPUT in inputs ) {
AVCaptureDevice * Device = INPUT. 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 ] ;

/ / beginConfiguration ensures that pending changes are not applied immediately
[ self . session beginConfiguration ] ;

[ self . session removeInput : INPUT ] ;
[ self . session addInput : newInput ] ;

/ / Changes take effect once the outermost commitConfiguration is invoked.
[ self . session commitConfiguration ] ;
break ;
}
}
}

No comments:

Post a Comment