如何使用 AVCaptureFlashMode

发布于 2024-09-08 15:27:52 字数 1488 浏览 8 评论 0原文

我正在制作一个应用程序,可以将图像拼接成全景场景。我希望能够以编程方式打开 iPhone 4 上的闪光灯 LED。

我该怎么做?

我阅读了文档并发现我需要使用 AVCaptureFlashMode

但我不知道 2 如何使用它?

任何帮助将不胜感激。


更新了下面的代码。谢谢西弗!

NSError* error = nil;
    NSLog(@"Setting up LED");

    if([captDevice hasTorch] == NO)
    {
        NSLog(@"Error: This device doesnt have a torch");
    }
    if([captDevice isTorchModeSupported:AVCaptureTorchModeOn] == NO)
    {
        NSLog(@"Error: This device doesnt support AVCaptureTorchModeOn");
    }

    AVCaptureSession* captureSession = [[AVCaptureSession alloc] init];
    AVCaptureDeviceInput* videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:captDevice error:&error];
    AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];

    if (videoInput && videoOutput) 
    {
        [captureSession addInput:videoInput];
        [captureSession addOutput:videoOutput];
        if([captDevice lockForConfiguration:&error])
        {
            if (flag == YES) {
                captDevice.torchMode = AVCaptureTorchModeOn;
            } else {
                captDevice.torchMode = AVCaptureTorchModeOff;
            }           
            [captDevice unlockForConfiguration];
        }
        else 
        {
            NSLog(@"Could not lock device for config error: %@", error);
        }
        [captureSession startRunning];
    }
    else 
    {
        NSLog(@"Error: %@", error);
    }

如何将其关闭?

I'm making an app to allow me to stitch images together into a panoramic scene. I want to be able to turn the Flash LED on the iPhone 4 programatically.

How can I do this?

I read the documentation and discovered that I need to use AVCaptureFlashMode

but I can't figure out how 2 use it?

any help would be appreciated.


Updated Code below. Thanks SIF!

NSError* error = nil;
    NSLog(@"Setting up LED");

    if([captDevice hasTorch] == NO)
    {
        NSLog(@"Error: This device doesnt have a torch");
    }
    if([captDevice isTorchModeSupported:AVCaptureTorchModeOn] == NO)
    {
        NSLog(@"Error: This device doesnt support AVCaptureTorchModeOn");
    }

    AVCaptureSession* captureSession = [[AVCaptureSession alloc] init];
    AVCaptureDeviceInput* videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:captDevice error:&error];
    AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];

    if (videoInput && videoOutput) 
    {
        [captureSession addInput:videoInput];
        [captureSession addOutput:videoOutput];
        if([captDevice lockForConfiguration:&error])
        {
            if (flag == YES) {
                captDevice.torchMode = AVCaptureTorchModeOn;
            } else {
                captDevice.torchMode = AVCaptureTorchModeOff;
            }           
            [captDevice unlockForConfiguration];
        }
        else 
        {
            NSLog(@"Could not lock device for config error: %@", error);
        }
        [captureSession startRunning];
    }
    else 
    {
        NSLog(@"Error: %@", error);
    }

How do you turn it off?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你而言 2024-09-15 15:27:52
AVCaptureDevice* d = nil;

// find a device by position
NSArray* allDevices = [AVCaptureDevice devices];
for (AVCaptureDevice* currentDevice in allDevices) {
  if (currentDevice.position == AVCaptureDevicePositionBack) {
    d = currentDevice;
  }
}

// at this point, d may still be nil, assuming we found something we like....

NSError* err = nil;
BOOL lockAcquired = [d lockForConfiguration:&err];

if (!lockAcquired) {
   // log err and handle...
} else {
   // flip on the flash mode
   if ([d hasFlash] && [d isFlashModeSupported:AVCaptureFlashModeOn] ) {
      [d setFlashMode:AVCaptureFlashModeOn];
   }

   [d unlockForConfiguration];
}
AVCaptureDevice* d = nil;

// find a device by position
NSArray* allDevices = [AVCaptureDevice devices];
for (AVCaptureDevice* currentDevice in allDevices) {
  if (currentDevice.position == AVCaptureDevicePositionBack) {
    d = currentDevice;
  }
}

// at this point, d may still be nil, assuming we found something we like....

NSError* err = nil;
BOOL lockAcquired = [d lockForConfiguration:&err];

if (!lockAcquired) {
   // log err and handle...
} else {
   // flip on the flash mode
   if ([d hasFlash] && [d isFlashModeSupported:AVCaptureFlashModeOn] ) {
      [d setFlashMode:AVCaptureFlashModeOn];
   }

   [d unlockForConfiguration];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文