noblerare
noblerare

Reputation: 11933

Swift 3 - How to use "AVFCapturePhotoOutput" - unrecognized raw format BGRA

I am using Swift 3 and developing for iPhone 7.

I have a custom camera view in which I try to take a photo. I am trying to display a UIImage of the photo that was taken but I get the following error:

[AVCapturePhotoOutput DNGPhotoDataRepresentationForRawSampleBuffer:previewPhotoSampleBuffer:] Unrecognized raw format BGRA'

This answer tells me that kCVPixelFormatType_24RGB is not supported by iPhone (for some reason) so I don't know what to do from here. Any help is greatly appreciated.

func shutterButtonPressed() {

    // apparently kCVPixelFormatType_24RGB is not supported for iPhone?
    let photoPixelFormatType = kCVPixelFormatType_32BGRA

    // using uncompressed format
    let settings = AVCapturePhotoSettings(
                    format: [kCVPixelBufferPixelFormatTypeKey as String : photoPixelFormatType])

    // thumbnail
    let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first!
    let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
                         kCVPixelBufferWidthKey as String: 160,
                         kCVPixelBufferHeightKey as String: 160,
                         ]
    settings.previewPhotoFormat = previewFormat

    // get the actual video feed and take a photo from that feed
    session_output.capturePhoto(with: settings, delegate: self)


}

func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {

    if let error = error {
            print(error.localizedDescription)
        }

        // ERROR ON THIS LINE
        if let sampleBuffer = photoSampleBuffer, let previewBuffer = previewPhotoSampleBuffer, let dataImage = AVCapturePhotoOutput.dngPhotoDataRepresentation(forRawSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {

            // this is the image that the user has taken!
            let takenImage : UIImage = UIImage(data: dataImage)!
        } else {
            print("Error setting up photo capture")
        }

Upvotes: 2

Views: 1135

Answers (1)

Gordon Childs
Gordon Childs

Reputation: 36169

I think you may be using the wrong AVCapturePhotoOutput delegate method. Can you try with

captureOutput:didFinishProcessingRawPhotoSampleBuffer:previewPhotoSampleBuffer:resolvedSettings:bracketSettings:error?

?

You should also be using AVCapturePhotoSettings(rawPixelFormatType:, processedFormat:) for your settings.

Upvotes: 1

Related Questions