public override void ExecuteTask(IClientTask task, IBatchContext batchContext) { URL = task.BatchNode.StepData.StepConfiguration.ReadString("BlazonURL"); DIR = task.BatchNode.StepData.StepConfiguration.ReadString("BlazonTempDIR"); WaitTime = task.BatchNode.StepData.StepConfiguration.ReadInt("WaitTime", 5); CleanUp = task.BatchNode.StepData.StepConfiguration.ReadBoolean("CleanUp", false); CurrentTask = task; try { //First read the file from the Batch if (task.BatchNode.RootLevel != 0) { //throw new System.ArgumentException("Module should run at Page Level"); IBatchNodeCollection pages = task.BatchNode.GetDescendantNodes(0); int numpages = pages.Count(); int i = 0; while (i < numpages) { IBatchNode page = pages[i]; SendFileToBlazon(page); i++; } task.CompleteTask(); } else { SendFileToBlazon(task.BatchNode); task.CompleteTask(); } } catch (Exception e) { task.FailTask(FailTaskReasonCode.GenericRecoverableError, e); } }
public void CompressNode(IBatchNode node, string TempPath, double Compression) { //Get the image Stream InputImage = new MemoryStream(); InputImage = node.NodeData.ValueSet.ReadFile("InputImage").ReadData(); Bitmap fromStream = new Bitmap(InputImage); //Convert this to a PixImage PixImage Image = new PixImage(); Image.Import(fromStream); //Now we have a bitmap we need to save it to the temporary file location //Get the file path string fPath = TempPath; //Get a node ID fPath = fPath + @"\" + node.NodeData.BatchId + "_" + node.NodeData.NodeId.ToString() + ".jpg"; //Convert the compression to an int int comp = Convert.ToInt32(Compression); PixImageStorage.Save(Image, PixFileType.Jpeg, fPath, new PixJpegCompressionSettings(ColorFormat.Rgb, PixCompression.ProgressiveJpeg, comp), OpenFileMode.CreateAlways); //Now we want to try and read that file and save it back try { byte[] f = File.ReadAllBytes(fPath); node.NodeData.ValueSet.WriteFileData("OutputFile", f, ".jpg"); //Now delete the files File.Delete(fPath); } catch (IOException e) { MessageBox.Show(e.Message); } }
public override void ExecuteTask(IClientTask task, IBatchContext batchContext) { //First Get the name of the current Step String StepName = task.BatchNode.StepData.StepName; //Now get the inputfiles for each page if (task.BatchNode.RootLevel > 0) { foreach (IBatchNode p in task.BatchNode.GetDescendantNodes(0)) { ReadPageXML(p); } } else { IBatchNode p = task.BatchNode; ReadPageXML(p); } //Now end task.CompleteTask(); }
private void ConvertImage(IBatchNode p) { //Get the Data from the file Stream s = new MemoryStream(); s = p.NodeData.ValueSet.ReadFile("InputImage").ReadData(); //Get Image data from the stream Image i = Image.FromStream(s); //Now write the data back to a stream Stream ns = new MemoryStream(); i.Save(ns, System.Drawing.Imaging.ImageFormat.Jpeg); //Now save it back as a Stage File byte[] fileData; ns.Position = 0; using (var streamReader = new MemoryStream()) { ns.CopyTo(streamReader); fileData = streamReader.ToArray(); } p.NodeData.ValueSet.WriteFileData("OutputImage", fileData, "Jpeg"); }
public void ReadPageXML(IBatchNode p) { //Get the XML String string pageXML = p.NodeData.ValueSet.ReadString("PageXML"); if (pageXML != "") { //Convert it to an XML Document XmlDocument XMLDoc = new XmlDocument(); XMLDoc.LoadXml(pageXML); XmlNodeList Fields = XMLDoc.GetElementsByTagName("Index"); if (Fields.Count == 1) { //We're only going to do the 1 field right now string X1 = Fields[0].Attributes.GetNamedItem("X1").Value; string X2 = Fields[0].Attributes.GetNamedItem("X2").Value; string Y1 = Fields[0].Attributes.GetNamedItem("Y1").Value; string Y2 = Fields[0].Attributes.GetNamedItem("Y2").Value; //Save the Values back string Coordinates = X1 + "|" + X2 + "|" + Y1 + "|" + Y2; p.NodeData.ValueSet.WriteString("Coordinates", Coordinates); } } }
public void SendFileToBlazon(IBatchNode taskFile) { CurrentFile = taskFile; //Try to see if there's an inputfile type string FileType = ""; FileType = taskFile.NodeData.ValueSet.ReadString("InputFileType"); if (FileType == "") { throw new System.Exception("InputFileType not found"); } //Now Set the File Name string TempFile = DIR + "/" + taskFile.NodeData.BatchId + "_" + taskFile.NodeData.NodeId + FileType; //Now read the data to a file taskFile.NodeData.ValueSet.ReadFile("InputFile").ReadToFile(TempFile); //Now call the Blazon service string BlazonCommand = URL + "/QueueServer/push.aspx?source=" + TempFile + "&target=" + DIR + "&outputformat=tif"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(BlazonCommand); request.GetResponse(); //Now wait untill the file is in the output directory string newfile = DIR + "/" + taskFile.NodeData.BatchId + "_" + taskFile.NodeData.NodeId + ".tif"; DateTime StartTime = DateTime.Now; while (!File.Exists(newfile)) { int HowLong = DateTime.Now.Subtract(StartTime).Seconds; if (HowLong < WaitTime) { Thread.Sleep(100); } else { //This is taking too long throw new System.TimeoutException("The Blazon Service took too long so I gave up"); } } bool canopen = false; while (!canopen == true) { //Check to see hoe long we've waited int HowLong = DateTime.Now.Subtract(StartTime).Seconds; if (HowLong > WaitTime) { //This is taking too long throw new System.TimeoutException("The Blazon Service took too long so I gave up"); } else { try { using (File.Open(newfile, FileMode.Open)) { canopen = true; } } catch (IOException e) { Thread.Sleep(100); e.Data.Clear(); } } } byte[] f = File.ReadAllBytes(newfile); CurrentFile.NodeData.ValueSet.WriteFileData("OutputFile", f, ".tif"); CurrentFile.NodeData.ValueSet.WriteString("OutputFileType", "tif"); //Now delete the files if (CleanUp == true) { File.Delete(TempFile); File.Delete(newfile); } }