-
問題
領(lǐng)導(dǎo)看了前面做的拍照,問了句"哪來的聲音",
"系統(tǒng)的,自帶的扎狱,你看系統(tǒng)的拍照也有聲音"
"有辦法能去掉嗎琳猫?挺糟心的"
"我試試"
-
思路
路漫漫其修遠(yuǎn)兮伟叛,吾在度娘+SDK中求索
拍磚AVCaptureVideoDataOutput, 代理方法中將CMSampleBufferRef轉(zhuǎn)成UIImage
-
上碼
session設(shè)置不提
layer設(shè)置可參考上篇 [iOS拍照定制之AVCapturePhotoOutput] 以及 上上篇[iOS寫在定制相機(jī)之前]
獲取攝像頭、取到設(shè)備輸入添加到session脐嫂、初始化videoOutput添加入session
AVCaptureDevice *device = [self cameraDevice]; if (!device) { NSLog(@"取得后置攝像頭出問題"); return;; } NSError *error = nil; self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:nil]; // 設(shè)備添加到會(huì)話中 if ([self.captureSession canAddInput:self.videoInput]) { [self.captureSession addInput:self.videoInput]; } [self.videoOutput setSampleBufferDelegate:self queue:self.videoQueue]; if ([self.captureSession canAddOutput:self.videoOutput]) { [self.captureSession addOutput:self.videoOutput]; } // 懶加載 - (AVCaptureVideoDataOutput *)videoOutput { if (!_videoOutput) { _videoOutput = [[AVCaptureVideoDataOutput alloc] init]; _videoOutput.alwaysDiscardsLateVideoFrames = YES; _videoOutput.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]; } return _videoOutput; } - (dispatch_queue_t)videoQueue { if (!_videoQueue) { _videoQueue = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL); } return _videoQueue; }
- 代理AVCaptureVideoDataOutputSampleBufferDelegate
- (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { @autoreleasepool { if (connection == [self.videoOutput connectionWithMediaType:AVMediaTypeVideo]) { // 視頻 @synchronized (self) { UIImage *image = [self bufferToImage:sampleBuffer rect:self.scanView.scanRect]; self.uploadImg = image; } } } }
- CMSampleBufferRef轉(zhuǎn)成UIImage, 該方法有所調(diào)整统刮,截圖整張圖中的某一部分,按需設(shè)置账千。具體獲取指定區(qū)域圖片需自己調(diào)整
- (UIImage *)bufferToImage:(CMSampleBufferRef)sampleBuffer rect:(CGRect)rect { // Get a CMSampleBuffer's Core Video image buffer for the media data CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); // Lock the base address of the pixel buffer CVPixelBufferLockBaseAddress(imageBuffer, 0); // Get the number of bytes per row for the pixel buffer void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer); // Get the number of bytes per row for the pixel buffer size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); // Get the pixel buffer width and height size_t width = CVPixelBufferGetWidth(imageBuffer); size_t height = CVPixelBufferGetHeight(imageBuffer); // Create a device-dependent RGB color space CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); // Create a bitmap graphics context with the sample buffer data CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); // Create a Quartz image from the pixel data in the bitmap graphics context CGImageRef quartzImage = CGBitmapContextCreateImage(context); // Unlock the pixel buffer CVPixelBufferUnlockBaseAddress(imageBuffer,0); // Free up the context and color space CGContextRelease(context); CGColorSpaceRelease(colorSpace); // 獲取指定區(qū)域圖片 CGRect dRect; CGSize msize = UIScreen.mainScreen.bounds.size; msize.height = msize.height - 150; CGFloat x = width * rect.origin.x / msize.width; CGFloat y = height * rect.origin.y / msize.height; CGFloat w = width * rect.size.width / msize.width; CGFloat h = height * rect.size.height / msize.height; dRect = CGRectMake(x, y, w, h); CGImageRef partRef = CGImageCreateWithImageInRect(quartzImage, dRect); // Create an image object from the Quartz image UIImage *image = [UIImage imageWithCGImage:partRef]; // Release the Quartz image CGImageRelease(partRef); CGImageRelease(quartzImage); return image; }
圖有了侥蒙,收工。怎么用圖匀奏,業(yè)務(wù)該干活了
iOS拍照定制之AVCaptureVideoDataOutput
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門捂人,熙熙樓的掌柜王于貴愁眉苦臉地迎上來御雕,“玉大人,你說我怎么就攤上這事滥搭∷岣伲” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵瑟匆,是天一觀的道長(zhǎng)闽坡。 經(jīng)常有香客問我,道長(zhǎng)愁溜,這世上最難降的妖魔是什么疾嗅? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮冕象,結(jié)果婚禮上代承,老公的妹妹穿的比我還像新娘。我一直安慰自己渐扮,他們只是感情好论悴,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著墓律,像睡著了一般膀估。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上耻讽,一...
- 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼捺僻!你這毒婦竟也來了乡洼?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬榮一對(duì)情侶失蹤匕坯,失蹤者是張志新(化名)和其女友劉穎束昵,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體葛峻,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡锹雏,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了术奖。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片礁遵。...
- 正文 年R本政府宣布兼砖,位于F島的核電站奸远,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏讽挟。R本人自食惡果不足惜然走,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望戏挡。 院中可真熱鬧,春花似錦晨仑、人聲如沸褐墅。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽妥凳。三九已至,卻和暖如春答捕,著一層夾襖步出監(jiān)牢的瞬間逝钥,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓沃琅,卻偏偏與公主長(zhǎng)得像哗咆,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子益眉,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 1.引入頭文件 #import 代理 2.新建屬性 @property(strong,nonatomic)AVC...
- Osho 相機(jī)是我獨(dú)立開發(fā)上架的一個(gè)相機(jī) App晌柬,App Store地址:點(diǎn)我。它支持1:1郭脂,4:3年碘,16:9多種...
- #import "XMMainViewController.h" #import #import #import ...
- pragma mark 獲取掃描到的圖片 (void)captureOutput:(AVCaptureOutput...
- 今天感恩節(jié)哎,感謝一直在我身邊的親朋好友展鸡。感恩相遇屿衅!感恩不離不棄。 中午開了第一次的黨會(huì)莹弊,身份的轉(zhuǎn)變要...