Tôi sẽ phát điên với cái này - đã nhìn khắp mọi nơi và thử mọi thứ và mọi thứ tôi có thể nghĩ đến.AVCapture appendSampleBuffer
Làm cho ứng dụng iPhone sử dụng AVFoundation - cụ thể là AVCapture để quay video bằng camera iPhone.
Tôi cần có một hình ảnh tùy chỉnh được phủ lên nguồn cấp dữ liệu video có trong bản ghi.
Cho đến nay tôi có phiên AVCapture được thiết lập, có thể hiển thị nguồn cấp dữ liệu, truy cập vào khung, lưu nó dưới dạng UIImage và marge hình ảnh lớp phủ lên đó. Sau đó chuyển đổi UIImage mới này thành CVPixelBufferRef. để kiểm tra xem bufferRef có đang hoạt động hay không, tôi đã chuyển nó trở lại UIImage và nó vẫn hiển thị ảnh tĩnh.
Sự cố bắt đầu khi tôi cố gắng chuyển đổi CVPixelBufferRef thành CMSampleBufferRef để thêm vào AVCaptureSessions assetWriterInput. CMSampleBufferRef luôn trả về NULL khi tôi cố gắng tạo nó.
Đây là - (void) captureOutput chức năng
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
UIImage *botImage = [self imageFromSampleBuffer:sampleBuffer];
UIImage *wheel = [self imageFromView:wheelView];
UIImage *finalImage = [self overlaidImage:botImage :wheel];
//[previewImage setImage:finalImage]; <- works -- the image is being merged into one UIImage
CVPixelBufferRef pixelBuffer = NULL;
CGImageRef cgImage = CGImageCreateCopy(finalImage.CGImage);
CFDataRef image = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));
int status = CVPixelBufferCreateWithBytes(NULL,
self.view.bounds.size.width,
self.view.bounds.size.height,
kCVPixelFormatType_32BGRA,
(void*)CFDataGetBytePtr(image),
CGImageGetBytesPerRow(cgImage),
NULL,
0,
NULL,
&pixelBuffer);
if(status == 0){
OSStatus result = 0;
CMVideoFormatDescriptionRef videoInfo = NULL;
result = CMVideoFormatDescriptionCreateForImageBuffer(NULL, pixelBuffer, &videoInfo);
NSParameterAssert(result == 0 && videoInfo != NULL);
CMSampleBufferRef myBuffer = NULL;
result = CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault,
pixelBuffer, true, NULL, NULL, videoInfo, NULL, &myBuffer);
NSParameterAssert(result == 0 && myBuffer != NULL);//always null :S
NSLog(@"Trying to append");
if (!CMSampleBufferDataIsReady(myBuffer)){
NSLog(@"sampleBuffer data is not ready");
return;
}
if (![assetWriterInput isReadyForMoreMediaData]){
NSLog(@"Not ready for data :(");
return;
}
if (![assetWriterInput appendSampleBuffer:myBuffer]){
NSLog(@"Failed to append pixel buffer");
}
}
}
Một giải pháp tôi tiếp tục nghe về đang sử dụng một AVAssetWriterInputPixelBufferAdaptor mà loại bỏ sự cần thiết phải làm CMSampleBufferRef gói lộn xộn. Tuy nhiên, tôi đã lùng sục các diễn đàn và tài liệu về nhà phát triển và xếp chồng lên nhau và không thể tìm thấy mô tả hoặc ví dụ rõ ràng về cách thiết lập hoặc cách sử dụng nó. Nếu bất cứ ai có một ví dụ làm việc của nó, bạn có thể vui lòng chỉ cho tôi hoặc giúp tôi giải quyết vấn đề trên - đã làm việc về điều này không ngừng trong một tuần và sáng suốt.
Hãy cho tôi biết nếu bạn cần bất kỳ thông tin khác
Cảm ơn trước,
Michael
Xin lỗi vì thiếu định dạng mã - nó xuất hiện tốt trong bản xem trước: S –