在Swift 4上从CVPixelBuffer获取像素RGB值

时间:2018-09-19 22:43:31

标签: ios iphone swift ios11

我的问题是我可以从CVPixelBuffer获取像素值,但是所有值都是255,255,255。 我使用如下代码:

func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {

    guard let photoPixelBuffer = photo.pixelBuffer else {
        print("Error occurred while capturing photo: Missing pixel buffer (\(String(describing: error)))")

        return
    }
    func pixelFrom(x: Int, y: Int, movieFrame: CVPixelBuffer) -> (UInt8, UInt8, UInt8) {
        let baseAddress = CVPixelBufferGetBaseAddress(movieFrame)

        let width = CVPixelBufferGetWidth(movieFrame)
        let height = CVPixelBufferGetHeight(movieFrame)

        let bytesPerRow = CVPixelBufferGetBytesPerRow(movieFrame)
        let buffer = baseAddress!.assumingMemoryBound(to: UInt8.self)

        let index = x+y*bytesPerRow
        let b = buffer[index]
        let g = buffer[index+1]
        let r = buffer[index+2]

        return (r, g, b)
    }
    print(pixelFrom(x:1,y:1,movieFrame:photoPixelBuffer))
    all the r,g,b value is 255.

here is a link for get pixel from CVBuffer

0 个答案:

没有答案