2012-06-11 54 views
4

這是很容易使用Font()有毛病我的條碼(代碼128)

Font f = new Font("Free 3 of 9", 80); 
this.Font = f; 

Label l = new Label(); 
l.Text = "*STACKOVERFLOW*"; 
l.Size = new System.Drawing.Size(800, 600); 
this.Controls.Add(l); 

this.Size = new Size(800, 600); 

其工作,以產生3 of 9條形碼。我看到了條形碼,我能夠掃描它。現在我想用別的東西,像Code 128爲此我需要安裝的字體(做),只是改變

Font f = new Font("Free 3 of 9", 80);Font f = new Font("Code 128", 80);

之後,我看到我的窗口上的條形碼。問題是Im無法掃描。我認爲這是因爲我不使用合適的開始停止條碼標籤。據我所知,必須始終有一個開始/停止字符或任何。對於9的3中的3,*代碼128 im不確定。在維基有啓動代碼A所以我試圖

Font f = new Font("<Start Code A>test<Stop>", 80);Font f = new Font("<Start Code A>test<Stop Code A>", 80);等等......我不是能夠掃描輸出。由於掃描儀無法找到啓動和停止字符。有任何想法嗎?謝謝

+0

我看到的問題與您的條形碼,請編輯在我的答案的底部。 – Geronimo

回答

3

代碼128可以用一種完全好的字體來表示。這比9的3更棘手,因爲你必須在最後包含一個校驗和字符,這個字符需要根據條形碼的內容進行動態計算。還有3個不同的版本,每個版本都有不同的起始字符。

換句話說條形碼需要進行佈局,如:

[start char][barcode][checksum][stop char] 

CODE128的好處是,它比9

This page 3簡潔得多幫我制定出算法計算我的校驗和。

A中的算法很一般的概述是:

  1. 條形碼的每個角色獲得分配給它 取決於什麼字符,它位於 條形碼,其中一個特定的值。

  2. 上面1)中的所有值都加在一起。

  3. 獲得上述2)中總模數103的值。

  4. 在大多數情況下,校驗和char將是以上3)中確定的:(模數值 加32)的ASCII碼。

有一些細微差別,我最終需要創建這個算法在JavaScript中的所有東西(沒有問題)。對於我自己的未來參考,並表現出一定的細微差別,這是是什麼樣子:

/* 
* This is the variable part of my barcode, I append this to a 
* static prefix later, but I need to perform logic to compute the 
* checksum for this variable. There is logic earlier that enforces 
* this variable as a 9 character string containing only digits. 
*/ 
var formIncrement = // a 9 char "digit" string variable 

/* 
* Dynamically compute the total checksum value (before modulus) 
* for the variable part of my barcode, I will need to get a modulus 
* from this total when I am done. If you need a variable number of 
* characters in your barcodes or if they are not all digits 
* obviously something different would have to be done here. 
*/ 
var incrementCS = ((parseInt(formIncrement.charAt(0)) + 16) * 7) + 
        ((parseInt(formIncrement.charAt(1)) + 16) * 8) + 
        ((parseInt(formIncrement.charAt(2)) + 16) * 9) + 
        ((parseInt(formIncrement.charAt(3)) + 16) * 10) + 
        ((parseInt(formIncrement.charAt(4)) + 16) * 11) + 
        ((parseInt(formIncrement.charAt(5)) + 16) * 12) + 
        ((parseInt(formIncrement.charAt(6)) + 16) * 13) + 
        ((parseInt(formIncrement.charAt(7)) + 16) * 14) + 
        ((parseInt(formIncrement.charAt(8)) + 16) * 15); 

/* 
* 452 is the total checksum for my barcodes static prefix (600001), 
* so it doesn't need to be computed dynamically, I just add it to 
* the variable checksum total determined above and then get the 
* modulus of that sum: 
*/ 
var checksum = (452 + incrementCS) % 103 


var barcode = "š600001" + formIncrement 

/* 
* The 0 and the 95 - 102 cases had to be defined explicitly because 
* their checksum figures do not line up with the javascript char 
* codes for some reason (see the code 128 definition table in the 
* linked page) otherwise we simply need to get the charCode of the 
* checksum + 32. I also tack on the stop char here. 
*/ 
switch (checksum) { 
    case 0 : 
    barcode += "€œ"; 
    break; 
    case 95 : 
    barcode += "‘œ"; 
    break; 
    case 96 : 
    barcode += "’œ"; 
    break; 
    case 97 : 
    barcode += "「œ"; 
    break; 
    case 98 : 
    barcode += "」œ"; 
    break; 
    case 99 : 
    barcode += "•œ"; 
    break; 
    case 100 : 
    barcode += "–œ"; 
    break; 
    case 101 : 
    barcode += "—œ"; 
    break; 
    case 102 : 
    barcode += "˜œ"; 
    break; 
    default : 
    barcode += String.fromCharCode(checksum + 32) + "œ"; 
} 

return barcode; 

您可能注意到,我的啓動和停止字符在我的例子(S,OE)似乎不匹配與鏈接頁面上顯示的內容一起顯示。如果我記得我認爲這是因爲我有一些非標準的code128字體,並且這些字符被翻譯成正確的字體。

編輯

我在我的文檔中籤回。它看起來像我從right here得到的字體。使用該字體明確和使用上面的算法,我只是做了一個test條形碼code128b它出來štestwœ,它掃描的罰款。您的校驗和算法似乎工作正常,因爲我們都有w,但看起來您的開始和結束代碼是關閉的,如果您得到:ÌtestwÎ

我做了點尋找,我使用,因爲我有一種感覺,不同品牌CODE128字體,可以實現不同的字符表示的啓動和停止條形碼相同的條形碼字體。

+0

多數民衆贊成在有趣的感謝你。如果我從頁面下載了源代碼並啓動它,我能夠生成一個Code 128(或類似Code 128的東西),但我無法掃描它。嗯,有什麼不對的。 – sabisabi

+0

@sabisabi如果你發佈你的條形碼包含的字符我可以告訴你它有什麼問題。 – Geronimo

+0

嗨,我的輸入值是「試驗」和我的編碼值是「ÌtestwÎ」 - 我得到一個條形碼,但我不是能夠掃描它(http://www.jtbarton.com/Barcodes/BarcodeStringBuilderExample.aspx) – sabisabi

4

您是否正在創建正確的校驗和字符?

看一看this page來看看如何計算校驗

對於可選看看下面的鏈接 - 這允許您創建條碼位圖:

http://www.codeproject.com/KB/graphics/BarcodeLibrary.aspx?fid=470627&fr=26#xx0xx

+0

我想我可以創建所有的條形碼只使用Font()就像9條形碼中的3條一樣。爲什麼要這麼辛苦,我沒有得到它......字體在那裏,那還不夠?如果你谷歌的「下載代碼128字體」,你會發現一些下載。 – sabisabi

+1

@sabisabi code128可以用字體來表示,我會在一會兒發表一個答案。 – Geronimo

+0

@Geronimo - 我已經編輯我的答案 –

0

看着Wikipedia page對於Barcode128,我想你應該用ASCII碼208-210來劃分一個塊,根據條碼寬度的段落和表格。

+0

那是什麼我不明白。 208到210應該是開始「標記」的權利?停止標記是211?然後我添加一個ascii字符像208到我的字符串的開頭,並在最後一個ASCII字符211 - 不知道如何做到這一點與C# – sabisabi