1

我的AVCaptureVideoDataOutputSampleBufferDelegate方法中有图像流:

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


然后我想sampleBuffer为 C++openalrp库提供它来识别哪些需要图像字节或原始像素数据。我应该使用什么功能以及如何转换sampleBuffer为合适的输入类型,std::vector<char>或者unsigned char* pixelData

alpr.h文件:

  // Recognize from an image on disk
  AlprResults recognize(std::string filepath);

  // Recognize from byte data representing an encoded image (e.g., BMP, PNG, JPG, GIF etc).
  AlprResults recognize(std::vector<char> imageBytes);

  // Recognize from byte data representing an encoded image (e.g., BMP, PNG, JPG, GIF etc).
  AlprResults recognize(std::vector<char> imageBytes, std::vector<AlprRegionOfInterest> regionsOfInterest);

  // Recognize from raw pixel data.  
  AlprResults recognize(unsigned char* pixelData, int bytesPerPixel, int imgWidth, int imgHeight, std::vector<AlprRegionOfInterest> regionsOfInterest);
4

1 回答 1

0

最后,我找到了答案:

CVPixelBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer, 0);
unsigned char *tempAddress = (unsigned char *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(tempAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef newImage = CGBitmapContextCreateImage(newContext);
CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);
UIImage *image = [UIImage imageWithCGImage:newImage];

NSData *data = UIImagePNGRepresentation(image);

if (data) {
    unsigned long int len = data.length;
    std::vector<char> v((char *)data.bytes, (char *)data.bytes + len);
    alpr::AlprResults results = delegate->recognize(v);
}

CGImageRelease(newImage);
于 2017-02-16T08:35:10.997 回答