Example Of C# Code I wrote This code is for an automatic book scanner
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using WIA; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; using System.Drawing.Drawing2D; using System.Threading; using iTextSharp; using iTextSharp.text; using iTextSharp.text.pdf; using System.Text; using System.IO; using System.Data; using System.Data.SqlClient; using System.util;
/// <Comments> ///This is the starting point for our application ///We have a main class and we have a start scanning class which will be called from the form when the ///user tells the form it is time to start our application /// </Comments> namespace BookScanCode { static class MainClass { public static double Brightness = 0; public static double Contrast = 0; public static double ImageWidth = 100; public static double ImageHeight = -100; public static bool killprogram = false; public static ImageFile Image1; public static ImageFile Image2; public static Document myDocument; //public static string Scanner1Name; //public static string Scanner2Name; //public static DeviceInfo Scanner1; //public static DeviceInfo Scanner2; public static BookScanner Scanner1; public static BookScanner Scanner2; //create a PDF variable here so it can be accessed everywhere //public static PreviewImage MyPreviewForm; /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { //Start up calls Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //This is directly related to the sizing of the images when we get them from the scanner. iTextSharp.text.Rectangle myRectangle = new iTextSharp.text.Rectangle(0, 0, 850, 1170);
myDocument = new Document(myRectangle, 0.0f, 0.0f, 0.0f, 0.0f); ////Show the user the main form //Application.Run(new Form1()); Scanning.ProcessAndPutImagesInPDF(); ////myDocument = new Document(myRectangle, 0.0f, 0.0f, 0.0f, 0.0f); //Scanning.ProcessAndPutImagesInPDF(); //myDocument = new Document(myRectangle, 0.0f, 0.0f, 0.0f, 0.0f); //Scanning.ProcessAndPutImagesInPDF();
} } public static class PLCToPCCom { //public static string GetSignal() //{ // //We will create a thread which will use this module in our process of getting the signal from the PLC // return hypotheticalvalue; //} public static void SendSignal(string SignalVal) { //Here we will be sending a signal to the PLC } } public static class Scanning { public static void GetScannerNames() { //********************Call to the PLC to turn on Scanner 1 //Create an instance of the device manager which enumarates connected devices for WIA var myDeviceManager = new DeviceManager(); //Iterate through every connected device. If a scanner try to connect. If successful, it is Scanner 1, so store name. for (int i = 1; i <= myDeviceManager.DeviceInfos.Count; i++) { //See if it is of scanner type if(myDeviceManager.DeviceInfos[i].Type == WiaDeviceType.ScannerDeviceType) { try { //Try to connect myDeviceManager.DeviceInfos[i].Connect(); //If the connection is successful, then the name will be stored MainClass.Scanner1 = new BookScanner(myDeviceManager.DeviceInfos[i],myDeviceManager.DeviceInfos[i].DeviceID); } //If there is an exception do nothing. Might not be necessary, but we can leave it for now catch { } } } //***************Turn on Scanner 2
//Now go through the list again for (int j = 1; j <= myDeviceManager.DeviceInfos.Count; j++) { //If we have a device of scanner type proceed if (myDeviceManager.DeviceInfos[j].Type == WiaDeviceType.ScannerDeviceType) { //If it is not scanner 1, and it is online, it must be scanner 2 if (myDeviceManager.DeviceInfos[j].DeviceID != MainClass.Scanner1._deviceID) { //Same as with scanner 1 part above try { myDeviceManager.DeviceInfos[j].Connect(); MainClass.Scanner2 = new BookScanner(myDeviceManager.DeviceInfos[j],myDeviceManager.DeviceInfos[j].DeviceID);
} catch { } } } } //Now we have stored both scanner ID's in the correct respective variables. We will check a little later to see if the scanners work correctly }
public static void DoPreviewScan() { //File name for where we will store the preview image const string PreviewFilePath = @"C:\temp\preview.jpg"; //Create a device manager to handle the connected devices and their info //var myDeviceManager = new DeviceManager(); //We only need 1 scanner (the first scanner) for the preview test procedure. It must be declared outside the loop to avoid CLR errors //That is why we also need to overload the constructors because when we declare we have no info to pass //BookScanCode.BookScanner Scanner1 = new BookScanCode.BookScanner(); //for (int i = 1; i <= myDeviceManager.DeviceInfos.Count; i++) //{ // //Connect to scanner 1 // if (myDeviceManager.DeviceInfos[i].DeviceID == MainClass.Scanner1._deviceID) // { // //Set scanner 1 to the one we determined has the ID of Scanner 1
// Scanner1 = new BookScanCode.BookScanner(myDeviceManager.DeviceInfos[i]); // } //} //*******Signal for the PLC to get in position and wait until it is there (Maybe we get a respone from the PLC)
//This is where we will store out image initially ImageFile myImagevariable; //Do the scan' if (MainClass.Scanner1 != null) { //Scan the image myImagevariable = MainClass.Scanner1.Scan(); //if the file is nonexistant create new, otherwise replace if (System.IO.File.Exists(PreviewFilePath) == false) { myImagevariable.SaveFile(PreviewFilePath); } else { System.IO.File.Delete(PreviewFilePath); myImagevariable.SaveFile(PreviewFilePath); } } //Now we have saved out image in the correct file so it can be loaded to our settings form }
//Here we will be scanning all of the pages to be scanned after the preview settings have been gathered //This gets more compplicated because we will be scanning with both scanners //We will need to use threading in this procedure public static void MainScanSequence() { Thread ScanWithScanner1 = new Thread(Scan1); //Set priority to highest ScanWithScanner1.Priority = ThreadPriority.Highest; //Do for scanner 2 also Thread ScanWithScanner2 = new Thread(Scan2); ScanWithScanner2.Priority =ThreadPriority.Highest; //Now we have the scanner infos we need
ScanWithScanner2.Start(); //Pause ScanWithScanner1.Start(); while(ScanWithScanner1.IsAlive || ScanWithScanner2.IsAlive) { //wait until we have the images } //Now we are done with the scanning and we can worry about processessing the images ProcessAndPutImagesInPDF(); {
} } //This is a complex idea. Basically, the original delcations+class cannot be correctly loaded from the libraries (its a bug), so //I created my own enumeration + abstract class to use more locally public abstract class TimsFormatID { //One of these was declared in the original program Dolina and I looked at. public const string wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"; public const string wiaFormatGIF = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}"; public const string wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"; public const string wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"; public const string wiaFormatTIFF = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}"; }
static void Scan1() { var device = MainClass.Scanner1._deviceInfo.Connect(); // Start the scan var item = device.Items[1]; //AdjustScannerSettings(item, 100, width, height, MainClass.Brightness * 10.0, 0);//MainClass.Contrast * 10.0); //Here is where the actual scan happens var imageFile = (ImageFile)item.Transfer(TimsFormatID.wiaFormatJPEG);// Return the imageFile //Return the image file MainClass.Image1 = imageFile; } static void Scan2() { var device = MainClass.Scanner2._deviceInfo.Connect(); // Start the scan var item = device.Items[1]; //AdjustScannerSettings(item, 100, width, height, MainClass.Brightness * 10.0, 0);//MainClass.Contrast * 10.0); //Here is where the actual scan happens var imageFile = (ImageFile)item.Transfer(TimsFormatID.wiaFormatJPEG);// Return the imageFile //Return the image file MainClass.Image2 = imageFile; } public static void ProcessAndPutImagesInPDF() { //Here we need to adjust the brightness, contrast, and cropping, and the put the images in the open PDF File //Scale the images //now add the images to the PDF file //if the file is nonexistant create new, otherwise replace //if (System.IO.File.Exists(@"C:\temp\Image1.jpg") == false) //{ // MainClass.Image1.SaveFile(@"C:\temp\Image1.jpg"); //} //else //{ // System.IO.File.Delete(@"C:\temp\Image1.jpg"); // MainClass.Image1.SaveFile(@"C:\temp\Image1.jpg"); //} //if (System.IO.File.Exists(@"C:\temp\Image2.jpg") == false) //{ // MainClass.Image1.SaveFile(@"C:\temp\Image2.jpg"); //} //else //{ // System.IO.File.Delete(@"C:\temp\Image2.jpg"); // MainClass.Image1.SaveFile(@"C:\temp\Image2.jpg"); //}
// //PdfWriter.GetInstance(MainClass.myDocument, new FileStream(@"C:\temp\Images.PDF", FileMode.Create)); //PdfWriter.GetInstance(MainClass.myDocument, )
using (FileStream stream = new FileStream(@"C:\temp\Images.pdf", FileMode.OpenOrCreate)) { PdfWriter writer; PdfReader reader = new PdfReader(stream); PdfStamper stamper = new PdfStamper(reader, stream); if (System.IO.File.Exists(@"C:\temp\Images.pdf") == false) { writer = PdfWriter.GetInstance(MainClass.myDocument, stream); } else { writer = PdfWriter.GetInstance(MainClass.myDocument, stream); } MainClass.myDocument.Open();