/** * An example class showing connection to DataPaq via a socket. * * Author Neil Benn * Version 1.2 */ using System; using System.IO; using System.Net.Sockets; using System.Text; using System.Drawing; using System.Windows.Forms; namespace DataPaqRemoteExample { public class ImageForm : Form{ private Image img; public ImageForm(Image img) { this.img = img; init(); } private void init(){ PictureBox pb = new PictureBox(); pb.SizeMode = PictureBoxSizeMode.StretchImage; pb.ClientSize = new Size(img.Width/4, img.Height/4); pb.Image = img; this.Controls.Add(pb); } } public class DataPaqRemoteClient { StreamReader reader = null; StreamWriter writer = null; public DataPaqRemoteClient() { TcpClient client = new TcpClient("localhost", 8888); NetworkStream stream = client.GetStream(); reader = new StreamReader(stream); writer = new StreamWriter(stream); reader.ReadLine(); } public String RunScan(String uid) { Console.WriteLine("Scanning"); // perform a scan writer.Write("SCAN " + uid + " TEXT\r\n"); writer.Flush(); Console.WriteLine("SCAN RECEIVED " + reader.ReadLine()); // read in all the results, appending to the StringBuffer StringBuilder scanBuffer = new StringBuilder(); Boolean scanFirstLine = true; while (true) { String line = reader.ReadLine(); if (scanFirstLine) { if (line.StartsWith("ERR")) { throw new Exception("Scanner reported error - " + line + reader.ReadLine()); } scanFirstLine = false; } if (line.Equals("OK")) { break; } scanBuffer.Append(line).Append("\r\n"); } return scanBuffer.ToString(); } public Image GetLastScanImage(int scanPos){ // get the image of the last scan Console.WriteLine("Getting last scanned image"); writer.Write("LAST_IMAGE 0\r\n"); writer.Flush(); Console.WriteLine("reading image in"); // reading image in StringBuilder buffer = new StringBuilder(); Boolean imageFirstLine = true; while (true) { String line = reader.ReadLine(); buffer.Append(line).Append("\r\n"); // the first line may be an error so we check for that // and then set first lien to false as the rest of the // lines will not be in error if (imageFirstLine) { imageFirstLine = false; if (line.StartsWith("ERR")) { Console.WriteLine("Error " + line); throw new Exception("failed to read image " + line + " " + reader.ReadLine()); } } else { if (line.Length == 0) { break; } } } // we have the image we now need to convert the encoded string to bytes // and then write the image to a file byte[] decoded = Convert.FromBase64String(buffer.ToString()); MemoryStream ms = new MemoryStream(decoded); Image returnImage = Image.FromStream(ms); Console.WriteLine("Got Last Scanned Image"); return returnImage; } public void Close() { writer.Write("CLOSE\r\n"); writer.Flush(); Console.WriteLine("Close Acknowledgement " + reader.ReadLine()); } public String GetVersion() { writer.Write("VERSION\r\n"); writer.Flush(); String version = reader.ReadLine(); Console.WriteLine("Version Acknowledgement " + reader.ReadLine()); return version; } public String GetStatus() { writer.Write("STATUS\r\n"); writer.Flush(); String status = reader.ReadLine(); Console.WriteLine("Status Acknowledgement " + reader.ReadLine()); return status; } public static void Main() { Console.WriteLine("Starting DataPaq Remote"); DataPaqRemoteClient dataPaqExample = new DataPaqRemoteClient(); Console.WriteLine("Version is " + dataPaqExample.GetVersion()); Console.WriteLine("Status is " + dataPaqExample.GetStatus()); Console.WriteLine("Scan Result is \r\n" + dataPaqExample.RunScan("1")); Image img = dataPaqExample.GetLastScanImage(0); dataPaqExample.Close(); ImageForm imgForm = new ImageForm(img); imgForm.AutoSize = true; imgForm.Show(); Application.Run(imgForm); Console.ReadKey(); Console.Write("Finishing DataPaq Remote"); } } }