lunes, 13 de febrero de 2017

Información sobre code 128

Fuente: https://strokescribe.com/en/code-128-excel-word-access-html.html

CODE 128

CODE 128 is a simple and effective format to store small amounts of alphanumeric and ASCII control characters. CODE 128 offers three specialized alphabets: CODE 128A for ASCII control codes, digits and uppercase letter encoding, CODE 128B for uppercase and lowercase letters and digits, CODE 128C for double density numeric data encoding.
CODE 128C compared to CODE 128B
On the right: the top picture shows how a numeric-only string can be encoded in CODE 128C. The middle picture shows an alphanumeric string of the same length encoded in alphabet B.
Of course, CODE 128 allows to mix alphabets - we can start from alphabet C to encode a numeric substring and then switch to alphabet A or B to encode the rest of data to produce a smallest possible barcode (see the bottom picture). This is called alphabet auto-switching - any high quality CODE 128 encoder will do this to achieve maximum barcode density.
Alphabet subtypeWhat can be encoded
CODE 128A
  • Punctuation characters: !"#$%&'()*+,-./:;<=>?@[]\^_
  • Digits: 0..9
  • Capital letters: A..Z
  • ASCII control codes with hex values 0H..20H
CODE 128B
  • Punctuation characters: !"#$%&'()*+,-./:;<=>?@[]\^_
  • Additional characters: `{|}~
  • Digits: 0..9
  • Capital letters: A..Z
  • Small letters: a..z
CODE 128C
  • Digits: 0..9
About maximum barcode length and character count. They are not standardized and limited by particular scanner capabilities. For example, Metrologic MS5145 is capable to scan 38 mm wide barcodes at face and 170 mm wide barcodes from the range of 140 mm. In the first case, bars must not be thinner than 0.1 mm and in the second case bars must not be thinner than 0.66 mm. In most cases, it's best to follow the GS1 recommendations for GS1-128 barcodes:
  • The maximum physical length of barcode is 165 mm (6.5in) including quiet zones.
  • The maximum number of encoded data characters is 48.
  • The minimum module width is 0.250 mm and the maximum module width is 1.016 mm.

How the CODE 128 works

CODE 128 zebra pattern explained
The CODE 128 encoding system consists of 3 code pages that share the same zebra patterns.
For example, the capital letter A is encoded as 10100011000 where each "1" represents a black bar and each "0" is a white bar. All bars must have the same width. A typical laser barcode scanner resolution is 0.15mm, so you can draw a barcode where each (black or white) bar has the width of 0.15mm.
On the right: 11101011 is a pattern you will find at the tail of any CODE 128 symbol. The "111010" sequence is a part of a STOP character and the final "11" (two black bars) is the TERM pattern.
Each CODE 128 symbol begins with a START-A (or START-B or START-C) character, continues with data characters and ends with a check-digit, a STOP character and a TERM pattern. The checksum covers the START character and all data characters multiplied by their position numbers. The check-digit is a reminder from division of the checksum by 103.
A real CODE 128 example: let's encode the capital letters ABC in a CODE 128A symbol.
CharacterSTART-AABCCheck-digitSTOPTERM
Code1033334350106107
Position-123
Checksum10333*1=3334*2=6835*3=105309%103=0
103 + 33 + 68 + 105 = 309
Character pattern11010000100101000110001000101100010001000110110110011001100011101011
Barcode pattern11010000100101000110001000101100010001000110110110011001100011101011
Printable zebraA complete CODE 128 zebra pattern
Note: the % sign is a "remainder from division" operator. For example, 103%103=0, 104%103=1, 207%103=1.
Another CODE 128 example: let's encode the abc<TAB>1234 in the barcode. Small letters can be encoded in the B code page, ASCII <TAB> character exists in the A code page and pairs of digits can be encoded using the C code page. So we start the CODE 128 symbol with the START-B instruction, encode the "abc" string, continue with the Code-A (see Table #3) instruction and encode the <TAB> character, use the Code-C instruction to encode the "1234" with only two character codes 12 and 34.
CharacterSTART-BabcCode-aTABCode-C1234Check-digitSTOPTERM
Code1046566671017399123458106107
Position-12345678
Checksum10465*1 =6566*2 =13267*3 =201101*4 =40473*5 =36599*6 =59412*7 =8434*8 =2722221%103 = 58
104 + 65 + 132 + 201 + 404 + 365 + 594 + 84 + 272 = 2221
Now, look up the encoding tables (see below) for zebra patterns for codes 104, 65, 66, 67, 101, 73, 99, 12, 34, 58, 106, 107. Combine these patterns into one string. Draw a black bar for each "1" and white bar for each "0".

CODE 128 encoding tables

Table #1
CodeCODE 128ACODE 128BCODE 128CZebra bits
0SPSP0011011001100
1!!0111001101100
2""0211001100110
3##0310010011000
4$$0410010001100
5%%0510001001100
6&&0610011001000
7''0710011000100
8((0810001100100
9))0911001001000
10**1011001000100
11++1111000100100
12,,1210110011100
13--1310011011100
14..1410011001110
15//1510111001100
16001610011101100
17111710011100110
18221811001110010
19331911001011100
20442011001001110
21552111011100100
22662211001110100
23772311101101110
24882411101001100
25992511100101100
26::2611100100110
27;;2711101100100
28<<2811100110100
29==2911100110010
30>>3011011011000
31??3111011000110
32@@3211000110110
33AA3310100011000
34BB3410001011000
35CC3510001000110
36DD3610110001000
37EE3710001101000
38FF3810001100010
39GG3911010001000
40HH4011000101000
41II4111000100010
42JJ4210110111000
43KK4310110001110
44LL4410001101110
45MM4510111011000
46NN4610111000110
47OO4710001110110
Table #2
CodeCODE 128ACODE 128BCODE 128CZebra bits
48PP4811101110110
49QQ4911010001110
50RR5011000101110
51SS5111011101000
52TT5211011100010
53UU5311011101110
54VV5411101011000
55WW5511101000110
56XX5611100010110
57YY5711101101000
58ZZ5811101100010
59[[5911100011010
60\\6011101111010
61]]6111001000010
62^^6211110001010
63__6310100110000
64<NUL> (0x00)`6410100001100
65<SOH> (0x01)a6510010110000
66<STX> (0x02)b6610010000110
67<ETX> (0x03)c6710000101100
68<EOT> (0x04)d6810000100110
69<ENQ> (0x05)e6910110010000
70<ACK> (0x06)f7010110000100
71<BEL> (0x07)g7110011010000
72<BS> (0x08)h7210011000010
73<HT> (0x09)i7310000110100
74<LF> (0x0a)j7410000110010
75<VT> (0x0b)k7511000010010
76<FF> (0x0c)l7611001010000
77<CR> (0x0d)m7711110111010
78<SO> (0x0e)n7811000010100
79<SI> (0x0f)o7910001111010
80<DLE> (0x10)p8010100111100
81<DC1> (0x11)q8110010111100
82<DC2> (0x12)r8210010011110
83<DC3> (0x13)s8310111100100
84<DC4> (0x14)t8410011110100
85<NAK> (0x15)u8510011110010
86<SYN> (0x16)v8611110100100
87<ETB> (0x17)w8711110010100
88<CAN> (0x18)x8811110010010
89<EM> (0x19)y8911011011110
90<SUB> (0x1a)z9011011110110
91<ESC> (0x1b){9111110110110
92<FS> (0x1c)|9210101111000
93<GS> (0x1d)}9310100011110
94<RS> (0x1e)~9410001011110
95<US> (0x1f)<DEL> (0x7f)9510111101000
Table #3
CodeCODE 128ACODE 128BCODE 128CZebra bits
96FNC3FNC39610111100010
97FNC2FNC29711110101000
98SHIFTSHIFT9811110100010
99Code-CCode-C9910111011110
100Code-BFNC4Code-B10111101110
101FNC4Code-ACode-A11101011110
102FNC1FNC1FNC111110101110
103START-ASTART-ASTART-A11010000100
104START-BSTART-BSTART-B11010010000
105START-CSTART-CSTART-C11010011100
106STOPSTOPSTOP11000111010
107TERMTERMTERM11
Attention! The barcode must have whitespaces at the left/right sides equal to the width of 10 bars. Do not place any text or graphics on these areas.
Do not try to make your barcode as small as possible. Even the barcode scanner's resolution can be 0.1-0.15 mm, a cheap paper or ink will produce ragged barcode bars. Normally, good quality 0.1 mm barcodes may be printed only at printing office.

