ios capturing image using AVFramework

后端 未结 3 663
轻奢々
轻奢々 2020-12-04 11:20

I\'m capturing images using this code

#pragma mark - image capture

// Create and configure a capture session and start it running
- (void)setupCaptureSessio         


        
3条回答
  •  Happy的楠姐
    2020-12-04 12:02

    And here is a Swift version of imageFromSampleBuffer function:

    func imageFromSampleBuffer(sampleBuffer:CMSampleBuffer!) -> UIImage {
        let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!
        CVPixelBufferLockBaseAddress(imageBuffer, 0)
    
        let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer)
        let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer)
        let width = CVPixelBufferGetWidth(imageBuffer)
        let height = CVPixelBufferGetHeight(imageBuffer)
    
        let colorSpace = CGColorSpaceCreateDeviceRGB()
    
        let bitmapInfo:CGBitmapInfo = [.ByteOrder32Little, CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)]
        let context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, bitmapInfo.rawValue)
    
        let quartzImage = CGBitmapContextCreateImage(context)
        CVPixelBufferUnlockBaseAddress(imageBuffer, 0)
    
        let image = UIImage(CGImage: quartzImage!)
        return image
    }
    

    Above working for me with following video settings:

    videoDataOutput = AVCaptureVideoDataOutput()
    videoDataOutput?.videoSettings = [kCVPixelBufferPixelFormatTypeKey:Int(kCVPixelFormatType_32BGRA)]
                videoDataOutput?.setSampleBufferDelegate(self, queue: queue)
    

提交回复
热议问题