Showing posts with label Surface Tag. Show all posts
Showing posts with label Surface Tag. Show all posts

Sunday, May 24, 2009

Generating and Printing Surface IdentityTag

Tags support is the most important feature in Microsoft Surface.Tags enable our application to recognize objects through surface.As you know there are 2 types of tags.Byte tag and identity tag.So before placing objects on the surface to recognize we have to print the tag and stick on the bottom part of the object.Then only surface can identify the tag value through its infrared sensing mechanism.There are 3 ways to visualize the tags.One is using the command line tool GenTag.exe and another using it’s visual tool GenTagUI.exe and last using our own programs which make use of the class IdentityTagGenerator

Using IdentityTag Image Generator
Tool is located at

[Install drive]:\Program Files\Microsoft SDKs\Surface\v1.0\Tools\GenTag\GenTag.exe

Syntax :
GenTag.exe <Series> <Value> <FileName.png> [/dpi:number]

Value and Series are in Hex.This tool outputs the tag as png image.Later we can print that image.Dots Per Inch (dpi) is optional.

Using IdentityTag printing tool
Surface now includes a tool in it’s sdk which helps us to generate and print IdentityTags.You can locate the program in start menu itself

Programs->Microsoft Surface sdk 1.0 sp1->Tools->Identity Tag Printing Tool

or in the location

[Install drive]:\Program Files\Microsoft SDKs\Surface\v1.0\Tools\GenTag\GenTagUI.exe

There is nothing to explain about using the tool I think.Try yourself.

Using IdentityTagGenerator class
Microsoft has provided API to visualize the tags and later we can save or print them.The entry to tag visualization API is IdentityTagGenerator.That class contains 2 static methods for getting tag in it’s actual form.

  • Image GenerateTag(long series,long value) : As its signature implies, it returns the tag image according to parameters series and value.There is an overload available which takes dpi as parameter and returns image according to that.
  • RenderTag(long series,long value,Point point,Graphics g) :Draws identity tag on the Graphics object.Mainly targeted to be used in Windows forms applications.Also this helps in distributing tags through asp.net applications.
In the first method we can set the returned Image object as source of image control in our application.The second method uses Graphics object and that is available in OnPaint methods or we can create it using the Graphics class.

The usage is straight forward.Seems there is no need to post a sample.

NB: Printed tag should be exactly 1.125 inches square in order to get detected by Surface.

Friday, May 8, 2009

Using TagVisualizer and TagVisualization in Surface development

This post describes about the programming aspects of Surface tags.If you are not familiar with surface and surface tags please see posts below

Surface Application Development using Simulator and VisualStudio.
Identifying Finger ,Tag and Blob in Surface using Contacts class

TagVisualizer

TagVisualizer is a ContentControl derived from SurfaceContentControl and it’s basic function is to show Tag Visualizations when the corresponding tag is placed on the surface. Tag Visualizations are added by means of TagVisualizationDefinition.For that the TagVisualizer has a property called Definitions which is of type TagVisualizationDefinitionCollection.Each TagVisualizationDefinition tells what to display when a tag is being placed.

TagVisualizationDefinitions

This defines the visual against the tag value.The visual can be a UserControl and it is mentioned in the form of Uri.There are 2 types of tags in surface as you know.So there should be a mechanism to differentiate these tags while displaying.The method here used here is inheritance.ByteTagVisualizationDefinition  which is derived from abstract class TagVisualizationDefinition deals with the byte tags and IdentityTagVisualizationDefinition derived from same ,deals with Identity tags.A simple xaml code which shows a red colored usercontrol on byte tag of value 10 is shown below.

<Grid Background="{StaticResource WindowBackground}">
<s:TagVisualizer>
<s:TagVisualizer.Definitions>
<s:ByteTagVisualizationDefinition Value="10"
Source="ByteTagVisualization.xaml">
</s:ByteTagVisualizationDefinition>
</s:TagVisualizer.Definitions>
</s:TagVisualizer>
</Grid>


<UserControl x:Class="SurfaceApplication1.ByteTagVisualization"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300" Background="Red">
<Grid>

</Grid>
</UserControl>