How to create CODE 128 in Microsoft Word with VBA

CODE 128 in Microsoft Word - YouTube video
A minimalistic example of placing CODE 128 barcodes into a Word document using VBA and StrokeScribe ActiveX. If you need to place a single barcode or if you want to manually set barcode properties, use these guides for Word 2007Word 2010 and Word 2013.
The code below uses the InlineShapes collection and places a barcode object at the current text input position. Also, the code demonstrates how to do the following tweaks:
CODE 128 in Microsoft Word
  • A CODE 128 produced with default ActiveX settings.
  • A barcode rotated 90 degrees counterclockwise.
  • The TextBelow property is used to show a user-specified text below the barcode.
  • The text justification is turned of by TextJustify=False.
  • The ProcessTilde property allows to encode ASCII control characters like <TAB>.
  • Another way to encode ASCII control characters is to use Chr(). This barcode encodes <CR><LF>.
Application.ScreenUpdating = False

Dim doc As Document
Set doc = Word.ActiveDocument

Dim shp As InlineShape 'This shape type behaves as an ordinary text character
Set shp = doc.InlineShapes.AddOLEObject("STROKESCRIBE.StrokeScribeCtrl.1", _
                                 Range:=Selection.Range) 'Selection.Range points to the current text input position

Dim ss As StrokeScribe 'You will need to add a reference to StrokeScribe ActiveX into VBA project
Set ss = shp.OLEFormat.Object

