iOS拍照定制之AVCaptureVideoDataOutput

  • 問題

    領(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ù)該干活了

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末鞭衩,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子攒射,更是在濱河造成了極大的恐慌醋旦,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,188評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件会放,死亡現(xiàn)場(chǎng)離奇詭異饲齐,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)咧最,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門捂人,熙熙樓的掌柜王于貴愁眉苦臉地迎上來御雕,“玉大人,你說我怎么就攤上這事滥搭∷岣伲” “怎么了?”我有些...
    開封第一講書人閱讀 165,562評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵瑟匆,是天一觀的道長(zhǎng)闽坡。 經(jīng)常有香客問我,道長(zhǎng)愁溜,這世上最難降的妖魔是什么疾嗅? 我笑而不...
    開封第一講書人閱讀 58,893評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮冕象,結(jié)果婚禮上代承,老公的妹妹穿的比我還像新娘。我一直安慰自己渐扮,他們只是感情好论悴,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著墓律,像睡著了一般膀估。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上耻讽,一...
    開封第一講書人閱讀 51,708評(píng)論 1 305
  • 那天玖像,我揣著相機(jī)與錄音,去河邊找鬼齐饮。 笑死捐寥,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的祖驱。 我是一名探鬼主播握恳,決...
    沈念sama閱讀 40,430評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼捺僻!你這毒婦竟也來了乡洼?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,342評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤匕坯,失蹤者是張志新(化名)和其女友劉穎束昵,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體葛峻,經(jīng)...
    沈念sama閱讀 45,801評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡锹雏,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評(píng)論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了术奖。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片礁遵。...
    茶點(diǎn)故事閱讀 40,115評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡轻绞,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出佣耐,到底是詐尸還是另有隱情政勃,我是刑警寧澤,帶...
    沈念sama閱讀 35,804評(píng)論 5 346
  • 正文 年R本政府宣布兼砖,位于F島的核電站奸远,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏讽挟。R本人自食惡果不足惜然走,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望戏挡。 院中可真熱鬧,春花似錦晨仑、人聲如沸褐墅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽妥凳。三九已至,卻和暖如春答捕,著一層夾襖步出監(jiān)牢的瞬間逝钥,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工拱镐, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留艘款,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,365評(píng)論 3 373
  • 正文 我出身青樓沃琅,卻偏偏與公主長(zhǎng)得像哗咆,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子益眉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容