6

我尝试编写 ios 相机,并从苹果中获取了部分代码:

- (void)captureOutput:(AVCaptureOutput *)captureOutput
         didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
         fromConnection:(AVCaptureConnection *)connection
{
    // Create a UIImage from the sample buffer data
    UIImage *image = [self imageFromSampleBuffer:sampleBuffer];

     < Add your code here that uses the image >

}

我需要从程序中的任何位置调用此函数。但是因为它需要创建一个对象类型为(CMSampleBufferRef). 怎么做?

我试着写一些类似的东西:

buf1 = [[CMSampleBufferRef alloc]init] 

但这是错误的方式。

4

3 回答 3

12

这是我目前用于CMSampleBuffer在 swift3 中模拟单元测试的片段:

fileprivate func getCMSampleBuffer() -> CMSampleBuffer {
    var pixelBuffer : CVPixelBuffer? = nil
    CVPixelBufferCreate(kCFAllocatorDefault, 100, 100, kCVPixelFormatType_32BGRA, nil, &pixelBuffer)

    var info = CMSampleTimingInfo()
    info.presentationTimeStamp = kCMTimeZero
    info.duration = kCMTimeInvalid
    info.decodeTimeStamp = kCMTimeInvalid


    var formatDesc: CMFormatDescription? = nil
    CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer!, &formatDesc)

    var sampleBuffer: CMSampleBuffer? = nil

    CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault,
                                             pixelBuffer!,
                                             formatDesc!,
                                             &info,
                                             &sampleBuffer);

    return sampleBuffer!
}
于 2017-08-21T08:03:23.247 回答
2

Swift 5 版本的@Rotem Tamir's Answer

fileprivate func getCMSampleBuffer() -> CMSampleBuffer {
    var pixelBuffer: CVPixelBuffer?
    CVPixelBufferCreate(kCFAllocatorDefault, 100, 100, kCVPixelFormatType_32BGRA, nil, &pixelBuffer)

    var info = CMSampleTimingInfo()
    info.presentationTimeStamp = CMTime.zero
    info.duration = CMTime.invalid
    info.decodeTimeStamp = CMTime.invalid

    var formatDesc: CMFormatDescription?
    CMVideoFormatDescriptionCreateForImageBuffer(allocator: kCFAllocatorDefault,
                                                 imageBuffer: pixelBuffer!,
                                                 formatDescriptionOut: &formatDesc)

    var sampleBuffer: CMSampleBuffer?

    CMSampleBufferCreateReadyWithImageBuffer(allocator: kCFAllocatorDefault,
                                             imageBuffer: pixelBuffer!,
                                             formatDescription: formatDesc!,
                                             sampleTiming: &info,
                                             sampleBufferOut: &sampleBuffer)

    return sampleBuffer!
}
于 2021-02-15T13:50:00.153 回答
0

尝试所有这些(一个可能有效):

UIImage *image = [self imageFromSampleBuffer:&sampleBuffer];
UIImage *image = [self imageFromSampleBuffer:(id)sampleBuffer];
UIImage *image = [self imageFromSampleBuffer:(__bridge CMSampleBufferRef)sampleBuffer];
UIImage *image = [self imageFromSampleBuffer:(__bridge id)sampleBuffer];

如果这些都不起作用,请创建对 CMSampleBuffer 的 CVImageBuffer 的引用,而不添加上述任何内容来替换 UIImage 方法中的 sampleBuffer:

CVImageBufferRef cvImageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

如果这不起作用,您可以创建一个单独的方法,将 CMSampleBuffer 转换为 UIImage,如下所示:

// Create a UIImage from sample buffer data
- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{
    // Get a CMSampleBuffer's Core Video image buffer for the media data
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    // Lock the base address of the pixel buffer
    CVPixelBufferLockBaseAddress(imageBuffer, 0);

    // Get the number of bytes per row for the pixel buffer
    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);

    // Get the number of bytes per row for the pixel buffer
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    // Get the pixel buffer width and height
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);

    // Create a device-dependent RGB color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // Create a bitmap graphics context with the sample buffer data
    CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,
      bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    // Create a Quartz image from the pixel data in the bitmap graphics context
    CGImageRef quartzImage = CGBitmapContextCreateImage(context);
    // Unlock the pixel buffer
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);

    // Free up the context and color space
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    // Create an image object from the Quartz image
    UIImage *image = [UIImage imageWithCGImage:quartzImage];

    // Release the Quartz image
    CGImageRelease(quartzImage);

    return (image);
}

这种方法有效;事实上,这是我使用的那个。

于 2016-08-13T09:40:33.403 回答