iOS拍照定制之AVCaptureVideoDataOutput

  • 问题

    领导看了前面做的拍照,问了句"哪来的声音",

    “系统的,自带的,你看系统的拍照也有声音”

    “有办法能去掉吗?挺糟心的”

    “我试试”

  • 思路

    路漫漫其修远兮,吾在度娘+SDK中求索

    拍砖AVCaptureVideoDataOutput, 代理方法中将CMSampleBufferRef转成UIImage

  • 上码

    • session设置不提

    • layer设置可参考上篇 [iOS拍照定制之AVCapturePhotoOutput] 以及 上上篇[iOS写在定制相机之前]

    • 获取摄像头、取到设备输入添加到session、初始化videoOutput添加入session

    AVCaptureDevice *device = [self cameraDevice];
    if (!device) {
        NSLog(@"取得后置摄像头出问题");
        return;;
    }
    
    NSError *error = nil;
    self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:nil];
    
    // 设备添加到会话中
    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转成UIImage, 该方法有所调整,截图整张图中的某一部分,按需设置。具体获取指定区域图片需自己调整
    - (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);
        
        // 获取指定区域图片
        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;
    }
    
    
  • 图有了,收工。怎么用图,业务该干活了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 冰与火之歌是指苹果操作系统iOS上的一款游戏,名为XPC。XPC是基于美国作家乔治·R·R·马丁的奇幻小说系列《冰与火之歌》(A Song of Ice and Fire)进行改编的角色扮演游戏。 在这款游戏中,玩家可以扮演小说中的不同角色,探索维斯特洛大陆和其他神秘土地,以及参与各种任务和战斗。游戏以庞大的游戏世界和精致的画面而闻名,借助iOS设备的强大性能,为玩家提供了极具沉浸感的游戏体验。玩家可以与其他玩家合作或对抗,体验维斯特洛大陆的权谋之战,探索危险的地下城市和试炼塔,挑战强大的敌人。 XPC游戏在iOS平台上采用了先进的游戏引擎和技术,为玩家带来了高品质的游戏图形和流畅的操作体验。游戏中包含了丰富多样的剧情和任务,玩家可以选择不同的决策路径和角色发展方向,体验到与小说相似的复杂性和深度。 此外,iOS冰与火之歌XPC还提供了多种多样的多人游戏模式,玩家可以与其他玩家一起组队探险、完成任务,或者参与竞技场对战,争夺排名和奖励。 总之,iOS冰与火之歌XPC是一款基于《冰与火之歌》小说系列的角色扮演游戏,为玩家提供了丰富的剧情、精美的画面和流畅的操作体验。它不仅让玩家能够亲身体验维斯特洛大陆的神秘和危险,还能与其他玩家一起探险和竞争,带来更多的乐趣和挑战。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值