为什么当我从设置为 OF_PIXELS_RGB
的 ofxAndroidVideoGrabber
绘制 ofImage
时,它看起来像这样吗?
这是我的代码:
void ofApp::setup(){
ofBackground(0,0,0);
grabber.setPixelFormat(OF_PIXELS_RGB);
// Start the grabber
grabber.setup(640,480);
// Get the native android video grabber
ofxAndroidVideoGrabber* androidGrabber = (ofxAndroidVideoGrabber*)grabber.getGrabber().get();
// Ensure facing the correct direction
androidGrabber->setDeviceID(androidGrabber->getBackCamera());
}
void ofApp::update(){
grabber.update();
if(grabber.isFrameNew()){
// Add frame
ofPixels nextFrame = grabber.getPixels();
frameQueue.push(nextFrame);
}
}
void ofApp::draw(){
// Calculate aspect ratio of grabber image
float grabberAspectRatio = grabber.getWidth() / grabber.getHeight();
// Draw camera image centered in the window
ofPushMatrix();
ofSetHexColor(0xFFFFFF);
ofSetRectMode(OF_RECTMODE_CENTER);
ofTranslate(ofGetWidth() / 2, ofGetHeight() / 2);
// Initial check that the next frame should be rendered
if ( ! frameQueue.empty() ) {
ofPixels nextFrame = frameQueue.front();
grabberImage.setFromPixels(nextFrame);
grabberImage.mirror(false, true);
grabberImage.update();
// Advance
frameQueue.pop();
}
if ( grabberImage.isAllocated() ) {
if(ofGetWidth() > ofGetHeight()) {
grabberImage.draw(0, 0, ofGetHeight() * grabberAspectRatio, ofGetHeight());
} else {
grabberImage.draw(0, 0, ofGetWidth(), ofGetWidth() * 1.0/grabberAspectRatio);
}
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
}
据我所知,视频以 NV21 形式出现,并由 openframeworks 转换为 RGB。我最好的猜测是:
- 我做错了什么,跳过了那个转换
- oF 中存在错误。
我已经尝试了提供的相机示例,我得到了同样的效果。
最佳答案
结果是我的模拟器使用了与大多数 Android 设备不同的视频格式。
潜在的问题是 openframeworks 假设每个像素的 U
和 V
channel 彼此相邻或半平面。而是基于查看 android source我发现 YUV 图像可以是 YUV420Planar
或 YUV420SemiPlanar
。如果图像是 YUV420Planar
,则 V
channel 跟随整个 U
channel 。 IE,第一个像素的V
channel 的索引是startIndexU + width * height
。我测试过的大多数 Android 设备都是半平面的,所以如果您看到像我这样的视频,可能只是您的模拟器提供的是平面馈送而不是半平面馈送。
关于android - Android 上的 VideoGrabber OF_PIXELS_RGB 渲染不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35448061/