#include "ofApp.h"
#include "ofxAndroidVideoGrabber.h"
void ofApp::setup(){
ofBackground(0,0,0);
vector<ofVideoDevice> devices = grabber.listDevices();
for(int i=0;i<devices.size(); i++){
ofLog()<<"Device "<<i<<": "<<devices[i].deviceName;
}
grabber.setPixelFormat(OF_PIXELS_GRAY);
grabber.setup(1280, 720);
if(grabber.isInitialized()) {
cameraOrientation = ((ofxAndroidVideoGrabber *) grabber.getGrabber().get())->getCameraOrientation();
cameraFacingFront = ((ofxAndroidVideoGrabber *) grabber.getGrabber().get())->getFacingOfCamera();
}
}
void ofApp::update(){
grabber.update();
if(grabber.isFrameNew()){
cameraFps.newFrame();
grabberImage.loadData(grabber.getPixels());
}
cameraFps.update();
}
void ofApp::draw(){
float grabberAspectRatio = grabber.getWidth() / grabber.getHeight();
ofPushMatrix();
ofSetColor(255);
ofSetRectMode(OF_RECTMODE_CENTER);
ofTranslate(ofGetWidth() / 2, ofGetHeight() / 2);
if(cameraFacingFront) {
ofRotateDeg(appOrientation);
} else {
ofRotateDeg(-appOrientation);
}
ofRotateDeg(cameraOrientation);
int width = ofGetWidth();
int height = ofGetHeight();
if(appOrientation == 90 || appOrientation == 270){
std::swap(width, height);
}
if(width < height) {
grabber.draw(0,0, width * grabberAspectRatio,
width);
} else {
grabber.draw(0,0, height,
height * 1.0/grabberAspectRatio);
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDrawRectangle(0, 0, 300, 120);
ofSetColor(0);
ofDrawBitmapString("fps: " + ofToString(ofGetFrameRate()),20,20);
ofDrawBitmapString("camera fps: " + ofToString(cameraFps.getFps()),20,40);
if(cameraFacingFront) {
ofDrawBitmapString("facing: front (click to swap)", 20, 60);
} else {
ofDrawBitmapString("facing: back (click to swap)", 20, 60);
}
ofDrawBitmapString("camera orientation: " + ofToString(cameraOrientation) ,20,80);
ofDrawBitmapString("app orientation: " + ofToString(appOrientation) ,20,100);
ofSetColor(255);
ofDrawRectangle(0, 130, 300, 20);
ofSetColor(0);
ofDrawBitmapString("raw pixel data" ,20,143);
ofSetColor(255);
grabberImage.draw(0,150, 300, 300*1.0/grabberAspectRatio);
}
void ofApp::keyPressed (int key){
}
void ofApp::keyReleased(int key){
}
void ofApp::windowResized(int w, int h){
}
void ofApp::touchDown(int x, int y, int id){
ofxAndroidVideoGrabber* androidGrabber = (ofxAndroidVideoGrabber*)grabber.getGrabber().get();
if(cameraFacingFront) {
androidGrabber->setDeviceID(androidGrabber->getBackCamera());
} else {
androidGrabber->setDeviceID(androidGrabber->getFrontCamera());
}
cameraOrientation = androidGrabber->getCameraOrientation();
cameraFacingFront = androidGrabber->getFacingOfCamera();
}
void ofApp::deviceOrientationChanged(ofOrientation newOrientation){
appOrientation = ofOrientationToDegrees(newOrientation);
}
void ofApp::touchMoved(int x, int y, int id){
}
void ofApp::touchUp(int x, int y, int id){
}
void ofApp::touchDoubleTap(int x, int y, int id){
}
void ofApp::touchCancelled(int x, int y, int id){
}
void ofApp::swipe(ofxAndroidSwipeDir swipeDir, int id){
}
void ofApp::pause(){
}
void ofApp::stop(){
}
void ofApp::resume(){
}
void ofApp::reloadTextures(){
}
bool ofApp::backPressed(){
return false;
}
void ofApp::okPressed(){
}
void ofApp::cancelPressed(){
}
Comments