CORE-POS/IS4C

View on GitHub
pos/is4c-nf/scale-drivers/drivers/NewMagellan/Signature.cs

Summary

Maintainability
A
1 hr
Test Coverage
/*******************************************************************************

    Copyright 2010 Whole Foods Co-op

    This file is part of IT CORE.

    IT CORE is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    IT CORE is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    in the file license.txt along with IT CORE; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*********************************************************************************/

using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace BitmapBPP 
{

public class Signature
{
    private readonly string filename;
    private readonly List<Point> points;
    private Bitmap bmp;

    private const int IMAGE_WIDTH = 512;
    private const int IMAGE_HEIGHT = 128;

    public Signature(string fn, List<Point> pts)
    {
       filename = fn;
       points = pts; 

       bmp = new Bitmap(IMAGE_WIDTH, IMAGE_HEIGHT);
       draw();
    }

    public void draw()
    {
        Graphics g = Graphics.FromImage(bmp);

        Brush whiteBrush = new SolidBrush(Color.White);
        bmp.SetResolution(IMAGE_HEIGHT/2, IMAGE_WIDTH/2);
        g.TranslateTransform(0f, IMAGE_HEIGHT);
        g.ScaleTransform(1f, -1f);
        g.FillRegion(whiteBrush,
            new Region(new Rectangle(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT)));

        Brush blackBrush = new SolidBrush(Color.Black);
        Pen p = getPen(blackBrush);
        List<Point> line = new List<Point>();
        foreach (Point point in points) {
            if (point.IsEmpty) {
                try {
                    g.DrawLines(p, line.ToArray());
                    line.Clear();
                } catch (Exception) {
                }
            } else {
                line.Add(point);
            }
        }

        bmp.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp);
        var bpp = BitmapConverter.To1bpp(filename);
        File.WriteAllBytes(filename, bpp);
    }

    private Pen getPen(Brush b)
    {
        Pen p = new Pen(b);
        p.Width = 10;
        p.StartCap = System.Drawing.Drawing2D.LineCap.Round;
        p.EndCap = System.Drawing.Drawing2D.LineCap.Round;
        p.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;

        return p;
    }
}

}