ss.Alphabet = CODE128
ss.Text = "1234ABCDE"
'ss.Rotation = 90  'Uncomment this to rotate barcode 90 degrees counterclockwise (2)   
'ss.TextBelow = "TEXT" 'Uncomment this to display a user-specified text below the barcode (3)
'ss.TextJustify = False 'Uncomment this to disable text justification (4)
ss.ProcessTilde = True 'This allows processing of tilde sequences (5)
ss.Text = "ABC~x09DEF" ' ~x09 encodes ASCII TAB character
ss.Text = "ABC" & Chr(13) & Chr(10) & "DEF" 'Another way to encode ASCII control codes (6)

shp.LockAspectRatio = msoFalse 'This allows to independently change width and height of the shape
shp.Width = Word.CentimetersToPoints(1)
shp.Height = Word.CentimetersToPoints(3)

Application.ScreenUpdating = True
Paste the code into a VBA procedure and add a reference to StrokeScribe ActiveX into your VBA project.
For bulk Code-128 printing in Microsoft Word, go here.
If you aren't familiar with VBA development for Word, see these examples for Word 2007 and Word 2010.
And use the following code to automatically delete all previously created barcode shapes:
Application.ScreenUpdating = False
For Each sh In ActiveDocument.InlineShapes
  If sh.OLEFormat.ClassType = "STROKESCRIBE.StrokeScribeCtrl.1" Then
    sh.Select
    sh.Delete
  End If
