Archive

Archive for March, 2012

Convert an Image to text in C#

March 14, 2012 1 comment

If you want to extract text from an image, then you can use a process called OCR.  If you create a new project in Visual Studio, then make a web service reference to http://free-ocr.co.uk/ocr.asmx called “ocr”

A limitation of this free service, is that the image must be at most 100 x 100 pixels

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using simpleOCR.ocr;

namespace simpleOCR
{
class Program
{
static void Main(string[] args)
{

var sFile = new FileStream(@”C:\hello.jpg”,FileMode.Open);
byte[] PhotoBytes = new byte[sFile.Length];
sFile.Read(PhotoBytes, 0, PhotoBytes.Length);
simpleOCR.ocr.ocr webservice = new simpleOCR.ocr.ocr();
Console.Out.Write(webservice.Analyze(“your email”, PhotoBytes));
Console.ReadLine();
}
}
}

Advertisements
Categories: Uncategorized

Parsing WDDX with a Regular Expression

March 9, 2012 Leave a comment

WDDX is an XML Serialization format native to ColdFusion, and can be parsed in .NET using WDDX.NET (http://wddxnet.codeplex.com/)  – However, it is an extra assembly in your deployment, and sometimes, something more simple (but less correct) may be handy.

Take these two WDDX packets for example;

<wddxPacket version=’1.0′><header/><data><struct>
<var name=’NOMARP’><string>Madrid</string></var>
<var name=’TYPCLA’><string>TOUTES</string></var>
<var name=’DIRECT’><number>1.0</number></var>
<var name=’NOMCIE’><string>Iberia</string></var>
<var name=’DATDEP’><string>070912</string></var>
<var name=’TAXES’><number>53.0</number></var>
<var name=’CDEARP’><string>MAD</string></var>
<var name=’REDUC’><number>0.0</number></var>
<var name=’TOTALD’><number>109.0</number></var>
<var name=’IDPROP’><string>MDcwMTExMjM0OTJBUlVNQURBTENJQiBXMDMwOTMwMzE5UkVTQVhNTCAgIDEyMzQ5Mg==</string></var>
<var name=’REMMTE’><string>0</string></var>
<var name=’DATRET’><string>140912</string></var>
<var name=’CODCIE’><string>IB</string></var>
<var name=’REMMTA’><string>0</string></var>
<var name=’REMMTB’><string>0</string></var>
<var name=’TAXADT’><string>53.63</string></var>
<var name=’QBDOSS’><number>56.0</number></var>
<var name=’RECODE’><string></string></var>
<var name=’TAXBEB’><string>0</string></var>
<var name=’RELIBL’><string>-</string></var>
<var name=’TAXENF’><string>0</string></var>
<var name=’PRIXE’><number>0.0</number></var>
<var name=’REMSSE’><string></string></var>
<var name=’PRIXB’><number>0.0</number></var>
<var name=’NUMSEG’><number>2.0</number></var>
<var name=’REMSSB’><string></string></var>
<var name=’SEQPRO’><number>1.0</number></var>
<var name=’PRIXA’><number>56.0</number></var>
<var name=’REMSSA’><string></string></var>
<var name=’TYPRO’><string></string></var>
<var name=’MSGREM’><string>Nant</string></var>
<var name=’FLGCIE’><string>IB_small.gif</string></var></struct></data></wddxPacket>

<wddxPacket version=’1.0′><header/><data><recordset rowCount=’2′ fieldNames=’SEQPRO,IDSEG,SEQSEG,CODSEG,SEQVOL,DATDEP,HEUDEP,DATARR,HEUARR,VILDEP,VILARR,CIESEG,CIENOM,NUMVOL,DEPNOM,ARRNOM,TYPEQU,OPRCIE,NESCAL’ type=’coldfusion.sql.QueryTable’>
<field name=’SEQPRO’><string>1</string><string>1</string></field>
<field name=’IDSEG’><string>NahjbcJUNFNIRaadfeLZw</string><string>CbejbcJNIRUNFaadgbLZw</string></field>
<field name=’SEQSEG’><number>1.0</number><number>2.0</number></field>
<field name=’CODSEG’><string>A</string><string>R</string></field>
<field name=’SEQVOL’><number>1.0</number><number>2.0</number></field>
<field name=’DATDEP’><string>070912</string><string>140912</string></field>
<field name=’HEUDEP’><string>2255</string><string>2140</string></field>
<field name=’DATARR’><string>070912</string><string>140912</string></field>
<field name=’HEUARR’><string>2355</string><string>2240</string></field>
<field name=’VILDEP’><string>MAD</string><string>ALC</string></field>
<field name=’VILARR’><string>ALC</string><string>MAD</string></field>
<field name=’CIESEG’><string>IB</string><string>IB</string></field>
<field name=’CIENOM’><string>Iberia</string><string>Iberia</string></field>
<field name=’NUMVOL’><string>354</string><string>361</string></field>
<field name=’DEPNOM’><string>Madrid</string><string>Alicante</string></field>
<field name=’ARRNOM’><string>Alicante</string><string>Madrid</string></field>
<field name=’TYPEQU’><string>320</string><string>320</string></field>
<field name=’OPRCIE’><string>—</string><string>—</string></field>
<field name=’NESCAL’><string>0</string><string>0</string></field></recordset></data></wddxPacket>

So, for this format, I used the Regular expression:

const string strWddxRegex = @”name..(?<Name>\w+)..(.(string|number).(?<Value>[\w.=]*)..(string|number).)+”;

Which served the purpose!

Categories: Uncategorized

Content Validation 5.5.2 WP7

March 9, 2012 Leave a comment

One of the most common reason for rejection for Windows Phone 7 (Wp7) apps, is support for dark and light themes.  Most developers work in the dark theme, then not realizing that the text can’t be read when the phone is switched to light theme. This is picked up by Microsoft Testers, and reported as a Content Violation 5.5.2

Requirements
Application content, such as text and visual
elements, must be visible and legible regardless of
the phone theme. For example, if the phone
theme changes from black background to white
background, the text and visual elements of your
application must be visible or legible.

Comments: STEPS TO REPRODUCE
1. Navigate to the Settings page in the app list.
2. Tap theme and change Background to ‘Light’.
3. Launch your application.
4. Verify that the text and visual elements of the application are visible and legible.
RESULT
The application UI text is not legible when viewed in the ‘Light’ theme.

 

Here, is a solution I have come up with for “Panorama” style apps, by creating two backgrounds, one mostly black, one mostly white, and then calling the following function on MainPage_Loaded

/// <summary>
/// Detect theme, and apply background accordingly
/// </summary>
private void DisplayState()
{
Color lightThemeBackground = Color.FromArgb(255, 255, 255, 255);
SolidColorBrush backgroundBrush =
Application.Current.Resources[“PhoneBackgroundBrush”] as SolidColorBrush;
// Assume dark theme
BitmapImage bitmapImage = new BitmapImage(new Uri(“PanoramaBackground.png”, UriKind.Relative));
if (backgroundBrush.Color == lightThemeBackground)
{
// you are in the light theme
bitmapImage = new BitmapImage(new Uri(“LightPanoramaBackground.png”, UriKind.Relative));
}
ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = bitmapImage;
this.panorama.Background = imageBrush;
}

 

Categories: Uncategorized