Utilizzare Camera, Touch Screen e Storage su SD con .NET Gadgeteer (Italiano)

by Mike Dodaro (translated from English by Marco Minerva)

Questa applicazione è implementata utilizzando i moduli e la mainboard che fanno parte del GHI Electronics Spider Kit per Micorosoft .NET Gadgeteer.  Utilizza la funzionalità touch screen del modulo GHI Electronics .NET Gadgeteer Display T35 per attivare la camera e mostrare le immagini catturate. L’alimentazione è fornita dal modulo USB Client DP Module (UsbClientDP) connesso ad una porta USB del computer di sviluppo; esso permette anche di caricare l’applicazione sulla mainboard.

Dopo l’avvio della scheda, un tocco sul display attiva la camera, che inizia lo straming delle immagini: a questo punto, il display mostra ogni immagini non appena viene cattura. Un secondo tocco arresta l’attività della camera e permette di salvare l’ultimo frame sulla scheda SD inserita nel modulo SD Card di .NET Gadgeteer (che utilizza il socket di tipo F).

Un modulo LED indica visivamente lo stato del sistema: rosso quando è in corso lo streaming delle immagini catturate; verde quando l’applicazione è ferma. Dopo aver catturato e salvato diverse immagini sulla scheda SD, premendo il modulo bottone si avvia la visualizzazione di tutte le bitmap che sono state memorizzate.

La seguente immagine mostra la configurazione del sistema all’interno del Microsoft Visual Studio .NET Gadgeteer Designer.

Touch Screen Camera SDCard con .NET Gadgeteer Designer

Touch Screen Camera SDCard in .NET Gadgeteer Designer

Il seguente codice fornisce un esempio di utilizzo dei moduli Camera, Display Touch Screen e SD Card. La procedura di salvataggio delle immagini si basa su una funzione di utilità del GHI Spider Kit per .NET Gadgeteer, contenuta nell’assembly GHIElectronics.NETMF.System (che si trova in C:\Program Files (x86)\GHI Electronics\GHI NETMF v4.1 SDK\Assemblies\be).

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;

using System.Collections;
using System.IO;

using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using GHIElectronics.NETMF.System;

namespace SDCardCameraTouchScreen
{
    public partial class Program
    {
        Window mainWindow;
        StackPanel stackPanel;
        Bitmap currentBitmapData;

        Image saveImage = new Image(Resources.GetBitmap(Resources.BitmapResources.SaveImg));
        Image skipImage = new Image(Resources.GetBitmap(Resources.BitmapResources.SkipImg));
        ArrayList buttons = new ArrayList();

        uint imageNumber = 0;

        void ProgramStarted()
        {
            // Do one-time tasks here
            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
            camera.BitmapStreamed += new Camera.BitmapStreamedEventHandler(camera_BitmapStreamed);
            SetupDisplay();
            led.TurnGreen();

            Debug.Print("Program Started");

            display.SimpleGraphics.DisplayTextInRectangle(
                "Touch screen to start streaming bitmaps. Touch again to stop and save picture.",
                50, 50, 250, 200, GT.Color.Green, Resources.GetFont(Resources.FontResources.NinaB),
                GTM.Module.DisplayModule.SimpleGraphicsInterface.TextAlign.Left,
                GTM.Module.DisplayModule.SimpleGraphicsInterface.WordWrap.Wrap,
                GTM.Module.DisplayModule.SimpleGraphicsInterface.Trimming.WordEllipsis,
                GTM.Module.DisplayModule.SimpleGraphicsInterface.ScaleText.ScaleToFit);

            System.Threading.Thread.Sleep(4000);
        }

        void SetupDisplay()
        {
            mainWindow = display.WPFWindow;
            stackPanel = new StackPanel(Orientation.Horizontal);
            stackPanel.HorizontalAlignment = HorizontalAlignment.Center;
            stackPanel.SetMargin(4);
            stackPanel.Children.Add(saveImage);
            stackPanel.Children.Add(skipImage);

            buttons.Add(saveImage);
            buttons.Add(skipImage);
            foreach (Image button in buttons)
                button.Visibility = Visibility.Hidden;

            mainWindow.Child = stackPanel;

            mainWindow.TouchDown += new Microsoft.SPOT.Input.TouchEventHandler(mainWindow_TouchDown);

        }

