6 Haziran 2017 Salı

c# barkod okuyucu yapımı

c# Barkod Okuyucu Yapımı


 


c# barkod okuyucu yapımı için makalemizi okumaya devam edebilirsiniz. Öncelikle bu konuda daha önce hazırlamış olduğumuz class yapısını sizinle paylaşıyorum


 


using System;

using System.Collections.Generic;

using System.Text;

using System.Drawing;


namespace BarcodeReader


public class BarcodeDecoder


private Dictionary<string, string> chars = new Dictionary<string, string>();

public BarcodeDecoder()


chars.Add(“100100001”, “1”);

chars.Add(“001100001”, “2”);

chars.Add(“101100000”, “3”);

chars.Add(“000110001”, “4”);

chars.Add(“100110000”, “5”);

chars.Add(“001110000”, “6”);

chars.Add(“000100101”, “7”);

chars.Add(“100100100”, “8”);

chars.Add(“001100100”, “9”);

chars.Add(“000110100”, “0”);

chars.Add(“100001001”, “A”);

chars.Add(“001001001”, “B”);

chars.Add(“101001000”, “C”);

chars.Add(“000011001”, “D”);

chars.Add(“100011000”, “E”);

chars.Add(“001011000”, “F”);

chars.Add(“000001101”, “G”);

chars.Add(“100001100”, “H”);

chars.Add(“001001100”, “I”);

chars.Add(“000011100”, “J”);

chars.Add(“100000011”, “K”);

chars.Add(“001000011”, “L”);

chars.Add(“101000010”, “M”);

chars.Add(“000010011”, “N”);

chars.Add(“100010010”, “O”);

chars.Add(“001010010”, “P”);

chars.Add(“000000111”, “Q”);

chars.Add(“100000110”, “R”);

chars.Add(“001000110”, “S”);

chars.Add(“000010110”, “T”);

chars.Add(“110000001”, “U”);

chars.Add(“011000001”, “V”);

chars.Add(“111000000”, “W”);

chars.Add(“010010001”, “X”);

chars.Add(“110010000”, “Y”);

chars.Add(“011010000”, “Z”);

chars.Add(“010000101”, “-“);

chars.Add(“110000100”, “.”);

chars.Add(“011000100”, “”);

chars.Add(“010010100”, “*”);

chars.Add(“010101000”, “$”);

chars.Add(“010100010”, “/”);

chars.Add(“010001010”, “+”);

chars.Add(“000101010”, “%”);


private Point FindFirstBlack(Bitmap b)


Point pFirstBlack = new Point();

for (int i = 0; i < b.Width; i++)


int firstYBlack = 0;

int lastYBlack = 0;

for (int j = 0; j < b.Height; j++)


Color c = b.GetPixel(i, j);

int average = (c.R + c.G + c.B) / 3;

if (average < 128)


if (firstYBlack == 0) firstYBlack = j;

lastYBlack = j;



if (firstYBlack != 0)


pFirstBlack.Y = (firstYBlack + lastYBlack) / 2;

pFirstBlack.X = i;

break;



return pFirstBlack;


public string DecodeCode39(Image img)


List<int> order = new List<int>();

Bitmap b = (Bitmap)img;

Point pFirstBlack = FindFirstBlack(b);


int blackIndex = pFirstBlack.X;

int whiteIndex = -1;

for (int i = pFirstBlack.X + 1; i < img.Width; i++)


Color c = b.GetPixel(i, pFirstBlack.Y + 1);

int average = (c.R + c.G + c.B) / 3;


if (average < 128)


if (whiteIndex > blackIndex)


order.Add(i – whiteIndex);

blackIndex = i;



else


if (blackIndex > whiteIndex)


order.Add(i – blackIndex);

whiteIndex = i;




order.Add(img.Width – blackIndex);


int TotalLength = 0;

foreach (int i in order)


TotalLength += i;


int avgLength = TotalLength / order.Count;

string text = “”;

for (int i = 0; i < order.Count; i = i + 9)


string s = “”;

s += (order[i] > avgLength ? “1” : “0”);

s += (order[i + 1] > avgLength ? “1” : “0”);

s += (order[i + 2] > avgLength ? “1” : “0”);

s += (order[i + 3] > avgLength ? “1” : “0”);

s += (order[i + 4] > avgLength ? “1” : “0”);

s += (order[i + 5] > avgLength ? “1” : “0”);

s += (order[i + 6] > avgLength ? “1” : “0”);

s += (order[i + 7] > avgLength ? “1” : “0”);

s += (order[i + 8] > avgLength ? “1” : “0”);

text += chars[s];

i++;


return text;



 


bu kodları class olarak projeye ekledikten sonra bir form dosyası oluşturalım.


c# barkod okuyucu yapımı için devam edelim.


formun loadına aşağıdaki yazıyı yazalım.


private void Form1_Load(object sender, EventArgs e)


BarcodeDecoder dec = new BarcodeDecoder();

Image img1 = Image.FromFile(“a.jpg”);

Image img2 = Image.FromFile(“b.jpg”);

pictureBox1.Image = img1;

pictureBox2.Image = img2;

label1.Text = dec.DecodeCode39(img1);

label2.Text = dec.DecodeCode39(img2);


 


c# barkod okuyucu yapımı bu kadar.Takıldığınız yerde soru sormak için aşağıdaki yorum bölümünü kullanabilirsiniz.


Örnek proje indirmek için : Burdan indirin


 



c# barkod okuyucu yapımı

Hiç yorum yok:

Yorum Gönder