Ejemplo n.º 1
0
        public WeightedObjectInfos Calculate(
            double weightGrams,
            RecognizedObjects recognizedObject)
        {
            var weightedObjectInfos = new WeightedObjectInfos(
                recognizedObject: recognizedObject,
                weightKilograms: (double)weightGrams / 1_000.0,
                calories: (_caloriesPerGram[recognizedObject] * weightGrams),
                pricePerKgEuro: _pricesPerKilo[recognizedObject],
                mainImagePath: recognizedObject.MainImagePath());

            var barcodeText      = Math.Round(weightedObjectInfos.PriceEuro * 10000, 0).ToString().PadLeft(12, '0');
            var barcodeGenerator = new Barcode();

            var barcodeImage = barcodeGenerator.Encode(
                TYPE.UPCA,
                barcodeText,
                Color.Black,
                Color.White,
                290,
                120);
            var barcodeImageDirectory = "picturesBarcode";

            Directory.CreateDirectory(barcodeImageDirectory);
            var filename = Path.GetFullPath($@"{barcodeImageDirectory}/{Guid.NewGuid().ToString("N")}.jpg");

            barcodeImage.Save(filename);

            weightedObjectInfos.BarCodePath = filename;

            return(weightedObjectInfos);
        }
        public void GenerateData(
            double weight,
            RecognizedObjects recognizedObject)
        {
            RecognizedObject = recognizedObject;

            if (recognizedObject == RecognizedObjects.Unrecognized)
            {
                WeightString = string.Empty;
                RecognizedObjectNameString = string.Empty;
                PriceString        = string.Empty;
                PricePerKiloString = string.Empty;
                CaloriesString     = string.Empty;
                FruitImagePath     = string.Empty;
                BarcodeImagePath   = string.Empty;
                return;
            }

            var scaleCalculator       = new ScaleCoreCalculator();
            var recognizedObjectInfos = scaleCalculator.Calculate(weight, recognizedObject);

            WeightString = $"{Math.Round(recognizedObjectInfos.WeightKilograms, 3):0.000}Kg";
            RecognizedObjectNameString = recognizedObject.DisplayText();
            PriceString        = $"{Math.Round(recognizedObjectInfos.PriceEuro, 2)}€";
            PricePerKiloString = $"{Math.Round(recognizedObjectInfos.PricePerKgEuro, 3)}€/Kg";
            CaloriesString     = $"{Math.Round(recognizedObjectInfos.Calories, 0)}cal / {Math.Round(recognizedObjectInfos.Joules, 0)}J";
            FruitImagePath     = recognizedObjectInfos.MainImagePath;
            BarcodeImagePath   = recognizedObjectInfos.BarCodePath;

            Task.Run(async() => await AddFakeNotifications(recognizedObject));
        }
 public WeightedObjectInfos(
     RecognizedObjects recognizedObject,
     double weightKilograms,
     double calories,
     double pricePerKgEuro,
     string mainImagePath)
 {
     RecognizedObject = recognizedObject;
     WeightKilograms  = weightKilograms;
     Calories         = calories;
     PricePerKgEuro   = pricePerKgEuro;
     MainImagePath    = mainImagePath;
 }
        private async Task AddFakeNotifications(RecognizedObjects recognizedObject)
        {
            await Task.Delay(1500);

            Application.Current.Dispatcher.Invoke(
                DispatcherPriority.Background,
                new Action(() =>
            {
                var mfi             = new System.Globalization.DateTimeFormatInfo();
                string strMonthName = mfi.GetMonthName(DateTime.Now.Month).ToString();
                switch (recognizedObject)
                {
                case RecognizedObjects.Apple:
                    Notifications.Add($"Apples are a seasonal fruit in {strMonthName}!");
                    Notifications.Add("If you buy 200gr more, you'll get a 20% discount!");
                    Thread.Sleep(500);
                    Notifications.Add("Pears are discounted too!");
                    break;

                case RecognizedObjects.Orange:
                    if (!new[] { 1, 6, 7, 8, 9, 10, 11, 12 }.Any(a => a == DateTime.Now.Month))
                    {
                        Notifications.Add($"Oranges are a seasonal fruit in {strMonthName}!");
                    }
                    else
                    {
                        Notifications.Add(new NiceNotification($"You are buying off-season fruits. Oranges are in season from February to May.", "Khaki"));
                    }
                    Notifications.Add("Good choice! In this period Vitamin C is important!");
                    break;

                case RecognizedObjects.Banana:
                    Notifications.Add($"Bananas are a seasonal fruit in {strMonthName}!");
                    Notifications.Add("Buongustaio!");
                    break;

                case RecognizedObjects.Strawberry:
                    if (!new[] { 2, 3, 4, 5 }.Any(a => a == DateTime.Now.Month))
                    {
                        Notifications.Add($"Stawberries are a seasonal fruit in {strMonthName}!");
                    }
                    else
                    {
                        Notifications.Add(new NiceNotification($"You are buying off-season fruits. Strawberries are in season from February to May.", "Khaki"));
                    }
                    break;
                }
            }));
        }
        public WeightPageViewModel(
            double weight,
            RecognizedObjects recognizedObject)
        {
            Notifications = new ObservableCollection <NiceNotification>();
            _timer        = new DispatcherTimer()
            {
                Interval = TimeSpan.FromMilliseconds(500)
            };
            _timer.Tick += _timer_Tick;
            _timer.Start();

            Date = DateTime.Now.ToString(_dateFormat);
            Hour = DateTime.Now.ToString(_hourFormat);

            GenerateData(weight, recognizedObject);
        }
