derekchan84
(11 comments, 64 posts)
This user hasn't shared any profile information
Posts by derekchan84
Capture Webcam Shots and Compress it on Silverlight with MVVM & FJCore – Step-by-Step
0Webcam and microphone integration was one of the exciting features introduced in Silverlight 4. With that, you can start creating your very own image capturing web application or videoconferencing app easily. One of the issues I faced is to ensure that it works with the Model View ViewModel (MVVM) pattern as well.
I have also used some 3rd party tools for doing PRISM and image compression. So please get them from the list as shown below:
So here goes the steps:
Step 1 – Create the View
I am just going to create a simple view where it consists of a rectangle to display your video stream, and an image control above it to show the snapped webcam picture. Together with the image, I also have a button where you can click on to trigger a snap, and another for you to retake if you are not happy with it.
All the way down at the bottom, there is an option for you to start capturing your webcam stream. All bindings have already been placed here as well as the relevant converters. If you are confused about converters, don’t worry, they are available in the full source code here.
Below is the xaml representing the View:
<UserControl x:Class="SimpleWebcamApp.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:converters="clr-namespace:SimpleWebcamApp.Converters" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.Resources> <converters:VisibilityConverter x:Key="VisibilityConverter" /> <converters:VisibilityConverterReverse x:Key="VisibilityConverterReverse" /> <converters:ByteToImageConverter x:Key="ByteToImageConverter" /> </Grid.Resources> <Border Grid.Column="0" Margin="10,8,8,8" BorderThickness="4" Background="Black" Grid.Row="0" Width="320" Height="240"> <Grid> <Rectangle Stroke="Black" x:Name="rectVideo" Fill="{Binding VideoBrush}" Visibility="{Binding IsSnapshotVisible, Converter={StaticResource VisibilityConverterReverse}}"/> <Image x:Name="snapshot" Source="{Binding MemberPicture, Converter={StaticResource ByteToImageConverter}}" Visibility="{Binding IsSnapshotVisible, Converter={StaticResource VisibilityConverter}}" /> <Button Content="Click to take photo" Height="30" VerticalAlignment="Bottom" Command="{Binding TakePhotoCommand}" Visibility="{Binding IsSnapshotVisible, Converter={StaticResource VisibilityConverterReverse}}"/> <Button Content="Click to retake photo" Height="30" VerticalAlignment="Bottom" Command="{Binding RetakePhotoCommand}" Visibility="{Binding IsSnapshotVisible, Converter={StaticResource VisibilityConverter}}"/> </Grid> </Border> <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center"> <Button x:Name="btnStartWebCam" Content="Start Capture" Command="{Binding StartCaptureCommand}"> </Button> </StackPanel> </Grid> </UserControl>
Step 2 – Create the View Model
The view model will contain all the core functionality of your properties that you have binded on your controls. Ensure that your view model class is implementing iNotifyPropertyChanged to ensure that the UI will update the bindings. The full codes for the entire solution is available for download here.
Alright, back to the codes then. 1st we have the view model code for starting up the webcam:
private void StartCaptureCommandAction() { if (CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices().Count > 0) { VideoBrush = new VideoBrush(); CapSource = new CaptureSource { VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice() }; if (CapSource != null) { if (CapSource.State != CaptureState.Stopped) { CapSource.Stop(); // stop whatever device may be capturing } // create the brush VideoBrush.SetSource(CapSource); // request user permission and display the capture if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess()) { CapSource.Start(); } } } else { MessageBox.Show("No webcam detected"); } }
Then we have the code to capture the image stream:
private void TakePhotoCommandAction() { if (CapSource != null && CapSource.State == CaptureState.Started) { CapSource.CaptureImageCompleted += (s, a) => { //WriteableBitmap bmp = a.Result; WriteableBitmap bmp = resizeBicubic(a.Result, 800, 800); //put the stream in the BinaryReader Stream stream = Encode(bmp, 80); BinaryReader binary = new BinaryReader(stream); //Read bytes from the BinaryReader and put them into a byte array. MemberPicture = binary.ReadBytes((int)stream.Length); MemberPictureStream = stream; CapSource.Stop(); IsSnapshotVisible = true; }; CapSource.CaptureImageAsync(); } }
Step 3 – Hook up your View to the View Model
Hooking up the view model to the view is pretty simple. All you need to do is to create a new instance of the view model and attach it to the view’s cs file DataContext as shown below:
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e) { MainPageViewModel vm = new MainPageViewModel(); this.DataContext = vm; //vm.Initialize(); }
And that’s it! You will now be able to capture screencaps from your webcam and retake if you are not happy.
Full source code for this application is available here.
MIND Speed Update #1 Flashback
0We have successfully got our MIND Speed Update revived on the 4th of August 2011 at Microsoft Malaysia, KLCC. We had an awesome attendance of about 30 developers rocking the house and learning new development tools, methods and best practices. Below is the summary of some of the topics and what we have learnt during the event:
1. Tim Chew – Memory Leak and How to Overcome it
Tim gave us some basic reminder that memory leaks can happen when developing a .NET application. One of the examples he highlighted to us was event handlers that were assigned but not unassigned, leaving memory to be used up. He also show us some debugging tools which will allow us to find out which referencing object is the one leaking the memory. Pretty sweet!
2. Chris Leong – Starting your Technopreneurship Business with Cradle
Always have an idea but do not have the funds to start it? Chris’ Cradle can fund you up to RM150,000 if you have a viable tech business idea. He also mentioned the 4 different types of business ideas; Vitamins, Chocolate, Sleeping Pills and Cocaine. If you think your idea falls into the latter 2 categories, it is time to contact Chris to get your money! ![]()
3. Walter Wong – Securing your ASP.NET Applications
According to Walter, 90% of the web service developer he meets never bother securing their services. Yeah, that is because most of them do not have the knowledge on how to do it, or they are just plain lazy. Walter introduced Windows Identification Foundation (WIF) and how you can put in relevant permissions to secure your web service. He also shown us some analyzers such as Microsoft Web Application Configuration Analyzer and Microsoft Based Security Analyzer so that developers can check how secure their app and servers are to prevent hacking.
4. Chan Wei Min – Recycling Arrays
He has no voice due to sore throat and was unable to speak during the presentation. To overcome that, he ended up creating an app which uses Microsoft’s Voice Synthesizer for his presentation (A lady’s voice btw). Epic!
Anyway, back to the topic, Wei Min introduced how Recycling Arrays can help improve performances especially in games where sprites have to be created and reused all the time. Using a Windows Phone 7 emulator, he simulated what will happen to the PC’s performance if you use a normal list array compared to recycling arrays. Interesting topic (and way of presenting too)!
And that’s it for our first MIND Speed Update in years. Below is a group shot of everyone present. Hope to see you all again next month (Bring more chicks too k?). Same time, same place, free food, different speakers (hopefully!)
Happy Wi-Fi Day!
0Happy Wi-Fi 802.11 Day!!!! 8/02/11 translates to the standard 802.11 that we are using to connect to networks wirelessly and It only happens once in a lifetime.
Geeky! How will you celebrate it?
Windows Phone 7 Mango Build 7712 Released with New Features!
0A new beta ISV update for mango have been released!
Some of the new features includes
- A new Windows Phone Boot logo
- Twitter integration
- LinkedIn integration
- More responsive Zune / Music + Videos hub
- New ‘Me’ icon in Bing maps
- No more Start page in task switcher
Windows Phone 7 Mango Released for Manufacturing!
0Heads up! Microsoft have signed off their Windows Phone 7 Mango build to manufacturers.
So what does that mean? Simple, we’ll be seeing phones from manufacturers like HTC, Samsung, Dell, LG and Fujitsu releasing their new phones anytime as early as August!
Check out what are the latest Mango features here at Engadget!
Links to Windows Phone 7 Resources
0
Meant for those who are attending my Windows Phone training course!
Click here to open my Skydrive
Using SQL ‘LIKE’ Statement on .NET SqlDatasource for MySQL
1I am back with another technical post! This time, it is with regards with the SQL ‘LIKE’ statement in SQLDatasources which are available in .NET development. Recently, I was having trouble figuring out how you can actually use a SQL statement such as
SELECT * FROM products WHERE productName LIKE ‘%Computers%’
inside a SQLDatasource in an ASP.NET project. Unlike SQL Server, MySQL does not understand the syntax if you use the ‘+’ symbol and put it this manner:
<asp:SqlDataSource ID=”SqlSearchResults” ConnectionString=”<%$ myConnString %>” ProviderName=”<%$ ConnectionStrings:myConnString.ProviderName %>” runat=”server” SelectCommand=”SELECT * FROM product WHERE productName LIKE ‘%’ + @productName + ‘%’)”>
</asp:SqlDataSource>
That is where the use of the CONCAT statement in MySQL comes to play. As the MySQL Database engine does not understand the ‘+’ symbol which we frequently use in SQL Server, CONCAT will help to replace this irregularity in MySQL. So, to get it to work, just modify the data source as follow:
<asp:SqlDataSource ID=”SqlSearchResults” ConnectionString=”<%$ myConnString %>” ProviderName=”<%$ ConnectionStrings:myConnString.ProviderName %>” runat=”server” SelectCommand=”SELECT * FROM product WHERE productName LIKE CONCAT(‘%’, @productName, ‘%’)”>
</asp:SqlDataSource>
With this, your problems with mySQL LIKE will be solved immediately! Hope this helps some people out there…
iPhone 4 Malaysia Release Date in August?
0Went through TechCentral today and found this quote from the Maxis COO:
Van Overbeke said the iPhone 4 could be available here by August, but declined to provide prices. And, he said, Maxis’ data packages for the smartphone “would not be very expensive.”
As for the iPad, he said it is possible that Maxis will be distributing the 3G version of the tablet computer. “But it’s really up to Apple at this point.”
So is the iPhone coming in August for Maxis or for both Maxis and DiGi? Hope to get more updates on this soon! Read the full article here at TechCentral!
Updated to iPhone’s iOS4 and Downloaded the Vuvuzuela App!
5I have just downloaded the latest iOS4 update and jailbroken it with PwnageTool 4.01 for my iPhone 3GS. For those who are interested in jailbreaking it, visit this URL at RedmondPie and follow the guide. All iPhone models from 2G to 3GS can apply this update. Again, please jailbreak at your own risk and a Mac is required to complete this process! As promised by Apple, iOS4 comes with over 100 new features which includes mainly; multitasking, folder management, improved e-mail, iBooks and digital zoom for the camera. For the full list of features, click here.
Below are some screens on the latest features in iOS4 from my iPhone (From top left: (1) A brand new Home Screen, (2) Folder Management, (3) Multitasking bar, (4) Camera Digital Zoom up to 5x:

Due to the World Cup season, I also managed to find a free Vuvuzuela app which you can download from the AppStore. All you need to do is to select your favorite team and touch or shake your phone to emit the buzzing sound which has been a culture and also an annoyance to players and fans altogether throughout World Cup 2010. You can get that app from here via iTunes too. A screenshot available below.
I have also realized in a way that the new iOS4 is faster and a little more smoother when in comes to rendering transitions than previous OS versions as opposed to what other people commented. Probably the 3GS still has enough juice to run iOS4 without any issues. Signing off for now and enjoying Apple’s latest iOS. Wooot! I can’t wait for iPhone 4 already!
iPhone 4 Round-Up
0
The iPhone 4 has already gone on sale at the United States yesterday. Summary: Thousands of people are lining up to purchase it, iOS4 is already jailbroken and the phone breaks easily when it drops (from only just 1 foot mind you)!
Below are a round-up of some interesting articles:












