My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members

Provides a high level managed interface to libsane. Currently tested and working on the following scanners:

  • CANON DR-7580 (ADF)
  • HP PSC_2350 (Flatbed)

All I have around are 64-bit processors to test on, so there may need to be some tweaking of the low level stuff on 32-bit

Quirks

Strangely enough, I can query the device list and settings a good 2 seconds quicker than either scanadf or scanimage (Kinda makes me think i'm doing something wrong)

Example Usage

A simple scanadf clone (minus a few options and niceties)

using System;
using Sane;
using System.Collections.Generic; //Dict
namespace scanadfsharp
{
	class MainClass
	{
		private static Core sane;  //Core = Sane.Core
		private static List<Device> devices;
		public static void Main (string[] args)
		{
			sane = new Core();
			devices=new List<Device>();
			init();
			loadDevices();
			Dictionary<string,string> ops = new Dictionary<string,string>();
			Device selected=null;
			string format="image-{0}";
			for(int i=0;i<args.Length;i++)
			{
				string name=args[i];
				if(name.StartsWith("--"))
				{
					if(name == "--help")
					{
						listCommonOptions();
						listOptions();
						quit();
						return;
					}else if(name == "--device")
					{
						selected=getDevice(args[++i]);
						if(selected==null)
						{
							Console.WriteLine("Invalid device specified");
							quit();
							return;
						}
						continue;
					}else if(name == "--output")
					{
						format=args[i++];
						continue;
					}else{
						ops.Add(name.Substring(2),args[++i]);
					}
				}
			}
			if(selected == null)
			{
				selected=devices[0];
				Console.WriteLine("Using device {0}",selected.name);
			}
			if(ops.Count > 0)
			{
				foreach(string key in ops.Keys)
				{
					Option op = selected.getOption(key);
					if(op==null)
					{
						Console.WriteLine("{0} is not a valid option for this device",op.name);
					}
					op.setValue(ops[key]);
				}
			}
			Image.onProgressTick+=delegate(Image sender, double progress)
			{
				Console.WriteLine("Progress: {0}%",Math.Round(progress,2));
			};
			acquire(selected);
			quit();
		}
		public static void acquire(Device d)
		{
			string format = "image-{0}";
			acquire(d,format,0);
		}
		public static void acquire(Device d, string format)
		{
			acquire(d,format,0);
		}
		public static void acquire(Device d,string format,int index)
		{
			string filename=string.Format(format,index);
			System.IO.FileStream stream = System.IO.File.OpenWrite(filename);
			IWriter writer = new Sane.Writers.PNM(stream);
			
			Image img = Image.acquire(d,writer);
			Console.WriteLine("Wrote {0}",filename);
			img.destroy();
			writer.close();
			if(img.lastStatus.isGood() || img.lastStatus.isEOF())
			{
				acquire(d,format,index+1);
			}else{
				Console.WriteLine("Exit with status: {0}",img.lastStatus.description);
			}
		}
		public static void listCommonOptions()
		{
			Console.Write("scanadf-sharp\n  Options:\n    --help    show this help text\n" +
				"    --device [name]    set the device to use\n" +
				"    --output [format]  Set the filename format to use (default - image-[index])");
		}
		public static void init()
		{
			sane.init();
		}
		public static void quit()
		{
			sane.exit();
		}
		public static void listOptions()
		{
			foreach(Device d in devices)
			{
				Console.WriteLine("Options for device: {0}:",d.name);
				listOptions(d);
			}
		}
		public static void listOptions(Device d)
		{
			d.enumOptionGroups(delegate(Device sender,string title){
				Console.WriteLine("    {0}:",title);
				d.enumOptions(title,delegate(Device mdevice, string section, Option o){
					Console.WriteLine("\t--{0} {1} [{2}]\n\t    {3}",o.name,o.validOptionsString,o.getValue(mdevice.getHandle()),o.description);
				});
			});
		}
		public static void loadDevices()
		{
			sane.enumDevices(delegate(Device d){
				if(!devices.Contains(d))
					devices.Add(d);
			});
		}
		public static void listDevices()
		{
			foreach(Device d in devices)
			{
				Console.WriteLine("Device: {0}",d.name);
			}
		}
		public static Device getDevice(string name)
		{
			foreach(Device d in devices)
			{
				if(d.name == name)
				{
					return d;
				}
			}
			return null;
		}
		public static void begin(Device d)
		{
		}
	}
}
Powered by Google Project Hosting