Ejemplo n.º 6
0
        public static string DisplayText(this RecognizedObjects recognizedObject)
        {
            switch (recognizedObject)
            {
            case RecognizedObjects.Apple:
                return("Apples");

            case RecognizedObjects.Orange:
                return("Oranges");

            case RecognizedObjects.Banana:
                return("Bananas");

            case RecognizedObjects.Strawberry:
                return("Strawberries");
            }

            return("WAT?!");
        }
Ejemplo n.º 7
0
        public static string MainImagePath(this RecognizedObjects recognizedObject)
        {
            switch (recognizedObject)
            {
            case RecognizedObjects.Apple:
                return("images/yellow-apple.jpg");

            case RecognizedObjects.Orange:
                return("images/oranges.jpg");

            case RecognizedObjects.Banana:
                return("images/bananas.jpg");

            case RecognizedObjects.Strawberry:
                return("images/straw.jpg");
            }

            return(null);
        }
Ejemplo n.º 8
0
        private void FinalWeightArrived(double weight)
        {
            try
            {
                Application.Current.Dispatcher.Invoke(
                    DispatcherPriority.Background,
                    new Action(() =>
                {
                    // Stoppo l'ascolto della bilancia
                    _scaleManager.StopListening();
                    RecognizedObjects fruit = RecognizedObjects.Unrecognized;
                    for (int i = 0; i < 3 && fruit == RecognizedObjects.Unrecognized; ++i)
                    {
                        // Prendo l'immagine
                        var image           = CaptureCameraCallback();
                        var fruitsDirectory = "picturesFruits";
                        Directory.CreateDirectory(fruitsDirectory);
                        var imagePath = Path.GetFullPath($@"{fruitsDirectory}/{image}-{Guid.NewGuid().ToString("N")}.jpg");
                        image.Save(imagePath);

                        // Riconoscimento
                        fruit = DoRecognizeObject(imagePath);
                    }



                    StopLoader();

                    // Mostro il risultato
                    var weightPage = new WeightPage(new ViewModels.WeightPageViewModel(
                                                        weight,
                                                        fruit));
                    weightPage.ShowDialog();

                    // Mi rimetto in ascolto
                    InitializeScale();
                }));
            }
            catch
            {
            }
        }