peony3000

Gadgeteer Pixel 02-02

Mar 4th, 2012
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using Microsoft.SPOT;
  4. using Microsoft.SPOT.Presentation;
  5. using Microsoft.SPOT.Presentation.Controls;
  6. using Microsoft.SPOT.Presentation.Media;
  7. using Microsoft.SPOT.Touch;
  8.  
  9. using Gadgeteer.Networking;
  10. using GT = Gadgeteer;
  11. using GTM = Gadgeteer.Modules;
  12. using Gadgeteer.Modules.GHIElectronics;
  13.  
  14. namespace Gadgeteer_Pixels_02
  15. {
  16.     public partial class Program
  17.     {
  18.         Bitmap currentImage;
  19.  
  20.         // This method is run when the mainboard is powered up or reset.  
  21.         void ProgramStarted()
  22.         {
  23.             /*******************************************************************************************
  24.             Modules added in the Program.gadgeteer designer view are used by typing
  25.             their name followed by a period, e.g.  button.  or  camera.
  26.            
  27.             Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
  28.                 button.ButtonPressed +=<tab><tab>
  29.            
  30.             If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
  31.                 GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
  32.                 timer.Tick +=<tab><tab>
  33.                 timer.Start();
  34.             *******************************************************************************************/
  35.  
  36.  
  37.             // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
  38.             Debug.Print("Program Started");
  39.  
  40.             camera.DebugPrintEnabled = true;
  41.             // So we can query the camera
  42.             // Stream the image to the mainboard as a bitmap as this is easy to process
  43.             camera.BitmapStreamed += new Camera.BitmapStreamedEventHandler(camera_BitmapStreamed);
  44.             // When you press the button take a picture and process the pixels
  45.             button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
  46.         }
  47.  
  48.          void camera_BitmapStreamed(Camera sender, Bitmap bitmap)
  49.         {
  50.             display.SimpleGraphics.DisplayImage(bitmap, 5, 5);
  51.             camera.StopStreamingBitmaps();
  52.             currentImage = bitmap;
  53.            
  54.             int redTotal = 0;
  55.             int redAverage = 0;
  56.             int greenTotal = 0;
  57.             int greenAverage = 0;
  58.             int blueTotal = 0;
  59.             int blueAverage = 0;
  60.             int pixelTotal = 0;
  61.             Color currentPixel;
  62.  
  63.             Debug.Print("Number of pixels wide = " + camera.CurrentPictureResolution.Width);
  64.             Debug.Print("Number of pixels high = " + camera.CurrentPictureResolution.Height);
  65.  
  66.             for (int xIncrement = 0; xIncrement < camera.CurrentPictureResolution.Width; xIncrement += 10)
  67.             {
  68.                 for (int yIncrement = 0; yIncrement < camera.CurrentPictureResolution.Height; yIncrement += 10)
  69.                 {
  70.  
  71.                     currentPixel = currentImage.GetPixel(xIncrement, yIncrement);
  72.  
  73.                     redTotal += ColorUtility.GetRValue(currentPixel);
  74.                     greenTotal += ColorUtility.GetGValue(currentPixel);
  75.                     blueTotal += ColorUtility.GetBValue(currentPixel);
  76.                     pixelTotal++;
  77.  
  78.                 }
  79.             }
  80.  
  81.             redAverage = redTotal / pixelTotal;
  82.             greenAverage = greenTotal / pixelTotal;
  83.             blueAverage = blueTotal / pixelTotal;
  84.  
  85.             led.SetRedIntensity(redAverage);
  86.             led.SetBlueIntensity(blueAverage);
  87.             led.SetGreenIntensity(greenAverage);
  88.            
  89.             led1.SetRedIntensity(redAverage);
  90.             led1.SetBlueIntensity(blueAverage);
  91.             led1.SetGreenIntensity(greenAverage);
  92.  
  93.             Debug.Print("redAverage = " + redAverage);
  94.             Debug.Print("greenAverage = " + greenAverage);
  95.             Debug.Print("blueAverage = " + blueAverage);
  96.  
  97.            
  98.         }
  99.  
  100.         void button_ButtonPressed(Button sender, Button.ButtonState state)
  101.         {
  102.             Debug.Print("Streaming....");
  103.             // How can I process pixels as they stream?
  104.             camera.StartStreamingBitmaps(new Bitmap(camera.CurrentPictureResolution.Width,camera.CurrentPictureResolution.Height));
  105.            
  106.         }
  107.        
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment