void startHardWork(int currentPhoto) { try { Dispatcher.BeginInvoke(delegate() { var source = new BitmapImage(); using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication()) { using (var fileStream = isoStore.OpenFile(currentPhoto + ".jpg", FileMode.Open, FileAccess.Read)) { source.SetSource(fileStream); } } this.bitmap = new WriteableBitmap(source); var sdkImg = ImageConverter.SystemToSdk(this.bitmap); var result = this.FaceSdkHelper.Detect(sdkImg); if (SdkHelper.foundFace == true) { foreach (var shape in SdkHelper.results) { var defaultFaceCalculations = Calculus.ReadDefaultFaceCalculations(); var array = FaceCalculationsHelpers.FromFaceCalculationsToVector(defaultFaceCalculations); var calculatedFaceCalculations = Calculus.GetFaceCalculationsFromShape(shape); var resultArray = FaceCalculationsHelpers.FromFaceCalculationsToVector(calculatedFaceCalculations); Conditions c = new Conditions(); var resultFromCalculus = c.GetLevelVerifierFunction(selectedPhotos[currentPhoto]).Invoke(array, resultArray); if (resultFromCalculus != 0) { using (var db = new GrimacizerContext(GrimacizerContext.ConnectionString)) { db.Settings.FirstOrDefault()._17_SurvivalScore += resultFromCalculus; db.SubmitChanges(); } MessageBox.Show("Picture number " + currentPhoto + " correct! You won " + resultFromCalculus + " points!"); } else { MessageBox.Show("Picture number " + currentPhoto + " did not match!"); } } } else { MessageBox.Show("Picture number " + currentPhoto + "! Camera did not detect your face!"); } }); } catch (Exception ex) { MessageBox.Show("Something bad appeared! Going back.."); NavigationService.Navigate(new Uri(Pages.MainPage, UriKind.RelativeOrAbsolute)); } finally { GC.Collect(); } }
private async Task startHardWork() { worker = new BackgroundWorker(); worker.DoWork += (sender, arg) => { FaceSdkHelper.Initialize(); }; await worker.BeginWorkerAsync(); var sdkImg = ImageConverter.SystemToSdk(imageBitmap); var result = FaceSdkHelper.Detect(sdkImg); if (SdkHelper.foundFace == true && SdkHelper.results.Count == 1) { var shape = SdkHelper.results.FirstOrDefault(); var defaultFaceCalculations = Calculus.ReadDefaultFaceCalculations(); var array = FaceCalculationsHelpers.FromFaceCalculationsToVector(defaultFaceCalculations); var calculatedFaceCalculations = Calculus.GetFaceCalculationsFromShape(shape); var resultArray = FaceCalculationsHelpers.FromFaceCalculationsToVector(calculatedFaceCalculations); var conditions = new Conditions(); var resultFromCalculus = conditions.GetLevelVerifierFunction(Level).Invoke(array, resultArray); if (resultFromCalculus != 0) { using (var db = new GrimacizerContext(GrimacizerContext.ConnectionString)) { var passedLevels = db.Settings.FirstOrDefault()._16_PassedLevels; db.Settings.FirstOrDefault()._16_PassedLevels = Math.Max(passedLevels, Level); var level = db.Levels.FirstOrDefault(t => t.Level == Level); level.Stars = Math.Max(resultFromCalculus, level.Stars); db.SubmitChanges(); } MessageBox.Show("Level passed. You won " + resultFromCalculus + " point" + (resultFromCalculus > 1 ? "s!" : "!")); } else { InGameLifeHelpers.LoseLife(); MessageBox.Show("Level failed"); } } else { MessageBox.Show("Camera did not detect your face! Try again!"); } NavigationService.Navigate(new Uri(Pages.GeneralGameplay_Adventure, UriKind.RelativeOrAbsolute)); FaceSdkHelper = null; GC.Collect(); }