Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用网络摄像头读取条形码_C#_Webcam_Barcode - Fatal编程技术网

C# 使用网络摄像头读取条形码

C# 使用网络摄像头读取条形码,c#,webcam,barcode,C#,Webcam,Barcode,嘿, 我正在尝试从我的网络摄像头读取EAN-13条形码。 我已经写了一个类来做这项工作。 我正在从我的网络摄像头中拍摄一张照片,将其剪裁为仅显示条形码, 并使用维基百科的代码表读取条形码。 由于某些原因,条形码会被修剪,但输出总是“0-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1”。 我想知道我是否犯了愚蠢的错误或误解了什么 我不想使用任何第三方程序 这是我目前的代码: public class BarcodeDecoder { static string[] li

嘿,
我正在尝试从我的网络摄像头读取EAN-13条形码。
我已经写了一个类来做这项工作。
我正在从我的网络摄像头中拍摄一张照片,将其剪裁为仅显示条形码,
并使用维基百科的代码表读取条形码。
由于某些原因,条形码会被修剪,但输出总是“0-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1”。
我想知道我是否犯了愚蠢的错误或误解了什么

我不想使用任何第三方程序

这是我目前的代码:

    public class BarcodeDecoder
{
    static string[] ligerade = new string[] { "0100111", "0110011", "0011011", "0100001", "0011101", "0000101", "0010001", "0001001", "0010111" };
    static string[] rechtsgerade = new string[ligerade.Length];
    static string[] liungerade = new string[ligerade.Length];
    static string[] GeradeUG = new string[] { "UUUUUU", "UUGUGG", "UUGGUG", "UUGGGU", "UGUUGG", "UGGUUG", "UGGGUU", "UGUGUG", "UGUGGU", "UGGUGU" };
    static int[] links;
    static int[] rechts;
    static string result;

