Last active
September 26, 2015 17:04
-
-
Save csotiriou/238fdf8f1d130c6013f9 to your computer and use it in GitHub Desktop.
Vuforia cloud reco sample OnQCarUpdate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Initialize the application trackers | |
- (BOOL) doInitTrackers { | |
// // Initialize the image or marker tracker | |
QCAR::TrackerManager& trackerManager = QCAR::TrackerManager::getInstance(); | |
// | |
// // Image Tracker... | |
QCAR::Tracker* trackerBase = trackerManager.initTracker(QCAR::ObjectTracker::getClassType()); | |
if (trackerBase == NULL) | |
{ | |
SFDebugLog(@"Failed to initialize ImageTracker."); | |
return NO; | |
} | |
QCAR::ObjectTracker* imageTracker = static_cast<QCAR::ObjectTracker*>(trackerManager.getTracker(QCAR::ObjectTracker::getClassType())); | |
if (imageTracker == NULL) | |
{ | |
SFDebugLog(@">doLoadTrackersData>Failed to load tracking data set because the ImageTracker has not been initialized."); | |
return NO; | |
} | |
// Initialize visual search: | |
QCAR::TargetFinder* targetFinder = imageTracker->getTargetFinder(); | |
if (targetFinder == NULL) | |
{ | |
SFDebugLog(@">doLoadTrackersData>Failed to get target finder."); | |
return NO; | |
} | |
NSDate *start = [NSDate date]; | |
// Start initialization: | |
if (targetFinder->startInit(kAccessKey, kSecretKey)){ | |
targetFinder->waitUntilInitFinished(); | |
NSDate *methodFinish = [NSDate date]; | |
NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:start]; | |
SFDebugLog(@"waitUntilInitFinished Execution Time: %f", executionTime); | |
} | |
// callback called when the initailization of the AR is done | |
- (void) onInitARDone:(NSError *)initError { | |
[self hideCameraLoader]; | |
SFDebugLog(@"error init ar: %@", initError); | |
if (initError == nil) { | |
NSError * error = nil; | |
[self.currentARSession startAR:QCAR::CameraDevice::CAMERA_BACK error:&error]; | |
// by default, we try to set the continuous auto focus mode | |
// and we update menu to reflect the state of continuous auto-focus | |
// bool isContinuousAutofocus = QCAR::CameraDevice::getInstance().setFocusMode(QCAR::CameraDevice::FOCUS_MODE_CONTINUOUSAUTO); | |
// SampleAppMenu * menu = [SampleAppMenu instance]; | |
// [menu setSelectionValueForCommand:C_AUTOFOCUS value:isContinuousAutofocus]; | |
} else { | |
SFDebugLog(@"Error initializing AR:%@", [initError description]); | |
} | |
} | |
int resultCode = targetFinder->getInitState(); | |
if ( resultCode != QCAR::TargetFinder::INIT_SUCCESS) | |
{ | |
SFDebugLog(@">doLoadTrackersData>Failed to initialize target finder."); | |
if (resultCode == QCAR::TargetFinder::INIT_ERROR_NO_NETWORK_CONNECTION) { | |
SFDebugLog(@"CloudReco error:QCAR::TargetFinder::INIT_ERROR_NO_NETWORK_CONNECTION"); | |
} else if (resultCode == QCAR::TargetFinder::INIT_ERROR_SERVICE_NOT_AVAILABLE) { | |
SFDebugLog(@"CloudReco error:QCAR::TargetFinder::INIT_ERROR_SERVICE_NOT_AVAILABLE"); | |
} else { | |
SFDebugLog(@"CloudReco error:%d", resultCode); | |
} | |
int initErrorCode; | |
if(resultCode == QCAR::TargetFinder::INIT_ERROR_NO_NETWORK_CONNECTION) | |
{ | |
initErrorCode = QCAR::TargetFinder::UPDATE_ERROR_NO_NETWORK_CONNECTION; | |
} | |
else | |
{ | |
initErrorCode = QCAR::TargetFinder::UPDATE_ERROR_SERVICE_NOT_AVAILABLE; | |
} | |
[self showUIAlertFromErrorCode: initErrorCode]; | |
return NO; | |
} else { | |
SFDebugLog(@">doLoadTrackersData>target finder initialized"); | |
} | |
return YES; | |
} | |
// update from the QCAR loop | |
- (void) onQCARUpdate: (QCAR::State *) state { | |
QCAR::TrackerManager& trackerManager = QCAR::TrackerManager::getInstance(); | |
// Get the image tracker: | |
QCAR::ObjectTracker* imageTracker = static_cast<QCAR::ObjectTracker*>(trackerManager.getTracker(QCAR::ObjectTracker::getClassType())); | |
// Get the target finder: | |
QCAR::TargetFinder* finder = imageTracker->getTargetFinder(); | |
// Check if there are new results available: | |
const int statusCode = finder->updateSearchResults(); | |
// SFDebugLog(@"status %i", statusCode); | |
if (statusCode < 0){ | |
// Show a message if we encountered an error: | |
SFDebugLog(@"update search result failed:%d", statusCode); | |
if (statusCode == QCAR::TargetFinder::UPDATE_ERROR_NO_NETWORK_CONNECTION) { | |
[self showUIAlertFromErrorCode:statusCode]; | |
} | |
}else if (statusCode == QCAR::TargetFinder::UPDATE_RESULTS_AVAILABLE){ | |
SFDebugLog(@"results found"); | |
// Iterate through the new results: | |
for (int i = 0; i < finder->getResultCount(); ++i){ | |
const QCAR::TargetSearchResult* result = finder->getResult(i); | |
// Check if this target is suitable for tracking: | |
if (result->getTrackingRating() > 0){ | |
NSString *currentID = [[NSString alloc] initWithBytes:result->getUniqueTargetId() length:strlen(result->getUniqueTargetId()) encoding:NSUTF8StringEncoding]; | |
if (!self.isPLayingVideoFullScreen && ![currentID isEqualToString:self.currentTargetID]) { | |
// Create a new Trackable from the result: | |
QCAR::ImageTarget* newTrackable = finder->enableTracking(*result); | |
if (newTrackable != 0){ | |
// Avoid entering on ContentMode when a bad target is found | |
// (Bad Targets are targets that are exists on the CloudReco database but not on our | |
// own book database) | |
std::string str = result->getMetaData(); | |
std::cout << str << '\n'; | |
SFDebugLog(@"Successfully created new trackable '%s' with rating '%d'.", newTrackable->getName(), result->getTrackingRating()); | |
NSDictionary *dictionary = [Util dictionaryFromMetadata:result->getMetaData()]; | |
self.currentTargetID = currentID; | |
self.lastRecognizedMarker = [Marker modelObjectWithDictionary:dictionary]; | |
[self actionForMarker:self.lastRecognizedMarker]; | |
[self doStopFinder]; | |
self.trackableDictionary[currentID] = self.lastRecognizedMarker; | |
}else{ | |
SFDebugLog(@"Failed to create new trackable."); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment