Mengaktifkan FlashLight Pada Windows Phone 7

Posted: February 16, 2013 by Sartikha in Uncategorized

Untuk membuat heart rate pada WIndows Phone 7 selain menggunakan kamera untuk menangkap RGB juga dibutuhkan Flash Light agar membuat RGB lebih jelas ditangkap. Berikut cara membuat flashlight pada Windows Phone Image

  • Buat file (.cs) VideoCamera.cs dan VideoCameraVisualizer.cs

VideoCamera.cs

using System;
using System.Reflection;

namespace Flashlight
{
/// <summary>
/// Wrapper around the Microsoft.Phone.VideoCamera object in Microsoft.Phone.Media.Extended.
/// </summary>
public class VideoCamera
{
private object _videoCamera;
private PropertyInfo _videoCameraLampEnabledPropertyInfo;
private MethodInfo _videoCameraStartRecordingMethod;
private EventHandler _videoCameraInitialized;

public object InnerCameraObject
{
get { return _videoCamera; }
}

public bool LampEnabled
{
get { return (bool)_videoCameraLampEnabledPropertyInfo.GetGetMethod().Invoke(_videoCamera, new object[0]); }
set { _videoCameraLampEnabledPropertyInfo.GetSetMethod().Invoke(_videoCamera, new object[] { value }); }
}

public VideoCamera()
{
// Load the media extended assembly which contains the extended video camera object.
Assembly mediaExtendedAssembly = Assembly.Load(“Microsoft.Phone.Media.Extended, Version=7.0.0.0, Culture=neutral, PublicKeyToken=24eec0d8c86cda1e”);

// Get the camera source type (primary camera).
Type cameraSourceType = mediaExtendedAssembly.GetType(“Microsoft.Phone.CameraSource”);
FieldInfo field = cameraSourceType.GetField(“PrimaryCamera”);
object cameraSource = Enum.ToObject(cameraSourceType, (int)field.GetValue(cameraSourceType));

// Create the video camera object.
Type videoCameraType = mediaExtendedAssembly.GetType(“Microsoft.Phone.VideoCamera”);
ConstructorInfo videoCameraConstructor = videoCameraType.GetConstructor(new Type[] { cameraSourceType });
_videoCamera = videoCameraConstructor.Invoke(new object[] { cameraSource });

// Set the properties and methods.
_videoCameraLampEnabledPropertyInfo = videoCameraType.GetProperty(“LampEnabled”);
_videoCameraStartRecordingMethod = videoCameraType.GetMethod(“StartRecording”);

// Let the initialize event bubble through.
_videoCameraInitialized = new EventHandler(VideoCamera_Initialized);
MethodInfo addInitializedEventMethodInfo = videoCameraType.GetMethod(“add_Initialized”);
addInitializedEventMethodInfo.Invoke(_videoCamera, new object[] { _videoCameraInitialized });
}

/// <summary>
/// Occurs when the camera object has been initialized.
/// </summary>
public event EventHandler Initialized;

/// <summary>
/// Videoes the camera_ initialized.
/// </summary>
/// <param name=”sender”>The sender.</param>
/// <param name=”eventArgs”>The event args.</param>
private void VideoCamera_Initialized(object sender, object eventArgs)
{
if (Initialized != null)
{
Initialized.Invoke(this, new EventArgs());
}
}

/// <summary>
/// Start recording.
/// </summary>
public void StartRecording()
{
// Invoke the start recording method on the video camera object.
_videoCameraStartRecordingMethod.Invoke(_videoCamera, new object[0]);
}
}
}

Selanjutnya VideoCameraVisualizer.cs

using System;
using System.Reflection;
using System.Windows.Controls;
//using System.Reflection.TargetInvocationException;

