本文介绍了条形码项目,不进行像素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有以下代码可以扫描条形码并解码它的值。 问题是arrayofbars []是空的并且没有任何价值 private static int readbarcode(位图图片){ string [] arrayofbars = new string [ 250 ]; for ( int x = 0 ; x < image.Width; x ++) { int i = 0 ; for ( int y = 0 ; y < image.Height; y ++) { Color pixel = image.GetPixel(x,y); // 获取每种黑色并将其放入arrayofbars [] if (pixel == Color.Black){ arrayofbars [i] = b;我++; } 其他 for ( int j = i-1; j> 0; j - ){ // 如果找到一个黑色像素 // 我们可能有一个栏放白色arrayofbars中的像素[] if (arrayofbars [j] == b){ arrayofbars [i] = W;我++; break ; } if (arrayofbars.Length!= 0 ){ // 如果它们是邻居,则将相等的像素连接成一个 int n = arrayofbars.Length; for ( int k = 0 ; k< n; k ++){ if (arrayofbars [k] == arrayofbars [k + 1]){ arrayofbars [ K] = arrayofbars [K] + arrayofbars [K + 1]; for ( int j = k + 1; j< n-1; j ++){ arrayofbars [j] = arrayofbars [j + 1]; } n--;} } // 找到最大元素(即吧) int [] length = new int [ 250 ]; for ( int b = 0 ; b< n; b ++){ length [b] = arrayofbars [b] .Length; } int min = maks.Min(); char [] bars = new char [ 100 ]; // 每个唯一区域找出它代表的条数 for (i = 0 ; i< n; i ++){ int p =(arrayofbars [i] .Length)/(min); switch (p){ case 1 : bars [i] = arrayofbars [i] .Last();我++; break ; case 2 : {bars [i] = arrayofbars [i]。持续(); bars [i + 1] = arrayofbars [i] .Last(); i + = 2 ; break ; } case 3 :{ bars [i] = arrayofbars [I]。去年(); bars [i + 1] = arrayofbars [i] .Last(); bars [i + 2] = arrayofbars [i] .Last(); i + = 3 ; break ; } } } if (bars [ 0 ] == ' b'&& bars [ 1 ] == ' w'& ;& bars [ 2 ] == ' b' ){ // 如果我们有条形码(以101开头) for (i = 3 ; i< 95; i + = 7 ){ string digit = ; for ( int j = i; j< i + 7; j ++) digit = bars [i] + digit; if (i%2 == 0 ){ switch (digit){ case bwbbwww: return 0 ; case bwwbbww : return 1 ; case bbwwbww : return 2 ; case bwbbbbw : return 3 ; case bbwwwbw : return 4 ; case bwwwbbw : return 5 ; case bbbbwbw : return 6 ; case bbwbbbw : return 7 ; case bbbwbbw : return 8 ; case bbwbwww : return 9 ; }} 其他 { 开关(数字){ case bbbwwbw: return 0 ; case bbwwbbw : return 1 ; case bbwbbww : return 2 ; case bwwwwbw : return 3 ; case bwbbbww : return 4 ; case bwwbbbw : return 5 ; case bwbwwww : return 6 ; case bwwwbww : return 7 ; case bwwbwww : return 8 ; case bbbwbww : return 9 ; } } } } } } return 0 ; } 解决方案 这里有很多错误的代码... if (arrayofbars.Length!= 0 ) 当然它从不0,你只需将它设为250!那你真的想做什么?! 如果(pixel == Color.Black) { arrayofbars [i] = b; i ++; } else for ( int j = i - 1 ; j > 0 ; j--) { // 如果找到一个黑色像素 // 我们可能有一个条形码arrayofbars中的白色像素[] if (arrayofbars [j] == b) { arrayofbars [i] = W; i ++; break ; } } 如果第一个像素不是黑色,你会遇到一个非常有趣的循环 for ( int j = -1; j > 0 ; j--) 你最好开始阅读主题... 从图像中读取条形码 [ ^ ] 从图像中读取条形码 - II [ ^ ] 从图像中读取条形码 - II我 [ ^ ] 以及Google中的许多其他人...... I have the following code that's supposed to scan a bar code and decode it's value. The problem is that arrayofbars[] is empty and doesn't get any value private static int readbarcode(Bitmap image){ string [] arrayofbars=new string[250]; for (int x = 0; x < image.Width; x++) { int i=0; for (int y = 0; y < image.Height; y++) { Color pixel = image.GetPixel(x, y);//get every black color and put it in an arrayofbars[] if(pixel==Color.Black){ arrayofbars[i]="b"; i++; } else for(int j=i-1;j>0;j--){//if a black pixel has been found//than we probably have a bar put the white pixel in arrayofbars[] if(arrayofbars[j]=="b"){ arrayofbars[i]="w"; i++; break; } }} if(arrayofbars.Length!=0){//concatenate equal pixel's into one if they are neighbors int n=arrayofbars.Length; for(int k=0;k<n;k++){ if(arrayofbars[k]==arrayofbars[k+1]){ arrayofbars[k]=arrayofbars[k]+arrayofbars[k+1]; for(int j= k+1;j<n-1;j++){ arrayofbars[j]=arrayofbars[j+1]; } n--;} } // find the largest element(i.e bar)int[] length=new int[250]; for(int b=0;b<n;b++){ length[b]=arrayofbars[b].Length; } int min= maks.Min(); char [] bars= new char[100] ;//for every unique area find out how many bars it represents for(i=0;i<n;i++){ int p=(arrayofbars[i].Length)/(min); switch (p){ case 1: bars[i]=arrayofbars[i].Last(); i++; break; case 2: {bars[i]=arrayofbars[i].Last(); bars[i+1]=arrayofbars[i].Last(); i+=2 ;break; } case 3:{ bars[i]=arrayofbars[i].Last(); bars[i+1]=arrayofbars[i].Last(); bars[i+2]=arrayofbars[i].Last(); i+=3;break; } } } if(bars[0]=='b'&&bars[1]=='w'&&bars[2]=='b'){// if we have a bar code (it starts as 101) for(i=3;i<95;i+=7){ string digit=""; for(int j=i;j<i+7;j++) digit=bars[i]+digit; if(i%2==0){ switch (digit){ case "bwbbwww": return 0; case "bwwbbww": return 1; case "bbwwbww": return 2; case "bwbbbbw": return 3; case "bbwwwbw": return 4; case "bwwwbbw": return 5; case "bbbbwbw": return 6; case "bbwbbbw": return 7; case "bbbwbbw": return 8; case "bbwbwww": return 9; } } else{ switch (digit){ case "bbbwwbw" : return 0; case "bbwwbbw" : return 1; case "bbwbbww" : return 2; case "bwwwwbw" : return 3; case "bwbbbww" : return 4; case "bwwbbbw" : return 5; case "bwbwwww" : return 6; case"bwwwbww" : return 7; case"bwwbwww" : return 8; case"bbbwbww" : return 9; } } } } } } return 0; } 解决方案 You have a lot of wrong code here...if ( arrayofbars.Length != 0 )Of course it never 0, you just set it to 250!!! So what really you want to do?!if ( pixel == Color.Black ){ arrayofbars[ i ] = "b"; i++;}else for ( int j = i - 1; j > 0; j-- ) { //if a black pixel has been found //than we probably have a bar put the white pixel in arrayofbars[] if ( arrayofbars[ j ] == "b" ) { arrayofbars[ i ] = "w"; i++; break; } }If the very first pixel is not black you run into a very interesting loopfor ( int j = -1; j > 0; j-- )You better start reading about the subject...Reading Barcodes from an Image[^]Reading Barcodes from an Image - II[^]Reading Barcodes from an Image - III[^]And many others in Google... 这篇关于条形码项目,不进行像素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 05:02