Next sh
Application.ScreenUpdating = True
To manually adjust the barcode properties, double-click the barcode picture.
To make a floating shape, right-click the barcode and select Format Control from the context menu. Switch to the Layout tab and choose a desired wrapping style. To do the same with VBA, execute:
Dim sh As Shape
shp.Select
Set sh = shp.ConvertToShape
sh.WrapFormat.Type = wdWrapNone
To change position of the floating shape, execute:
sh.Left = Application.CentimetersToPoints(5)
sh.Top = Application.CentimetersToPoints(10)
To delete barcodes converted to Shapes (not InlineShapes!), you need the different approach:
Set doc = Word.ActiveDocument
Dim sh As Shape
For i = doc.Shapes.Count To 1 Step -1
  Set sh = doc.Shapes(i)
  If sh.Type = msoEmbeddedOLEObject Then
    If sh.OLEFormat.ClassType = "STROKESCRIBE.StrokeScribeCtrl.1" Then
      sh.Delete
    End If
  End If
Next i

CODE 128 in Microsoft Excel

This will show how to use StrokeScribe ActiveX to create a single CODE 128 barcode from a cell data in Excel. For bulk CODE 128 printing, see next example. Additional examples of automatic barcode generation are available here.
If you need to manually place a single CODE 128 on Excel worksheet, see instructions how to do it in Excel 2007Excel 2010 and Excel 2013. Also, you can install our barcode add-in to simplify this process.
If you aren't familiar with VBA development for Excel or if you want an example with statically placed ActiveX, see these step-by-step guides for Excel 2007Excel 2010 and Excel 2013.
A CODE 128 barcode in Excel, rotated to 90 degrees
The VBA code shown below encodes content of the cell A1 in a vertically oriented CODE 128 barcode which is placed in the cell range B1:B8. To create a horizontally oriented barcode, set barcode.Rotation=0 and use a range like B1:B3
To run this code, add a reference to StrokeScribe ActiveX into your VBA project.
Application.ScreenUpdating = False 'This prevents screen flickering. Useful when creating/moving/deleting multiple ActiveX objects

Dim wsh As Worksheet
Set wsh = Excel.ActiveSheet 'A CODE 128 object will be placed on the currently active worksheet
'Use wsh=Excel.Worksheets(1) or wsh=Excel.Worksheets("Sheet1") to select a worksheet by index or by name

data = wsh.Cells(1, 1) 'or =wsh.Range("A1").Text

Dim r As Range
Set r = wsh.Range("B1:B8") 'A range of cells to fit the barcode

Dim shp As Shape
Set shp = wsh.Shapes.AddOLEObject("STROKESCRIBE.StrokeScribeCtrl.1")

shp.LockAspectRatio = msoFalse
shp.Width = r.Width 'The barcode will have the same position and size with the cell range B1:B8
shp.Height = r.Height
shp.Left = r.Left
shp.Top = r.Top

Dim barcode As StrokeScribe
Set barcode = shp.OLEFormat.Object.Object

barcode.Alphabet = CODE128
barcode.Text = data 'A text string from the cell A1
barcode.Rotation = 90 'The barcode is turned 90 degrees counterclockwise

'barcode.TextBelow = "text label" 'Uncomment this if you want a user-specified label below the barcode
'barcode.ProcessTilde = True 'Uncomment this if you need to encode ASCII control codes as tilde sequences
'barcode.Text = "ABC~x09DEF" 'A way to encode ASCII control codes in a plain text string (~x09 is ASCII TAB)
'barcode.Text = "ABC" & Chr(13) & Chr(1) & "DEF" 'Another way to use ASCII codes is to use the Chr() function
                                                'This encodes <CR><LF>
Application.ScreenUpdating = True

If barcode.Error Then
  MsgBox barcode.ErrorDescription
End If
To delete (or to modify) previously created CODE 128 barcodes, use the following VBA code:
Dim sh As Shape
For Each sh In Excel.ActiveSheet.Shapes
  If sh.Type = msoOLEControlObject Then
    If sh.OLEFormat.progID = "STROKESCRIBE.StrokeScribeCtrl.1" Then
      Dim ss As StrokeScribe
      Set ss = sh.OLEFormat.Object.Object
      If ss.Alphabet = CODE128 Then
        sh.Delete
      End If
    End If
  End If