    public static string Decode(Bitmap b)
    {
        result = "";
        Bitmap bb = CutOutOf(b, b.Height / 2);
        bb = trimBitmap(bb);
        int[] lgs = GetNumberOutOf(bb);
        int[][] rr = trimArray(lgs);
        links = rr[0];
        rechts = rr[1];
        FillArrays();
        BearbeiteLinks();
        BearbeiteRechts();
        return result;
    }
    static void BearbeiteLinks()
    {
        string GU = "";

        string[] zahlen = new string[6];
        zahlen[0] = OutOfArray(links, 0, 7);
        zahlen[1] = OutOfArray(links, 7, 7);
        zahlen[2] = OutOfArray(links, 14, 7);
        zahlen[3] = OutOfArray(links, 21, 7);
        zahlen[4] = OutOfArray(links, 28, 7);
        zahlen[5] = OutOfArray(links, 35, 7);

        foreach (string pq in zahlen)
        {
            bool gerade = ligerade.ToList().IndexOf(pq) > -1;
            if (gerade)
            {
                result += ligerade.ToList().IndexOf(pq).ToString();
                GU += "G";
            }
            else
            {
                result += liungerade.ToList().IndexOf(pq).ToString();
                GU += "U";
            }
        }
        result = GeradeUG.ToList().IndexOf(GU).ToString() + result;
    }
    static void BearbeiteRechts()
    {
        string[] zahlen = new string[6];
        zahlen[0] = OutOfArray(rechts, 0, 7);
        zahlen[1] = OutOfArray(rechts, 7, 7);
        zahlen[2] = OutOfArray(rechts, 14, 7);
        zahlen[3] = OutOfArray(rechts, 21, 7);
        zahlen[4] = OutOfArray(rechts, 28, 7);
        zahlen[5] = OutOfArray(rechts, 35, 7);

        foreach (string pq in zahlen)
        {
            result += rechtsgerade.ToList().IndexOf(pq).ToString();
        }
    }
    static string OutOfArray(int[] ar, int startindex, int length)
    {
        int[] gar = new int[length];
        Array.Copy(ar, startindex, gar, 0, length);
        StringBuilder bilder = new StringBuilder();
        for (int i = 0; i < gar.Length; i++)
        {
            bilder.Append(gar[i].ToString());
        }
        return bilder.ToString();
    }
    static Bitmap trimBitmap(Bitmap b)
    {
        bool alreadyBlack = false;
        int firstblack = 0;

        for (int i = 0; i < b.Width; i++)
        {
            Color gp = b.GetPixel(i, 0);
            if ((gp.R + gp.G + gp.B) / 3 < 128)
            {
                if (!alreadyBlack)
                {
                    alreadyBlack = true;
                    firstblack = i;
                }
            }
        }

        bool alreadyblack = false;
        int lastblack = 0;
        for (int i = b.Width -1; i > 0; i--)
        {
            Color gpp = b.GetPixel(i, 0);
            if ((gpp.R + gpp.G + gpp.B) / 3 < 128)
            {
                if (!alreadyblack)
                {
                    alreadyblack = true;
                    lastblack = i;
                }
            }
        }
        Bitmap result = new Bitmap(lastblack - firstblack, 1);
        for (int i = firstblack; i < lastblack; i++)
        {
            Color c = b.GetPixel(i, 0);
            result.SetPixel(i - firstblack, 0, c);
        }
        result.Save("C:\\result.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
        return result;
    }
    static int[][] trimArray(int[] ar)
    {
        int[][] res = new int[2][];
        int[] resl = new int[6 * 7];
        int[] resr = new int[6 * 7];
        Array.Copy(ar, 2, resl, 0, 6 * 7);
        Array.Copy(ar, 2 + 6 * 7 + 5, resr, 0, 6 * 7);
        res[0] = resl;
        res[1] = resr;
        return res;
    }
    static void FillArrays()
    {
        for (int i = 0; i < ligerade.Length; i++)
        {
            rechtsgerade[i] = string.Concat(ligerade[i].Reverse());
        }
        for (int x = 0; x < liungerade.Length; x++)
        {
            liungerade[x] = Invert(rechtsgerade[x]);
        }
    }
    static string Invert(string xx)
    {
        string xs = "";
        for (int y = 0; y < xx.Length; y++)
        {
            int fd = int.Parse(xx[y].ToString());
            if (fd == 0)
                fd = 1;
            else
                fd = 0;
            xs += fd.ToString();
        }
        return xs;
    }
    static Bitmap CutOutOf(Bitmap b, int y)
    {
        Bitmap res = new Bitmap(b.Width, 1);

        for (int i = 0; i < b.Width; i++)
        {
            Color c = b.GetPixel(i, y);
            res.SetPixel(i, 0, c);
        }
        return res;
    }
    static int[] GetNumberOutOf(Bitmap bb)
    {
        List<int> intlst = new List<int>();
        float f = (float)bb.Width / 95.0f;
        float wd = f / 2.0f;
        for (float i = wd; i < bb.Width; i+=f)
        {
            Color c = bb.GetPixel((int)Math.Round(i,0), 0);
            intlst.Add(GetOutOfColor(c));
        }
        return intlst.ToArray();
    }
    static int GetOutOfColor(Color c)
    {
        if (c.A + c.B + c.R > 128 * 3)
        {
            return 0;
        }
        return 1;
    }
}
公共类条码解码器
{
静态字符串[]ligerade=新字符串[]{“0100111”、“01100111”、“0011011”、“0100001”、“0011101”、“0000101”、“0010001”、“0001001”、“0010001”、“0001001”、“0010111”};
静态字符串[]rechtsgerade=新字符串[ligerade.Length];
静态字符串[]liungerade=新字符串[ligerade.Length];
静态字符串[]GeradeUG=新字符串[]{“UUUUUU”、“uugugugg”、“UUGGGU”、“UUGGGU”、“uguggug”、“UGGUUG”、“ugugugugu”、“uguguggu”、“UGGUGU”};
静态int[]链接;
静态int[]rechts;
静态字符串结果;
公共静态字符串解码(位图b)
{
结果=”;
位图bb=断路器(b,b.高度/2);
bb=修剪位图(bb);
int[]lgs=getnumberroutof(bb);
int[][]rr=trimArray(lgs);
links=rr[0];
rechts=rr[1];
填充数组();
BearbeiteLinks();
BearbeiteRechts();
返回结果;
}
静态无效BearbeiteLinks()
{
字符串GU=“”;
字符串[]zahlen=新字符串[6];
zahlen[0]=OutOfArray(links,0,7);
zahlen[1]=OutOfArray(links,7,7);
zahlen[2]=OutOfArray(links,14,7);
zahlen[3]=OutOfArray(links,21,7);
zahlen[4]=OutOfArray(links,28,7);
zahlen[5]=OutOfArray(links,35,7);
foreach(扎伦中的字符串pq)
{
bool-gerade=ligerade.ToList().IndexOf(pq)>-1;
伊夫(杰拉德)
{
结果+=ligerade.ToList().IndexOf(pq.ToString();
GU+=“G”;
}
其他的
{
结果+=liungerade.ToList().IndexOf(pq.ToString();
GU+=“U”;
}
}
结果=GeradeUG.ToList().IndexOf(GU.ToString()+结果;
}
静态空隙率beaterechts()
{
字符串[]zahlen=新字符串[6];
zahlen[0]=OutOfArray(rechts,0,7);
zahlen[1]=OutOfArray(rechts,7,7);
zahlen[2]=脱离阵列(rechts,14,7);
zahlen[3]=脱离阵列(rechts,21,7);
zahlen[4]=脱离阵列(rechts,28,7);
zahlen[5]=脱离阵列(rechts,35,7);
foreach(扎伦中的字符串pq)
{
结果+=rechtsgerade.ToList().IndexOf(pq.ToString();
}
}
静态字符串超出数组(int[]ar,int startindex,int length)
{
int[]gar=新的int[长度];
数组.Copy(ar,startindex,gar,0,长度);
StringBuilder bilder=新的StringBuilder();
for(int i=0;i0;i--)
{
颜色gpp=b.GetPixel(i,0);
如果((gpp.R+gpp.G+gpp.B)/3<128)
{
如果(!alreadyblack)
{
alreadyblack=true;
lastblack=i;
}
}
}
位图结果=新位图(lastblack-firstblack,1);
for(int i=firstblack;i128*3)
Color c = bb.GetPixel((int)Math.Round(i,0), 0);
if (c.A + c.B + c.R > 128 * 3)