前置摄像头的设备唯一标识是什么?

13

我正在尝试编写一个涉及前置和后置摄像头以及它们之间切换的应用程序。据我所知,在addVideoInput方法中,我必须更改其中的ID。

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

但是那些NSStrings是哪些ID呢?

或者,如果应该用另一种方式完成,请给出建议。

谢谢帮助!

3个回答

29

好的,我已经找到了解决方案。我不知道它是对还是错的,但它来自于http://www.bunnyhero.org/2010/08/15/turn-your-iphone-into-a-vampire-with-avfoundation-and-ios-4/

只需使用:

AVCaptureDevice *captureDevice = [self frontFacingCameraIfAvailable];

frontFacingCameraIfAvailable是什么:

-(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;
}

0

你可以使用通常获取前置摄像头

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

但是我绝对更愿意使用您接受的方法 - 这个方法根本不安全。


0
我在使用前置摄像头扫描QR码时遇到了问题。我查找了很多资源和库来解决这个问题。但是,库并不能满足我的需求,因为我需要自定义UI来进行扫描。而且,互联网上的QR码扫描代码也已经过时了。所以,通过调试并了解设备类型,我应用了相机的位置,最终解决了问题。我将这篇文章发布为答案,希望能帮助像我一样寻找答案的同行。
- (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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接