Can't get pixel data via CGDataProviderCopyData using AVCaptureVideoDataOutput in swift 2

天涯浪子 提交于 2019-12-08 04:41:10

问题


I'm working on updating this for swift 2.0 and I currently get fatal error: unexpectedly found nil while unwrapping an Optional value on line: let data = CGDataProviderCopyData(CGImageGetDataProvider(imageRef)) as! NSData

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
print("Capture output running")
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
CVPixelBufferLockBaseAddress(imageBuffer!, 0)

let baseAddress = CVPixelBufferGetBaseAddressOfPlane(imageBuffer!, 0)
let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!)
let width = CVPixelBufferGetWidth(imageBuffer!)
let height = CVPixelBufferGetHeight(imageBuffer!)
let colorSpace = CGColorSpaceCreateDeviceRGB()

let bitmapInfo = UInt32(CGBitmapInfo.ByteOrder32Big.rawValue)
//Original version of above line
//var bitmapInfo = CGBitmapInfo.fromRaw(CGImageAlphaInfo.PremultipliedFirst.toRaw())! | CGBitmapInfo.ByteOrder32Little

let context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, bitmapInfo)
let imageRef = CGBitmapContextCreateImage(context)
CVPixelBufferUnlockBaseAddress(imageBuffer!, 0)

let data = CGDataProviderCopyData(CGImageGetDataProvider(imageRef)) as! NSData

let pixels = UnsafePointer<UInt8>(data.bytes)

let imageSize : Int = Int(width) * Int(height) * 4

var newPixelArray = [UInt8](count: imageSize, repeatedValue: 0)

for index in 0.stride(to: data.length, by: 4) {
    newPixelArray[index] = 255 - pixels[index]
    newPixelArray[index + 1] = 255 - pixels[index + 1]
    newPixelArray[index + 2] = 255 - pixels[index + 2]
    newPixelArray[index + 3] = pixels[index + 3]
    print(newPixelArray[index])
}
//remainder of function
}

回答1:


Fixed! Note the let data:NSData... line

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
    print("Capture output running")
    let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
    CVPixelBufferLockBaseAddress(imageBuffer!, 0)

    let baseAddress = CVPixelBufferGetBaseAddressOfPlane(imageBuffer!, 0)
    let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!)
    let width = CVPixelBufferGetWidth(imageBuffer!)
    let height = CVPixelBufferGetHeight(imageBuffer!)
    let colorSpace = CGColorSpaceCreateDeviceRGB()

    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue).rawValue | CGBitmapInfo.ByteOrder32Little.rawValue

    let context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, bitmapInfo)
    let imageRef = CGBitmapContextCreateImage(context)
    CVPixelBufferUnlockBaseAddress(imageBuffer!, 0)


    let data:NSData = CGDataProviderCopyData(CGImageGetDataProvider(imageRef))!

    let pixels = UnsafePointer<UInt8>(data.bytes)

    let imageSize : Int = Int(width) * Int(height) * 4

    var newPixelArray = [UInt8](count: imageSize, repeatedValue: 0)

    for index in 0.stride(to: data.length, by: 4) {
        newPixelArray[index] = 255 - pixels[index]
        newPixelArray[index + 1] = 255 - pixels[index + 1]
        newPixelArray[index + 2] = 255 - pixels[index + 2]
        newPixelArray[index + 3] = pixels[index + 3]
        print(newPixelArray[index+1])
    }
}


来源:https://stackoverflow.com/questions/32658576/cant-get-pixel-data-via-cgdataprovidercopydata-using-avcapturevideodataoutput-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!