        void ShowButtons()
        {
             foreach (Image button in buttons)
                button.Visibility = Visibility.Visible;

            saveImage.TouchDown += new Microsoft.SPOT.Input.TouchEventHandler(saveImage_TouchDown);
            skipImage.TouchDown += new Microsoft.SPOT.Input.TouchEventHandler(skipImage_TouchDown);

            mainWindow.Invalidate();
        }

        void HideButtons()
        {
            foreach (Image button in buttons)
            {
                button.Visibility = Visibility.Hidden;
            }

            skipImage.TouchDown -= new Microsoft.SPOT.Input.TouchEventHandler(skipImage_TouchDown);
            saveImage.TouchDown -= new Microsoft.SPOT.Input.TouchEventHandler(saveImage_TouchDown);

            mainWindow.Invalidate();
        }

        void skipImage_TouchDown(object sender, Microsoft.SPOT.Input.TouchEventArgs e)
        {
            HideButtons();
        }

        void saveImage_TouchDown(object sender, Microsoft.SPOT.Input.TouchEventArgs e)
        {
            if (VerifySDCard())
            {
                GT.StorageDevice storage = sdCard.GetStorageDevice();
                imageNumber = (uint)storage.ListFiles("\\SD\\").Length;

                byte[] bytes = new byte[currentBitmapData.Width *
                                                    currentBitmapData.Height * 3 + 54];
                Util.BitmapToBMPFile(currentBitmapData.GetBitmap(), currentBitmapData.Width,
                                                                  currentBitmapData.Height, bytes);
                GT.Picture picture = new GT.Picture(bytes, GT.Picture.PictureEncoding.BMP);

                string pathFileName = "\\SD\\Image-" + (imageNumber++).ToString() + ".bmp";

                try
                {
                    storage.WriteFile(pathFileName, picture.PictureData);
                }
                catch (Exception ex)
                {
                    Debug.Print("Message: " + ex.Message + "  Inner Exception: " + ex.InnerException);
                }
            }

            HideButtons();
        }

        void mainWindow_TouchDown(object sender, Microsoft.SPOT.Input.TouchEventArgs e)
        {
            if (saveImage.Visibility == Microsoft.SPOT.Presentation.Visibility.Visible)
                return;

            if (saveImage.Visibility == Microsoft.SPOT.Presentation.Visibility.Hidden &&
                                                           led.GetCurrentColor() == GT.Color.Red)
            {
                ShowButtons();
                camera.StopStreamingBitmaps();
                led.TurnGreen();
            }
            if (saveImage.Visibility == Microsoft.SPOT.Presentation.Visibility.Hidden &&
                                                           led.GetCurrentColor() == GT.Color.Green)
            {
                HideButtons();
                camera.StartStreamingBitmaps(new Bitmap(camera.CurrentPictureResolution.Width,
                                                          camera.CurrentPictureResolution.Height));
                led.TurnRed();
            }
        }

        void camera_BitmapStreamed(Camera sender, Bitmap bitmap)
        {
            display.SimpleGraphics.DisplayImage(bitmap, 0, 0);
            currentBitmapData = bitmap;
        }

        void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            button.TurnLEDOn();

            if (VerifySDCard())
            {
                GT.StorageDevice storage = sdCard.GetStorageDevice();

                foreach (string s in storage.ListRootDirectorySubdirectories())
                {
                    Debug.Print(s);
                    if (s == "SD")
                    {
                    foreach (string f in storage.ListFiles("\\SD\\"))
                        {
                            Bitmap bitmap = storage.LoadBitmap(f, Bitmap.BitmapImageType.Bmp);
                            display.SimpleGraphics.DisplayImage(bitmap, 0, 0);
                            System.Threading.Thread.Sleep(2000);
                        }
                    }
                }
            }

            led.TurnOff();
        }

        bool VerifySDCard()
        {
            if (!sdCard.IsCardInserted || !sdCard.IsCardMounted)
            {
                display.SimpleGraphics.DisplayText("Insert SD card!",
                        Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Cyan, 20, 50);

                System.Threading.Thread.Sleep(2000);
                display.SimpleGraphics.ClearNoRedraw();

                led.BlinkRepeatedly(GT.Color.Orange);
                return false;
            }

            return true;
        }
    }
}
Advertisement

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: