IOS – converts CMSampleBufferRef to UIImage

I use AVCaptureSession to shoot video.
But I want to convert the captured image to UI Image.

I found some code on the Internet :

- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{

NSLog(@"imageFromSampleBuffer: called");< br /> // 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];
< br /> // Release the Quartz image
CGImageRelea se(quartzImage);

return (image);
}

But I encountered some errors:

06001

Edit:
I also use UIImage to get rgb colors:

-(void) captureOutput:( AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection
{

UIImage* image = [self imageFromSampleBuffer:sampleBuffer];
unsigned char* pixels = [image rgbaPixels];
double totalLuminance = 0.0;
for(int p=0;p {
totalLuminance += pixels[p]*0.299 + pixels[p+1]*0.587 + pixels[p+2]*0.114;
}
totalLuminance /= (image.size.width *image.size.height);
totalLuminance /= 255.0;
NSLog(@"totalLuminance %f",totalLuminance);

}

Your best choice is to set the capture video data output’s videoSettings to one s pecifies the pixel format you want, you need to set to RGB some variants that CGBitmapContext can handle.

The document has a list of all of the pixel formats that Core Video can process.CGBitmapContext Only a small part of it is supported. The format expected by the code you find on the Internet is kCVPixelFormatType_32BGRA, but this may be written for Macs-on iOS devices, kCVPixelFormatType_32ARGB (big-endian) may be faster. Try them on the device , And compare the frame rate.

I use AVCaptureSession to shoot video.
But I want to convert the captured image to UI Image.

< p>I found some codes on the Internet:

- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{

NSLog (@"imageFromSampleBuffer: called");
// 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 byt es 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 conte xt 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);
)

But I encountered some errors:

06001

Edit:
I also use UIImage to get rgb Color:

-(void) captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection
{
< br /> UIImage* image = [self imageFromSampleBuffer:sampleBuffer];
unsigned char* pixels = [image rgbaPixels];
double totalLuminance = 0.0;
for(int p=0;p< image.size.width*image.size.height*4;p+=4)
{
totalLuminance += pixels[p]*0.299 + pixels[p+1]*0.587 + pixels[p+ 2]*0.114;
}
totalLuminance /= (i mage.size.width*image.size.height);
totalLuminance /= 255.0;
NSLog(@"totalLuminance %f",totalLuminance);

}

Your best choice is to set the capture video data output’s videoSettings to a dictionary that specifies the pixel format you want, you need to set it to Some of the variants of RGB that CGBitmapContext can handle.

The document has a list of all of the pixel formats that Core Video can process. CGBitmapContext only supports a small part of them. You can find it on the Internet The expected format of the code is kCVPixelFormatType_32BGRA, but this may be written for Macs-on iOS devices, kCVPixelFormatType_32ARGB(big-endian) may be faster. Try them on the device and compare the frame rate.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3899 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.