检查iOS应用程序中当前使用的摄像头

时间:2013-05-09 11:28:14

标签: ios objective-c camera avcapturedevice avcam

我正在编写一个应用程序,它具有用于使用相机拍摄照片的自定义视图,类似于Apple的AVCam。在其中,我希望每次切换相机时按钮都会消失并重新显示闪光灯图标。 IE使用前置摄像头时,闪光灯按钮不应该在那里,当使用后面时它应该是!

目前我的代码是:

  AVCaptureDevicePosition position = [[videoInput device] position];

    if (position == AVCaptureDevicePositionBack) {
  self.flashButton.hidden == YES;
}

但是它在videoInput上出现了一个错误,我不知道为什么......你可以指导我的任何文档或我的代码更改的想法将非常感激!

修改

基本上只是为什么它会在这段代码中出现'使用未声明的标识符'的错误:

AVCaptureDevicePosition position = [[videoInput device] position];

3 个答案:

答案 0 :(得分:2)

以下代码可能会对您有所帮助:

AVCaptureDeviceInput *newVideoInput;
AVCaptureDevicePosition currentCameraPosition = [[videoInput device] position];

if (currentCameraPosition == AVCaptureDevicePositionBack)
{
    currentCameraPosition = AVCaptureDevicePositionFront;
}
else
{
    currentCameraPosition = AVCaptureDevicePositionBack;
}

AVCaptureDevice *backFacingCamera = nil;
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) 
{
    if ([device position] == currentCameraPosition)
    {
        backFacingCamera = device;
    }
}
newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:backFacingCamera error:&error];

答案 1 :(得分:0)

我一直在寻找类似问题的解决方案,并提出了这个问题,它可能对您有用(仅在iOS8中测试并使用Swift编写):

var captureDevice : AVCaptureDevice?

...

var currentDevice:String = captureDevice?.localizedName as String!

if currentDevice.rangeOfString("Back Camera") != nil {
   //hide flash icon
} else if currentDevice.rangeOfString("Front Camera") != nil {
   //show flash icon
}

此代码假定您已经正确设置了相机

注意:这可能不是最佳方式,因为如果Apple决定更改localizedName,它将会中断。并且,我知道这个问题很古老,但它可能会帮助其他人偶然发现它

答案 2 :(得分:0)

Swift 5.2

AVCaptureDevice是一个枚举。您可以像这样检查它:

#include <stdio.h>

struct SingleLinkedList
{
    int data;
    struct SingleLinkedList* next;
};

typedef struct SingleLinkedList sll;

void insertAtStart(sll** head, int data)
{
    sll newNode = { data, NULL };
    newNode.data = data;
    if (*head == NULL)
    {
        *head = &newNode;
        return;
    }
    newNode.next = *head;
    *head = &newNode;
}

void insertAtEnd(sll** head, int data)
{
    sll newNode = { data, NULL };
    newNode.data = data;
    if (*head == NULL)
    {
        *head = &newNode;
        return;
    }
    sll* node = *head;
    while (node -> next != NULL)
    {
        node = node -> next;
    }
    node -> next = &newNode;
}

void insertAfterNode(sll **head, int data)
{
    int nodeNum, count = 1;
    printf("\nEnter the node number to insert after: ");
    scanf("%d", &nodeNum);
    if (*head == NULL)
    {
        printf("\nThe List is empty!\n");
        return;
    }
    sll *prevNode = *head;
    while (count < nodeNum && prevNode -> next != NULL)
    {
        prevNode = prevNode -> next;
        count++;
    }
    if (count < nodeNum)
    {
        printf("\nThere are only %d nodes in the list\n", count);
        return;
    }

    sll newNode = { data, NULL };
    newNode.next = prevNode -> next;
    prevNode -> next = &newNode;
}

int choiceSelection()
{
    int choice;
    printf("\nSelect an Option:\n");
    printf("1. Insert At Beginning\n");
    printf("2. Insert At Last\n");
    printf("3. Insert After Certain Node\n");
    printf("4. Print all nodes\n");
    printf("5. Exit\n");
    scanf("%d", &choice);
    return choice;
}

int dataEntry()
{
    int data;
    printf("\nEnter the data: ");
    scanf("%d", &data);
    return data;
}

void print(sll* node)
{
    int count = 1;
    while(node != NULL)
    {
        printf("\n----------------%d----------------\n", count);
        printf("Data: %d", node -> data);
        printf("\tAddress: %p", node);
        printf("\tNext: %p\n", node -> next);
        node = node -> next;
        count++;
    }
}

int main()
{
    sll *head = NULL;
    enum option {
        InsertAtStart = 1,
        InsertAtEnd = 2,
        InsertAfterNode = 3,
        Print = 4,
        Exit = 5,
    } choice;

    while (choice != Exit)
    {
        choice = choiceSelection();
        switch (choice)
        {
            case InsertAtStart:
                insertAtStart(&head, dataEntry());
                break;
            case InsertAtEnd:
                insertAtEnd(&head, dataEntry());
                break;
            case InsertAfterNode:
                insertAfterNode(&head, dataEntry());
                break;
            case Print:
                print(head);
                break;
            case Exit:
                break;

            default:
                printf("\nIncorrect Choice..Please choose among 1, 2, 3, 4, 5\n");
                break;
        }
    }
    printf("\nExiting!");
    return 0;
}