Next
If you want to place barcode at arbitrary position on Excel worksheet, modify the code as follows:
shp.Left = Application.InchesToPoints(1)
shp.Top = Application.InchesToPoints(1)
shp.Width = Application.CentimetersToPoints(4)
shp.Height = Application.CentimetersToPoints(2)
If you need to start printing from the same VBA script which modifies barcode properties, you need to know: Excel does not process the message queue while the VBA script is running, and, therefore, it does not refresh ActiveX controls. To force Excel to redraw a barcode, change it's size:
barcode.Text = data

tmp = barcode.Width
barcode.Width = 10
barcode.Width = tmp

wsh.PrintOut
For bulk CODE 128 printing in Microsoft Excel, go here.
Also, you may use formula to insert barcode into a cell. The example uses the same advanced technique as it described in the bulk barcode printing example.

How to use Data Binding in Excel

A CODE 128 produced using Data Binding in Excel
To synchronize a barcode object with a cell's content, you can use the LinkedCell property. For a manually inserted ActiveX object (see example for Excel 2013), set the LinkedCell property as shown on the picture.
If a barcode control is inserted using VBA code (see previous example), use the following code to bind it to a cell:
barcode.LinkedCell = "A1"

CODE 128 in Microsoft Access

This example shows how to create CODE 128 barcodes in Access 2007 and Access 2010 reports. For Access XP/2003, go to this article.
1. Open the report where you want to create CODE 128 barcodes and perform the following steps:
Switching to the Desing mode in Microsoft Access
2. Switch to the Design tab. Click on the View button and select the Design View.
ActiveX controls in Access 2007
3.1. If you are working with Access 2007, click on the ActiveX Controls button as shown on the picture.
'More controls' button on the Design tab
3.2. If you are working with Access 2010, do the following two steps:
3.2.1. Click on the More Controls button.
The 'More controls' drop-down list
3.2.2. From the drop-down list, select ActiveX Controls.
A list of available ActiveX objects
4. From the list of available objects, select StrokeScribe Control.
The context menu of barcode object
5. Right-click on the barcode object and select Properties from the context menu to open the property sheet.
The property sheet of the barcode object
6. Switch to the Data tab on the Property Sheet window. Use the Control Source field to bind the barcode object to the desired database field.
Adjusting the barcode properties
7. Switch to the Other tab on the Property Sheet. Select CODE128 in the Alphabet field.

CODE 128 in HTML

An example how to show a CODE 128 barcode on HTML page. Because this example uses an ActiveX control to draw barcodes, it will work only with Internet Explorer. To run this example, copy the code into .hta file, this will disable numerous security warnings displayed by Internet Explorer. For .html files, you will need to modify some security settings in IE (see examples for IE 6 and IE 7 and 8).
<html>
<head> 
  <script>
  function SetBarcode() 
  { 
    var barcode = document.getElementById("barcode");
    barcode.Text = "1234567";
  }
  </script>
</head>

<body>
  <button onclick="javascript:SetBarcode()">Start</button>

  <object classid="CLSID:7E42B8C5-73BE-4806-8904-FF4080A6960C" id="barcode" 
    style="position: absolute; left: 20mm; top: 10mm; width: 15mm; height: 35mm">
   <param name="Alphabet" value="5"/>
   <param name="Text" value="1234ABCDEF"/>
   <param name="Rotation" value="90"/>
  </object>
</body>
</html>

No hay comentarios:

Publicar un comentario

Jesús Moreno - Ingeniero Ténico Informático - consultor Informático

Hola, soy Jesús Moreno Ingeniero Técnico Informático en sistemas por la US y propietario de éste blog. Mi trabajo en los ultimos años se ...