• 如何简单扫描整理


    背景

    2.开发的原因是因为需要扫描A、B、C。。多种文档然后放入不同的文件夹并且修改文件名。有些打印机具有编程功能,可以设定文件夹名称,但是还是需要人工修改文件名,扫描过程中还必须停下来。

    3.想过很多ORC的方式来解决如何区分ABC文件的办法,但是速度慢并且效果差。

    4.最后解决办法很简单,扫完A之后扫描一张纯色的图片,然后随机几个点判断是否是这个颜色,如果是则确定A文档已扫描完成,然后扫描B,同理。

    5.在用高拍仪、摄像头的时候会因为现场光源影响图片的扫描结果,所以需要设定一个颜色区间。

    6.效果奇好,识别达到100%

    功能

    1.把要扫描的文档扫描到一个文件夹里面

    2.设定图片关联的数据,用程序进行处理

    3.输出结果

    关键代码

    1.动作锦囊

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Text.RegularExpressions;
    using System.IO;
    using REP = DAL.ImgScan.Repository;
    using FrameWork;
    using System.IO;
    using System.ComponentModel;
    
    namespace DAL.ImgScan
    {
        public class ActionModual
        {
            public static void ExcuteAction(REP.RowRecord rowRec, List<REP.Action> acRecs, REP.GeneralSetting genRec, REP.AdvanceSetting advRec, string rootPath)
            {
                foreach (var acRec in acRecs)
                {
                    string folderPath = ActionModual.GetFolderPath(rowRec, rootPath + "\" + acRec.SavePath, advRec);
                    string partOfSavePath = folderPath + "\" + Common.GetParamString(rowRec, acRec.JoinName);
                    if (acRec.isFixed)
                    {
                        for (int i = 0; i < int.Parse(acRec.Amount); i++)
                        {
                            CopyFile(rowRec.imgFiles[i], folderPath, partOfSavePath + "$" + (i + 1).ToString() + ".jpg");
                        }
                        rowRec.imgFiles.RemoveRange(0, int.Parse(acRec.Amount));
                    }
                    else
                    {
                        for (int i = 0; i < rowRec.imgFiles.Count; i++)
                        {
                            CopyFile(rowRec.imgFiles[i], folderPath, partOfSavePath + "$" + (i + 1).ToString() + ".jpg");
                        }
                    }
                }
            }
    
            public static List<REP.ParamInfo> CheckFileExist(List<REP.RowRecord> rowRecs, List<REP.Action> acRecs, REP.AdvanceSetting advRec)
            {
                int imgNm = 0;
                List<REP.ParamInfo> paramInfoes = new List<Repository.ParamInfo>();
                foreach (var acRec in acRecs)
                {
                    imgNm += int.Parse(acRec.Amount);
                }
                foreach (var rowRec in rowRecs)
                {
                    if (imgNm > rowRec.imgFiles.Count)
                    {
                        paramInfoes.Add(new Repository.ParamInfo("图片数量小于动作设定所需图片数量", rowRec.value[advRec.oid]));
                    }
                }
                return paramInfoes;
            }
            public static string GetFolderPath(REP.RowRecord rowRec, string rootPath, REP.AdvanceSetting advRec)
            {
                if (advRec.isChildFolder)
                {
                    string childFolderPath = Common.GetParamString(rowRec, advRec.ChildFieldName);
                    return rootPath + "\" + childFolderPath;
                }
                else
                {
                    return rootPath;
                }
            }
    
            private static void CopyFile(string sourceFileName, string folderPath, string saveFullName)
            {
                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }
                if (!File.Exists(saveFullName))
                {
                    File.Copy(sourceFileName, saveFullName);
                }
            }
        }
    }
    View Code

    2.图像识别

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    using System.Drawing.Imaging;
    using REP = DAL.ImgScan.Repository;
    namespace DAL.ImgScan
    {
        public class PhotoIdentify
        {
            public static bool CheckImg(string path)
            {
                using (Bitmap img = new Bitmap(path))
                {
                    int iRes = 0;
                    int iY = img.Height / 3;
                    int iX = img.Width / 8;
                    for (int i = 1; i < 5; i++)
                    {
                        if (i == 2)//余下2个到纸的另一边取
                        {
                            iY = iY * 2;
                        }
                        Color pixel = img.GetPixel(iX * i, iY + i * 2);//
                        if (isTagImg(pixel))
                        {
                            iRes++;
                        }
                    }
                    if (iRes > 2)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            public static List<REP.ParamInfo> ColorPicker(string folderPath)
            {
                List<REP.ParamInfo> paramInfoes = new List<REP.ParamInfo>();
                string[] files = System.IO.Directory.GetFiles(folderPath, "*.jpg");
                for (int j = 0; j < files.Length; j++)
                {
    
                    using (Bitmap img = new Bitmap(files[j]))
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            Color pixel = img.GetPixel(50 + i * 50, 200 + i * 2);
                            paramInfoes.Add(new REP.ParamInfo(System.IO.Path.GetFileName(files[j]), "r:" + pixel.R + ";g:" + pixel.G + ";b:" + pixel.B));
                        }
                    }
                }
                return paramInfoes;
            }
            private static bool isTagImg(Color pixColor)
            {
                if (pixColor.G - pixColor.R > 20 && pixColor.G > pixColor.B)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
    View Code

    3.执行函数

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Threading;
    using System.Diagnostics;
    using System.Data;
    using REP = DAL.ImgScan.Repository;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    
    namespace DAL.ImgScan.Service
    {
        public class Facade
        {
            public static List<REP.ParamInfo> Run(string[] pathes, BackgroundWorker bWork)
            {
                REP.GeneralSetting generalRec = XMLModual.ReadGeneralConfig(pathes[1]);
                List<REP.ParamInfo> errLogs = new List<REP.ParamInfo>();
                switch (generalRec.CatalogType)
                {
                    case "隔页图":
                        {
                            errLogs = CatalogByImg(pathes, bWork);
                            break;
                        }
                    case "文件夹":
                        {
                            errLogs = CatalogByFolder(pathes, bWork);
                            break;
                        }
                }
    
                #region 完成
                return errLogs;
                #endregion
            }
    
            private static List<REP.ParamInfo> CatalogByImg(string[] pathes, BackgroundWorker bWork)
            {
                List<REP.ParamInfo> errLogs = new List<REP.ParamInfo>();
                int iCurProc = 10;
    
                #region 解析XML
                bWork.ReportProgress(iCurProc, "读取配置");
                List<REP.Action> acRecs = XMLModual.ReadActionConfig(pathes[1]);
                REP.GeneralSetting generalRec = XMLModual.ReadGeneralConfig(pathes[1]);
                REP.AdvanceSetting advRec = XMLModual.ReadAdvanceConfig(pathes[1]);
                //动作排个序 
                acRecs.Sort(new REP.ActionComparer());
                #endregion
    
                #region 读取数据
                bWork.ReportProgress(iCurProc, "解析对照表");
                List<REP.RowRecord> rowRecs = null;
                switch (generalRec.MappingTableType)
                {
                    case "MDB":
                        {
                            REP.DBRep dbModual = new REP.DBRep(pathes[2]);
                            rowRecs = dbModual.GetFullRecords(generalRec);
                            break;
                        }
                    case "EXCEL":
                        {
                            rowRecs = REP.NPOIRep.GetFullRecs(pathes[2], generalRec);
                            break;
                        }
                    default:
                        rowRecs = new List<REP.RowRecord>();
                        break;
                }
                #endregion
    
                #region 图片准备
                bWork.ReportProgress(iCurProc, "读取图片");
                REP.NPOIRep.GetImgListByImg(ref rowRecs, pathes[0], advRec, bWork);//解析图片
                bWork.ReportProgress(iCurProc, "保存图片列表缓存");
                Common.ExportLog(rowRecs, advRec);
                bWork.ReportProgress(iCurProc, "检查图片");
                errLogs = ActionModual.CheckFileExist(rowRecs, acRecs, advRec);
                if (errLogs.Count != 0)
                {
                    return errLogs;
                }
                #endregion
    
                #region 执行操作
                foreach (var rowRec in rowRecs)
                {
                    bWork.ReportProgress(iCurProc, "处理:" + rowRec.value[advRec.oid]);
                    if (rowRec.imgFiles.Count == 0)
                    {
                        errLogs.Add(new REP.ParamInfo("没有图片", rowRec.value[advRec.oid]));
                    }
                    try
                    {
                        ActionModual.ExcuteAction(rowRec, acRecs, generalRec, advRec, pathes[3]);
                    }
                    catch (Exception ex)
                    {
                        errLogs.Add(new REP.ParamInfo(ex.Message, rowRec.value[advRec.oid]));
                    }
    
                }
                #endregion
    
                #region 完成
                bWork.ReportProgress(iCurProc, "完成");
                return errLogs;
                #endregion
            }
    
            private static List<REP.ParamInfo> CatalogByFolder(string[] pathes, BackgroundWorker bWork)
            {
                List<REP.ParamInfo> errLogs = new List<REP.ParamInfo>();
                int iCurProc = 10; 
                #region 解析XML
                bWork.ReportProgress(iCurProc, "读取配置");
                List<REP.Action> acRecs = XMLModual.ReadActionConfig(pathes[1]);
                REP.GeneralSetting generalRec = XMLModual.ReadGeneralConfig(pathes[1]);
                REP.AdvanceSetting advRec = XMLModual.ReadAdvanceConfig(pathes[1]);
                //动作排个序 
                acRecs.Sort(new REP.ActionComparer());
                #endregion
    
                #region 读取数据
                bWork.ReportProgress(iCurProc, "解析对照表");
                List<REP.RowRecord> rowRecs = null;
                switch (generalRec.MappingTableType)
                {
                    case "MDB":
                        {
                            REP.DBRep dbModual = new REP.DBRep(pathes[2]);
                            rowRecs = dbModual.GetFullRecords(generalRec);
                            break;
                        }
                    case "EXCEL":
                        {
                            rowRecs = REP.NPOIRep.GetFullRecs(pathes[2], generalRec);
                            break;
                        }
                    default:
                        rowRecs = new List<REP.RowRecord>();
                        break;
                }
                #endregion
    
                #region 图片准备
                bWork.ReportProgress(iCurProc, "读取图片");
                try
                {
                    REP.NPOIRep.GetImgListByFolder(ref rowRecs, pathes[0], advRec, bWork);//解析图片
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                bWork.ReportProgress(iCurProc, "保存图片列表缓存");
                Common.ExportLog(rowRecs, advRec);
                errLogs = ActionModual.CheckFileExist(rowRecs, acRecs, advRec);
                if (errLogs.Count != 0)
                {
                    return errLogs;
                }
                #endregion
    
                #region 执行操作
                foreach (var rowRec in rowRecs)
                {
                    bWork.ReportProgress(iCurProc, "处理:" + rowRec.value[advRec.oid]);
                    if (rowRec.imgFiles.Count == 0)
                    {
                        errLogs.Add(new REP.ParamInfo("没有图片", rowRec.value[advRec.oid]));
                    }
                    try
                    {
                        ActionModual.ExcuteAction(rowRec, acRecs, generalRec, advRec, pathes[3]);
                    }
                    catch (Exception ex)
                    {
                        errLogs.Add(new REP.ParamInfo(ex.Message, rowRec.value[advRec.oid]));
                    }
    
                }
                #endregion
    
                #region 完成
                bWork.ReportProgress(iCurProc, "完成");
                return errLogs;
                #endregion
            }
        }
    }
    View Code
  • 相关阅读:
    Swift-自定义类的构造函数
    Swift-存储属性,计算属性,类属性
    iOS-UICollectionViewController协议及回调
    Swift-数组
    Swift-switch使用注意点
    Swift-setValuesForKeysWithDictionary
    解决IDEA Struts2 web.xml问题
    枚举类
    增强for循环 -- foreach循环
    静态导入
  • 原文地址:https://www.cnblogs.com/matoo/p/4763660.html
Copyright © 2020-2023  润新知