這篇文章給大家介紹怎么在iOS中利用攝像頭獲取環(huán)境光感參數(shù),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

成都創(chuàng)新互聯(lián)成立與2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站制作、成都做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元施甸做網(wǎng)站,已為上家服務(wù),為施甸各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220
#import "LightSensitiveViewController.h"
@import AVFoundation;
#import <ImageIO/ImageIO.h>
@interface LightSensitiveViewController ()< AVCaptureVideoDataOutputSampleBufferDelegate>
@property (nonatomic, strong) AVCaptureSession *session;
@end
@implementation LightSensitiveViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"光感";
[self lightSensitive];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark- 光感
- (void)lightSensitive {
// 1.獲取硬件設(shè)備
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// 2.創(chuàng)建輸入流
AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc]initWithDevice:device error:nil];
// 3.創(chuàng)建設(shè)備輸出流
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[output setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
// AVCaptureSession屬性
self.session = [[AVCaptureSession alloc]init];
// 設(shè)置為高質(zhì)量采集率
[self.session setSessionPreset:AVCaptureSessionPresetHigh];
// 添加會話輸入和輸出
if ([self.session canAddInput:input]) {
[self.session addInput:input];
}
if ([self.session canAddOutput:output]) {
[self.session addOutput:output];
}
// 9.啟動會話
[self.session startRunning];
}
#pragma mark- AVCaptureVideoDataOutputSampleBufferDelegate的方法
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CFDictionaryRef metadataDict = CMCopyDictionaryOfAttachments(NULL,sampleBuffer, kCMAttachmentMode_ShouldPropagate);
NSDictionary *metadata = [[NSMutableDictionary alloc] initWithDictionary:(__bridge NSDictionary*)metadataDict];
CFRelease(metadataDict);
NSDictionary *exifMetadata = [[metadata objectForKey:(NSString *)kCGImagePropertyExifDictionary] mutableCopy];
float brightnessValue = [[exifMetadata objectForKey:(NSString *)kCGImagePropertyExifBrightnessValue] floatValue];
NSLog(@"%f",brightnessValue);
// 根據(jù)brightnessValue的值來打開和關(guān)閉閃光燈
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
BOOL result = [device hasTorch];// 判斷設(shè)備是否有閃光燈
if ((brightnessValue < 0) && result) {// 打開閃光燈
[device lockForConfiguration:nil];
[device setTorchMode: AVCaptureTorchModeOn];//開
[device unlockForConfiguration];
}else if((brightnessValue > 0) && result) {// 關(guān)閉閃光燈
[device lockForConfiguration:nil];
[device setTorchMode: AVCaptureTorchModeOff];//關(guān)
[device unlockForConfiguration];
}
}
@end注意點:
首先引入AVFoundation框架和ImageIO/ImageIO.h聲明文件
遵循AVCaptureVideoDataOutputSampleBufferDelegate協(xié)議
AVCaptureSession對象要定義為屬性,確保有對象在一直引用AVCaptureSession對象;否則如果在lightSensitive方法中定義并初始化AVCaptureSession對象,會造成AVCaptureSession對象提前釋放, [self.session startRunning];會失效
實現(xiàn)AVCaptureVideoDataOutputSampleBufferDelegate的代理方法,參數(shù)brightnessValue就是周圍環(huán)境的亮度參數(shù)了,范圍大概在-5~~12之間,參數(shù)數(shù)值越大,環(huán)境越亮
關(guān)于怎么在iOS中利用攝像頭獲取環(huán)境光感參數(shù)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)站名稱:怎么在iOS中利用攝像頭獲取環(huán)境光感參數(shù)
URL標(biāo)題:http://chinadenli.net/article36/gsgjsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、ChatGPT、品牌網(wǎng)站制作、Google、動態(tài)網(wǎng)站、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)