ByteTagVisualization is a normal UserControl available in WPF.Hope this is clear like water.Shows the UserControl ByteTagVisualization when we place a byte tag with value 10 in the area covered by TagVisualizer.


TagVisualization


This is again a ContentControl which can be used as Source in TagVisualizationDefinition.The advantage here is that, the TagVisualization class has some properties which are related to the tag and tag data.Below goes steps which explains how to create TagVisualization.


  1. Create a UserControl

  2. In the xaml file change the root tag as s:TagVisualization where s is xmlns:s="http://schemas.microsoft.com/surface/2008" namespace of surface controls.

  3. In the xaml.cs file change the base class to TagVisualization.Also change the constructor to suit the class.


A sample TagVisualization is given below



<s:TagVisualization x:Class="SurfaceApplication1.IdentityTagVisualization"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="http://schemas.microsoft.com/surface/2008">
<StackPanel>
<TextBlock>An Identity tag has been placed</TextBlock>
</StackPanel >
</s:TagVisualization>

Getting tag values in TagVisualization and databinding


Even though the TagVisualization has the Property TagData we can’t use that in binding because that is not WPF friendly.



<TextBlock  Text="{Binding VisualizedTag.Type,ElementName=tagVisualization}" />

The above code won’t work

To enable databinding in TagVisualization we have to write our own properties which are data bindable.Then to get the value of tag which is placed over TagVisualizer override the OnGotTag method and get value from VisualizedTag property.



public partial class IdentityTagVisualization : TagVisualization
{
public long? TagValue
{
get { return (long?)GetValue(TagValueProperty); }
set { SetValue(TagValueProperty, value); }
}
public static readonly DependencyProperty TagValueProperty =
DependencyProperty.Register("TagValue", typeof(long?), typeof(IdentityTagVisualization));

public IdentityTagVisualization()
{
InitializeComponent();
}
protected override void OnGotTag(RoutedEventArgs e)
{
if (VisualizedTag != null)
{
TagValue = (VisualizedTag.Type == TagType.Byte) ?
VisualizedTag.Byte.Value :
VisualizedTag.Identity.Value;
}
base.OnGotTag(e);
}
}
}

Now you can databind the value of the tag in xaml.



<s:TagVisualization x:Class="SurfaceApplication1.IdentityTagVisualization"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="http://schemas.microsoft.com/surface/2008" x:Name="tagVisualization">
<StackPanel>
<TextBlock>Tag has been placed value is </TextBlock>
<TextBlock Text="{Binding TagValue,ElementName=tagVisualization}" />
</StackPanel >
</s:TagVisualization>

Download sample from here.

Thursday, May 7, 2009

Identifying Finger Tag and Blob in Surface using Contacts class

If you are not much familiar with Microsoft surface just have a look here in my another blog and get start in Surface development from here.

There is an attached event called ContactDown in the class Contacts.We can subscribe to that in any visuals and process according to that.

<s:SurfaceWindow x:Class="SurfaceApplication.SurfaceWindow1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="http://schemas.microsoft.com/surface/2008">
<Grid>
<TextBlock>Place contact here</TextBlock>
<Rectangle Grid.Row="1" Fill="#0fff0000"
Width="100" Height="100" s:Contacts.ContactDown="Rectangle_ContactDown" />
</Grid>
</s:SurfaceWindow>











Code behind





private void Rectangle_ContactDown(object sender, Microsoft.Surface.Presentation.ContactEventArgs e)
{
if (e.Contact.IsTagRecognized)
{
switch(e.Contact.Tag.Type)
{
case Microsoft.Surface.Presentation.TagType.Byte:
MessageBox.Show(string.Format("Byte tag identified value={0}", e.Contact.Tag.Byte.Value));
break;
case Microsoft.Surface.Presentation.TagType.Identity:
MessageBox.Show(string.Format("Identity tag identified value={0}", e.Contact.Tag.Identity.Value));
break;
}
return ;
}
if (e.Contact.IsFingerRecognized)
{
MessageBox.Show("Finger contact identified");
}
else
{
MessageBox.Show("Blob identified");
}
}









This code just shows a message box. Processing the contacts such as showing something upon placing a contact and all will be discussed later in the post ‘Using TagVisualizer and TagVisualization’.