BarcodeScanner咋调用iPad前置摄像头的苦恼

FEI 发布于 2014/07/21 13:58
阅读 1K+
收藏 1
在iOS平台用到 BarcodeScanner进行扫描二维码的时候,发布到iPad上后,要怎么样才能调用前置摄像头呢?发现默认是只能用后置的那个摄像头进行扫描的。
加载中
0
FEI
FEI
#import <AVFoundation/AVFoundation.h>
 
// Switching between front and back cameras
 
- (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;
        }
    } 
}




返回顶部
顶部