Vuforia-抓取当前视频帧

时间:2018-09-06 09:52:27

标签: ios objective-c vuforia

我正在尝试从Vuforia抓取视频帧。我已经在互联网上找到了一些代码,但效果不佳。我得到的图像是grayscaledistortedduplicated。图像的分辨率也很低-120x67。我可以使用灰度,但是需要更高分辨率的普通图像而不会失真。我注意到,我无法设置

Vuforia::setFrameFormat(Vuforia::RGB888, true)

或其他任何值-返回false。也许我在错误的地方使用了它,我不确定何时应该执行此操作。我认为Vuforia :: Image返回的格式为GRAYSCALE,这可能就是为什么转换为UIImage也不起作用的原因。 这是我用来创建UIImage的ObjC包装器:

#import "VuforiaObjects.h"
#import <Vuforia/Renderer.h>
#import <Vuforia/Trackable.h>
#import <Vuforia/TrackableResult.h>
#import <Vuforia/Image.h>

@implementation VuforiaImage {
    const Vuforia::Image *_image;
}

- (instancetype)initWithImage:(const Vuforia::Image*)image {
    if(self = [super init]) {
        _image = image;
    }
    return self;
}

- (UIImage *)toUIImage {
    int width = self->_image->getWidth();
    int height = self->_image->getHeight();
    NSLog(@"%d x %d", width, height);
    int bitsPerComponent = 8;
    int bitsPerPixel = Vuforia::getBitsPerPixel(Vuforia::RGB888);
    int bytesPerRow = self->_image->getBufferWidth() * bitsPerPixel / bitsPerComponent;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaNone;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, self->_image->getPixels(), Vuforia::getBufferSize(width, height, Vuforia::RGB888), NULL);

    CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
    UIImage *result = [UIImage imageWithCGImage:imageRef];

    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpaceRef);
    CGImageRelease(imageRef);

    return result;
}

@end

@implementation VuforiaFrame {
    Vuforia::Frame _frame;
}

- (instancetype)initWithFrame:(Vuforia::Frame)frame {
    if(self = [super init]) {
        _frame = frame;
    }
    return self;
}

- (VuforiaImage *)image:(int)index {
    return [[VuforiaImage alloc] initWithImage:_frame.getImage(index)];
}

@end

这是它的外观(小的UIImageView显示生成的图像)​​: enter image description here

0 个答案:

没有答案