开发者

What is the front camera's deviceUniqueID?

I am trying to write an app that involves both front and rear camera and switching between them. As far as I understand, in the addVideoInput method, I ha开发者_运维技巧ve to change the IDs in

    AVCaptureDevice *videoDevice = [AVCaptureDevice deviceWithUniqueID:(NSString *)deviceUniqueID];

But which NSStrings are those IDs?

Or, if it should be done in the other way, please, give a suggestion.

Thank you for help!


Ok, I have managed to find out a solution. I don't know if it's right or wrong, it was taken from http://www.bunnyhero.org/2010/08/15/turn-your-iphone-into-a-vampire-with-avfoundation-and-ios-4/

Just use

AVCaptureDevice *captureDevice = [self frontFacingCameraIfAvailable];

where frontFacingCameraIfAvailable is:

-(AVCaptureDevice *)frontFacingCameraIfAvailable
{
    NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    AVCaptureDevice *captureDevice = nil;
    for (AVCaptureDevice *device in videoDevices)
    {
        if (device.position == AVCaptureDevicePositionFront)
        {
            captureDevice = device;
            break;
        }
    }

    //  couldn't find one on the front, so just get the default video device.
    if ( ! captureDevice)
    {
        captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    }

    return captureDevice;
}


You can usually get frontal camera using

AVCaptureDevice *frontalCamera = [AVCaptureDevice deviceWithUniqueID:@"com.apple.avfoundation.avcapturedevice.built-in_video:1"];

But I would by all means rather use your accepted method - this one is not safe at all.


I was facing issue scanning QR Code with Front Camera. I looked out for so many resources and libraries in order to do so. Library was not fulfilling my requirement as I needed customised UI for scanner. And the piece of code on the internet to scan QR Code was also deprecated. So by debugging and knowing the device type I applied position of the camera and it worked. I am posting this as an answer so that it will help another peer like me looking for the answer.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupScanner];
    [self openScanner:nil];
}

#pragma mark- Actions

- (IBAction)openScanner:(id)sender {
    if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]){
        [self.session startRunning];
    }
}

- (IBAction)stopScanner:(id)sender {
    [self.session stopRunning];
}

- (void)setupScanner {

    #if !(TARGET_OS_SIMULATOR)
    
    //self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    self.device = [self frontFacingCameraIfAvailable];
    
    self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];

    self.session = [[AVCaptureSession alloc] init];

    self.output = [[AVCaptureMetadataOutput alloc] init];
    
    if([self.session canAddOutput:self.output]) {
        [self.session addOutput:self.output];
    }
    
    if ([self.session canAddInput:self.input]){
        [self.session addInput:self.input];
    }

    [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [self.output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];

    self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
    self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.preview.frame = CGRectMake(0, 0, CGRectGetWidth(self.pLayer.frame), CGRectGetHeight(self.pLayer.frame));

    AVCaptureConnection *con = self.preview.connection;

    con.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
    //pLayer is a UIView outlet on which the scanner fits or occupies its area to scan QR Code
    [self.pLayer.layer insertSublayer:self.preview atIndex:0];
     
    #endif
}

#pragma mark - AVCaptureMetadataOutputObjectsDelegate
    
- (void)captureOutput:(AVCaptureOutput *)output didOutputMetadataObjects:(NSArray<__kindof AVMetadataObject *> *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
  
    CGRect highlightViewRect = CGRectZero;
    AVMetadataMachineReadableCodeObject *barCodeObject;
    NSString *detectionString = nil;
    NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code,
            AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code,
            AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];

    for (AVMetadataObject *metadata in metadataObjects) {
        for (NSString *type in barCodeTypes) {
            if ([metadata.type isEqualToString:type])
            {
                barCodeObject = (AVMetadataMachineReadableCodeObject *)[self.preview transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];

                highlightViewRect = barCodeObject.bounds;
                detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
                break;
            }
        }
        
        if (detectionString != nil) {
            self.codeLabel.text = detectionString;
            [self stopScanner:nil];
            //Do your work with QR Code String ---
            break;
        }
        else
            self.codeLabel.text = @"CODE";
    }
}


#pragma mark- Capture Device

-(AVCaptureDevice *)frontFacingCameraIfAvailable {
    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionFront];
    NSLog(@"capture device %@",captureDevice.description);
    NSLog(@"device type %@",captureDevice.deviceType);
    NSLog(@"unique Id: %@",captureDevice.uniqueID);
    //com.apple.avfoundation.avcapturedevice.built-in_video:1
    //Device Position: 2
    NSLog(@"frontFacingCameraIfAvailable-> Device Position: %ld",(long)captureDevice.position);
    return captureDevice;
}

-(AVCaptureDevice *)backFacingCameraIfAvailable {
    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSLog(@"capture device %@",captureDevice.description);
    NSLog(@"device type %@",captureDevice.deviceType);
    NSLog(@"unique Id: %@",captureDevice.uniqueID);
    NSLog(@"backFacingCameraIfAvailable-> Device Position: %ld",(long)captureDevice.position);
    return captureDevice;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