iOS中的相机取景器框架

iOS中的相机取景器框架,ios,swift,camera,Ios,Swift,Camera,我正在使用SWIFT语言,试图从相机取景器缓冲区拍摄快照图像。到目前为止,除了图像的颜色外,一切都很好。它似乎不正确或正在被替换。下面是我设置视频设置和捕获图像帧的代码片段 func addVideoOutput() { videoDeviceOutput = AVCaptureVideoDataOutput() videoDeviceOutput.videoSettings = NSDictionary(objectsAndKeys: Int(kCVPixelFormatTy

我正在使用SWIFT语言,试图从相机取景器缓冲区拍摄快照图像。到目前为止,除了图像的颜色外,一切都很好。它似乎不正确或正在被替换。下面是我设置视频设置和捕获图像帧的代码片段

func addVideoOutput() {

    videoDeviceOutput = AVCaptureVideoDataOutput()
    videoDeviceOutput.videoSettings = NSDictionary(objectsAndKeys: Int(kCVPixelFormatType_32BGRA), kCVPixelBufferPixelFormatTypeKey) as[NSObject: AnyObject]

    // kCVPixelFormatType_32ARGB tested and found not supported
    videoDeviceOutput.alwaysDiscardsLateVideoFrames = true

    videoDeviceOutput.setSampleBufferDelegate(self, queue: sessionQueue)

    if captureSession!.canAddOutput(videoDeviceOutput) {
        captureSession!.addOutput(videoDeviceOutput)
    }
}

/* AVCaptureVideoDataOutput Delegate
------------------------------------------*/
func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
    sessionDelegate ? .cameraSessionDidOutputSampleBuffer ? (sampleBuffer)

    // Extract a UImage
    //var pixel_buffer : CVPixelBufferRef?
    let pixel_buffer = CMSampleBufferGetImageBuffer(sampleBuffer)

    CVPixelBufferLockBaseAddress(pixel_buffer, 0);

    // Get the number of bytes per row for the pixel buffer
    var baseAddress = CVPixelBufferGetBaseAddress(pixel_buffer);

    // Get the number of bytes per row for the pixel buffer
    var bytesPerRow = CVPixelBufferGetBytesPerRow(pixel_buffer);
    // Get the pixel buffer width and height
    let width : Int = CVPixelBufferGetWidth(pixel_buffer);
    let height : Int = CVPixelBufferGetHeight(pixel_buffer);

    /*Create a CGImageRef from the CVImageBufferRef*/
    let colorSpace: CGColorSpace = CGColorSpaceCreateDeviceRGB()
    let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)

    var newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, bitmapInfo)

    CVPixelBufferUnlockBaseAddress(pixel_buffer, 0);

    // get image frame and save to local storage
    var refImage: CGImageRef = CGBitmapContextCreateImage(newContext)
    var pixelData = CGDataProviderCopyData(CGImageGetDataProvider(refImage))

    var image: UIImage = UIImage(CGImage: refImage)!;
    self.SaveImageToDocumentStorage(image)
}
正如您在addVideoOutput函数中看到的注释行一样,我尝试了kCVPixelFormatType_32ARGB格式,但它说iOS不支持

我有点怀疑视频格式是32BGRA,但图像帧的颜色空间是用CGColorSpaceCreateDeviceRGB()设置的,但我找不到任何其他适合视频设置的RGB格式

任何解决方案或提示都将不胜感激


谢谢

我找到了原因和解决办法

以防有人遇到同样的问题。只需按如下方式更改bitmapInfo:

// let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)

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

我找到了原因和解决办法。以防有人遇到同样的问题。只需按如下方式更改bitmapInfo:var bitmapInfo=CGBitmapInfo(rawValue:CGImageAlphaInfo.premultipledFirst.rawValue)| CGBitmapInfo.ByteOrder32Little