代码之家  ›  专栏  ›  技术社区  ›  Origamiguy

在iPhone应用程序中检测到摄像头的存在?

  •  60
  • Origamiguy  · 技术社区  · 14 年前

    那么,有人能给我提供返回是否有摄像头的代码吗?

    7 回复  |  直到 7 年前
        1
  •  167
  •   Vladimir    14 年前

    你可以用 +isSourceTypeAvailable: UIImagePickerController中的方法:

    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
       // Has camera
    
        2
  •  25
  •   Boaz Frenkel    4 年前

    作为 胡安·博罗 写检查:

        if UIImagePickerController.isSourceTypeAvailable(.camera) {...}
    

    但我会添加另一个检查,看看用户是否允许访问摄像头,就像苹果在他们的PhotoPicker示例中建议的那样( PhotoPicker example Objective-C

    请注意,您必须导入AVFoundation

    斯威夫特 5

        let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
        switch authStatus {
            /*
             Status Restricted -
             The client is not authorized to access the hardware for the media type. The user cannot change the client's status, possibly due to active restrictions such as parental controls being in place.
             */
        case .denied, .restricted:
            // Denied access to camera
            // Explain that we need camera access and how to change it.
            let dialog = UIAlertController(title: "Unable to access the Camera", message: "To enable access, go to Settings > Privacy > Camera and turn on Camera access for this app.", preferredStyle: UIAlertController.Style.alert)
    
            let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil)
    
            dialog.addAction(okAction)
            self.present(dialog, animated:true, completion:nil)
        case .notDetermined:
            // The user has not yet been presented with the option to grant access to the camera hardware.
            // Ask for it.
            AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: { (grantd) in
            // If access was denied, we do not set the setup error message since access was just denied.
               if grantd {
               // Allowed access to camera, go ahead and present the UIImagePickerController.
                self.showImagePickerForSourceType(sourceType: UIImagePickerController.SourceType.camera)
                }
            })
        case .authorized:
            // Allowed access to camera, go ahead and present the UIImagePickerController.
            self.showImagePickerForSourceType(sourceType: UIImagePickerController.SourceType.camera)
        @unknown default:
            break; //handle other status
        }
    

    斯威夫特

    let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
        
    if authStatus == AVAuthorizationStatus.denied {
        // Denied access to camera
        // Explain that we need camera access and how to change it.
        let dialog = UIAlertController(title: "Unable to access the Camera", message: "To enable access, go to Settings > Privacy > Camera and turn on Camera access for this app.", preferredStyle: UIAlertControllerStyle.alert)
            
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)
            
        dialog.addAction(okAction)
        self.present(dialog, animated:true, completion:nil)
            
    } else if authStatus == AVAuthorizationStatus.notDetermined {     // The user has not yet been presented with the option to grant access to the camera hardware.
        // Ask for it.
        AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (grantd) in
        // If access was denied, we do not set the setup error message since access was just denied.
           if grantd {
           // Allowed access to camera, go ahead and present the UIImagePickerController.
                self.showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType.camera)
            }
        })
    } else {
            
        // Allowed access to camera, go ahead and present the UIImagePickerController.
        self.showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType.camera)
    
    }
    
    func showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType) {
        
        let myPickerController = UIImagePickerController()
        myPickerController.delegate = self;
        myPickerController.sourceType = sourceType  
        self.present(myPickerController, animated: true, completion: nil)
    }
    
        3
  •  22
  •   prewett    11 年前

    如果使用AV基础类而不是UIImagePickerController,则可以执行以下操作:

    BOOL hasCamera = ([[AVCaptureDevice devices] count] > 0);
    

    如果您使用的是UIImagePickerController,那么这可能不值得,因为您必须添加AVFoundation.framework文件你的项目。

        4
  •  20
  •   Ben Zotto sberry    14 年前

    是的,提供了一个API来实现这一点:

    BOOL isCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    
        5
  •  12
  •   Juan Boero    8 年前

    银行代码:

    if UIImagePickerController.isSourceTypeAvailable(.Camera){
    
        //Your code goes here
        //For example you can print available media types:
    
        print(UIImagePickerController.availableMediaTypesForSourceType(.Camera))
    
        }
    
        6
  •  6
  •   RawMean    10 年前

    如果您需要知道该设备是否专门具有前置或后置摄像头,请使用以下选项:

    isCameraAvailable = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
    
        7
  •  -1
  •   Usman Nisar    8 年前

    检查摄像机是否可用(Swift)

    if(!UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
    
        8
  •  -1
  •   Jovan Stankovic    5 年前

    您可以使用发现会话(Swift 5)检查特定源类型的可用性:

    let discovery = AVCaptureDevice.DiscoverySession.init(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back)
    let isWideAngleCameraSupported = !discovery.devices.isEmpty