namespace Flashlight
{
/// <summary>
/// Wrapper around the Microsoft.Phone.CameraVisualizer object in Microsoft.Phone.Media.Extended.
/// </summary>
public class VideoCameraVisualizer : UserControl
{
private object _cameraVisualizer;
private MethodInfo _cameraVisualizerSetSourceMethod;

public VideoCameraVisualizer()
{
// Load the media extended assembly which contains the extended video camera object.
Assembly mediaExtendedAssembly = Assembly.Load(“Microsoft.Phone.Media.Extended, Version=7.0.0.0, Culture=neutral, PublicKeyToken=24eec0d8c86cda1e”);

// Get the camera source type (primary camera).
Type cameraVisualizerType = mediaExtendedAssembly.GetType(“Microsoft.Phone.CameraVisualizer”);
ConstructorInfo cameraVisualizerConstructor = cameraVisualizerType.GetConstructor(Type.EmptyTypes);
_cameraVisualizer = cameraVisualizerConstructor.Invoke(null);

// Set the properties and methods.
_cameraVisualizerSetSourceMethod = cameraVisualizerType.GetMethod(“SetSource”);
}

public void SetSource(VideoCamera camera)
{
// Invoke the set source method on the camera visualizer object.
_cameraVisualizerSetSourceMethod.Invoke(_cameraVisualizer, new object[] { camera.InnerCameraObject });
}
}
}

  • Pada MainPage.xaml.cs masukan sintaks berikut

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Devices;
using System.Threading;
using System.Windows.Media.Imaging;

namespace HeartRate
{
public partial class MainPage : PhoneApplicationPage
{
private VideoCamera _videoCamera;
private VideoCameraVisualizer _videoCameraVisualizer;

// Constructor
public MainPage()
{
InitializeComponent(); this.Bitmap = new WriteableBitmap(256, 100);
imgHistogram.Source = this.Bitmap;
this.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(MainPage_OrientationChanged);

if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
{
this.Camera = new PhotoCamera(CameraType.Primary);
this.Camera.Initialized += new EventHandler<CameraOperationCompletedEventArgs>(Camera_Initialized);
this.cameraViewBrush.SetSource(this.Camera);
this.CorrectViewFinderOrientation(this.Orientation);

//// Use standard camera on back of device.
//_videoCamera = new VideoCamera();

//// Event is fired when the video camera object has been initialized.
//_videoCamera.Initialized += VideoCamera_Initialized;

//// Add the photo camera to the video source
//_videoCameraVisualizer = new VideoCameraVisualizer();
//_videoCameraVisualizer.SetSource(_videoCamera);

}
else
throw new NotSupportedException(“Camera is not supported”);

}

private void VideoCamera_Initialized(object sender, EventArgs e)
{
_videoCamera.LampEnabled = true;
_videoCamera.StartRecording();

}
private void Camera_Initialized(object sender, CameraOperationCompletedEventArgs e)
{
Timer timer = new Timer(UpdateHistogram, null, 0, 1000);
}

private void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
this.CorrectViewFinderOrientation(e.Orientation);
}

private void CorrectViewFinderOrientation(PageOrientation orientation)
{
if (orientation == PageOrientation.LandscapeLeft)
this.cameraViewBrushTransform.Rotation = this.Camera.Orientation – 90.0;
else if (orientation == PageOrientation.LandscapeRight)
this.cameraViewBrushTransform.Rotation = this.Camera.Orientation + 90.0;
}

private void UpdateHistogram(object state)
{
int max = (int)this.Camera.PreviewResolution.Width * (int)this.Camera.PreviewResolution.Height;
int[] buffer = new int[max];
this.Camera.GetPreviewBufferArgb32(buffer);

int[] histogram = new int[256];

foreach (int pixel in buffer)
{
byte r = (byte)((pixel >> 16) & 0xff);
byte g = (byte)((pixel >> 8) & 0xff);
byte b = (byte)(pixel & 0xff);
int value = (r + g + b) / 3;
histogram[value]++;
}

int h = histogram.Max();

Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
this.Bitmap.Clear();

for (int i = 0; i < 256; i++)
{
int v = histogram[i] * 100 / h;
this.Bitmap.DrawLine(i, 100, i, 100 – v, Colors.Red);
}

this.Bitmap.Invalidate();
});
}

private void button1_Click(object sender, RoutedEventArgs e)
{
// Use standard camera on back of device.
_videoCamera = new VideoCamera();

// Event is fired when the video camera object has been initialized.
_videoCamera.Initialized += VideoCamera_Initialized;

// Add the photo camera to the video source
_videoCameraVisualizer = new VideoCameraVisualizer();
_videoCameraVisualizer.SetSource(_videoCamera);
button1.Content = “hhaha”;

}

Sintaks

   private VideoCamera _videoCamera;
        private VideoCameraVisualizer _videoCameraVisualizer;

digunakan untuk memanggil file (.cs) yang dibuat

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s