1

My Problem is that I can get the pixel value from CVPixelBuffer, but all the value is 255,255,255. I use code as below:

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

Jerry
  • 31
  • 2
  • You have some unused variables in your function `width`, and `height`. I'm not sure if that makes a difference, but they're never called again in the function and their scope is limited to the function – Rhetorical Sep 19 '18 at 23:07
  • yeah its just get the size for photo buffer, in this case, it's 1280*720 photo. – Jerry Sep 19 '18 at 23:09
  • @Codo can you help me with this problem? Thanks a lot – Jerry Sep 20 '18 at 15:18

0 Answers0