예제 #1
0
        /// <summary>
        ///     Do work
        /// </summary>
        private void ProcessImageWorker(object state)
        {
            ScannedImage scannedImage = null;
            Image        original     = null;

            try
            {
                lock (m_lockObject)
                {
                    using (var ms = new MemoryStream((byte[])state))
                    {
                        var img = Image.FromStream(ms);
                        img.RotateFlip(RotateFlipType.Rotate270FlipNone);
                        scannedImage = new ScannedImage(img);
                    }
                }

                scannedImage.Analyze();

                original =
                    new ResizeNearestNeighbor(scannedImage.Image.Width / 4, scannedImage.Image.Height / 4).Apply(
                        (Bitmap)scannedImage.Image);
            }
            catch
            {
                if (scannedImage != null)
                {
                    scannedImage.Dispose();
                }
                return; // Abort
            }

            try
            {
                // Add an Error entry
                if (!scannedImage.IsScannable)
                {
                    var lsv = new ListViewItem();
                    lsv.Tag = 2;
                    lsv.SubItems.Add(new ListViewItem.ListViewSubItem(lsv,
                                                                      "Scanned image doesn't appear to be a scannable form."));
                    lock (m_lockObject)
                    {
                        m_uiResults.Add(lsv, original);
                    }
                }
                else
                {
                    var engine       = new Engine();
                    var templateFile = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
                                                    scannedImage.TemplateName + ".mxml");
                    if (!File.Exists(templateFile))
                    {
                        var lsv = new ListViewItem();
                        lsv.Tag = 0;
                        lsv.SubItems.Add(new ListViewItem.ListViewSubItem(lsv,
                                                                          "Template file " + templateFile + " is missing"));
                        lock (m_lockObject)
                        {
                            m_uiResults.Add(lsv, original);
                        }
                        return;
                    }

                    // Apply template
                    var template = OmrTemplate.Load(templateFile);
                    var pageData = engine.ApplyTemplate(
                        template,
                        scannedImage);

                    // Draw the page data
                    ICanvas canvas = new DesignerCanvas();
                    canvas.Add(new OutputVisualizationStencil(pageData));
                    original.Dispose();
                    original = new Bitmap((int)template.BottomRight.X, (int)template.BottomLeft.Y,
                                          PixelFormat.Format24bppRgb);
                    using (var g = Graphics.FromImage(original))
                    {
                        float width  = template.TopRight.X - template.TopLeft.X,
                              height = template.BottomLeft.Y - template.TopLeft.Y;
                        g.DrawImage(scannedImage.Image, template.TopLeft.X, template.TopLeft.Y, width, height);
                        canvas.DrawTo(g);
                    }
                    // Save original analyzed image
                    try
                    {
                        var tPath = Path.GetTempFileName();
                        original.Save(tPath);
                        pageData.AnalyzedImage = tPath;
                    }
                    catch
                    {
                    }

                    var oldOriginal = original;
                    original =
                        new ResizeNearestNeighbor(scannedImage.Image.Width / 2, scannedImage.Image.Height / 2).Apply(
                            (Bitmap)original);
                    oldOriginal.Dispose();


                    lock (m_lockObject)
                    {
                        if (pageData.Outcome == OmrScanOutcome.Failure)
                        {
                            var lsv = new ListViewItem();
                            lsv.Tag = 0;
                            lsv.SubItems.Add(new ListViewItem.ListViewSubItem(lsv, pageData.ErrorMessage));
                            lock (m_lockObject)
                            {
                                m_uiResults.Add(lsv, original);
                            }
                        }
                        else
                        {
                            var validation = pageData.Validate(template);
                            var lsv        = new ListViewItem();
                            lsv.Tag = validation.IsValid ? 2 : 1;
                            if (!validation.IsValid)
                            {
                                lsv.SubItems.Add(validation.Issues[0]);
                            }
                            lsv.Tag = pageData;
                            lock (m_lockObject)
                            {
                                m_executionQueue.Enqueue(new KeyValuePair <OmrTemplate, OmrPageOutput>(template, pageData));
                            }
                            lock (m_lockObject)
                            {
                                m_uiResults.Add(lsv, original);
                            }
                        }
                        m_scannedPages.Pages.Add(pageData);
                    }
                }
            }
            catch (Exception e)
            {
                var lsv = new ListViewItem();
                lsv.Tag = 0;
                lsv.SubItems.Add(e.Message);
                lock (m_lockObject)
                {
                    m_uiResults.Add(lsv, original);
                }
            }
        }