Dec 24 2008

Signature Converter

Category: Software DevelopmentJeff @ 07:27

For a mobile application I'm working on, I needed a simple signature capture control. I found just such a control at the Code Project (http://www.codeproject.com/KB/mobile/SignatureCapture_PocketPC.aspx). The code will capture a signature and actually convert it into bitmap coordinates. This will allow me to store much less data rather than full images. But at some point I'm going to want to convert these bitmap coordinates back into an image. And unfortunately, the signature capture project only provides Java code for performing this process.

Well, if anyone else is interested, here is the Java code ported to C#. My next step will be to extend this class so that it isn't directly dependent on File System IO. But this should be enough to get anyone started who'd like to use the Signature Capture control from CodeProject, but wants to use .NET and C# to convert the bitmap coordinates back into an image.

using System;

using System.Collections;

using System.Collections.Generic;

using System.IO;

using System.Drawing;

using System.Drawing.Imaging;

namespace Agilification

{

    public class SignatureConverter

    {

        const int baseX = 25; const int baseY = 25;

 

        public static void Convert(String inputTxtFile, String outputGraphicsFile)

        {

            List<String[]> v = readFile(inputTxtFile); Hashtable ht = getMinMax(v); int minX = (Int32)ht["minX"]; int maxX = (Int32)ht["maxX"]; int minY = (Int32)ht["minY"]; int maxY = (Int32)ht["maxY"]; int width = maxX - minX + baseX; int height = maxY - minY + baseY; Image image = new Bitmap(width, height); Graphics g2d = Graphics.FromImage(image); var b = new SolidBrush(Color.White); g2d.FillRectangle(b, 0, 0, width, height); drawImage(g2d, v, minX, minY); g2d.Dispose(); image.Save(outputGraphicsFile, ImageFormat.Jpeg);

        }

 

        private static void drawImage(Graphics g2d, List<String[]> v, int minX, int minY)

        {

            for (int j = 0; j < v.Count; j++)

            {

                String[] points = (String[])v[j]; int x1 = 0; int y1 = 0; int x2 = 0; int y2 = 0; for (int i = 0; i < 4; i++)

                {

                    x1 = Int32.Parse(points[0]) - minX + (baseX / 2); x2 = Int32.Parse(points[2]) - minX + (baseX / 2); y1 = Int32.Parse(points[1]) - minY + (baseY / 2); y2 = Int32.Parse(points[3]) - minY + (baseY / 2);

                }

                var p = new Pen(Color.Black); g2d.DrawLine(p, x1, y1, x2, y2);

            }

        }

 

        private static Hashtable getMinMax(List<String[]> v)

        {

            Hashtable ht = new Hashtable(); int minX = 9999; int maxX = 0; int minY = 9999; int maxY = 0; for (int j = 0; j < v.Count; j++)

            {

                String[] points = (String[])v[j]; for (int i = 0; i < 4; i++)

                {

                    minX = (Int32.Parse(points[0]) < minX) ? Int32.Parse(points[0]) : minX; minX = (Int32.Parse(points[2]) < minX) ? Int32.Parse(points[2]) : minX; maxX = (Int32.Parse(points[0]) > maxX) ? Int32.Parse(points[0]) : maxX; maxX = (Int32.Parse(points[2]) > maxX) ? Int32.Parse(points[2]) : maxX; minY = (Int32.Parse(points[1]) < minY) ? Int32.Parse(points[1]) : minY; minY = (Int32.Parse(points[3]) < minY) ? Int32.Parse(points[3]) : minY; maxY = (Int32.Parse(points[1]) > maxY) ? Int32.Parse(points[1]) : maxY; maxY = (Int32.Parse(points[3]) > maxY) ? Int32.Parse(points[3]) : maxY;

                }

            }

            ht.Add("minX", minX); ht.Add("maxX", maxX); ht.Add("minY", minY); ht.Add("maxY", maxY); return ht;

        }

 

        private static List<String[]> readFile(String fileName)

        {

            List<String[]> v = new List<String[]>(); var reader = new StreamReader(fileName); String record = null; while ((record = reader.ReadLine()).Trim().Length > 0)

            {

                String[] points = new String[4]; points = record.Split(' '); v.Add(points);

            }

            return v;

        }

 

        [MTAThread]

        static void Main(String[] args)

        {

            try

            {

                String inputTextFile = ".\\Sign.txt"; String outputImageFile = ".\\Sign.jpg"; Convert(inputTextFile, outputImageFile);

            }

            catch (Exception ex)

            {

                Console.WriteLine(" exception: " + ex.ToString());

            }

        }

    }

}


Tags: ,

Comments are closed