Generate BarCode

Hi,How can we generate barcodes (type 39 and 128) and then print them along with some thext and boxes (actually for ID Cards)?
[147 byte] By [prakharbirlaa] at [2007-10-3 4:16:51]
# 1

I have a code in VB (for code 39) and so i am trying to convert it into java. I reached the loop, but then i dont know how the find the Asc or Ascii of the character. In VB it returns a number like for 1 : 49 etc.

Asc(Mid$(new_string, n, 1))

If any one can help me in this then i may proceed to make the barcode.

'

' Module: ModuleBarcode

' DateTime : 10/7/2002 19:37

' Author: Joyprakash saikia

' Purpose: this Module Consist of all functions required to

' print a barCode 39.

' Fixes: The Bar39 had a small bug in printing '*',

' which is essential for Barcode Readers.

'

Sub DrawBarcode(ByVal bc_string As String, obj As Object)

Dim xpos!, Y1!, Y2!, dw%, Th!, tw, new_string$

If bc_string = "" Then obj.Cls: Exit Sub

'define barcode patterns

Dim bc(90) As String

bc(1) = "1 1221"'pre-amble

bc(2) = "1 1221"'post-amble

bc(48) = "11 221"'digits

bc(49) = "21 112"

bc(50) = "12 112"

bc(51) = "22 111"

bc(52) = "11 212"

bc(53) = "21 211"

bc(54) = "12 211"

bc(55) = "11 122"

bc(56) = "21 121"

bc(57) = "12 121"

'capital letters

bc(65) = "211 12"'A

bc(66) = "121 12"'B

bc(67) = "221 11"'C

bc(68) = "112 12"'D

bc(69) = "212 11"'E

bc(70) = "122 11"'F

bc(71) = "111 22"'G

bc(72) = "211 21"'H

bc(73) = "121 21"'I

bc(74) = "112 21"'J

bc(75) = "2111 2"'K

bc(76) = "1211 2"'L

bc(77) = "2211 1"'M

bc(78) = "1121 2"'N

bc(79) = "2121 1"'O

bc(80) = "1221 1"'P

bc(81) = "1112 2"'Q

bc(82) = "2112 1"'R

bc(83) = "1212 1"'S

bc(84) = "1122 1"'T

bc(85) = "2 1112"'U

bc(86) = "1 2112"'V

bc(87) = "2 2111"'W

bc(88) = "1 1212"'X

bc(89) = "2 1211"'Y

bc(90) = "1 2211"'Z

'Misc

bc(32) = "1 2121"'space

bc(35) = ""'# cannot do!

bc(36) = "1 1 1 11" '$

bc(37) = "11 1 1 1" '%

bc(43) = "1 11 1 1" '+

bc(45) = "1 1122"'-

bc(47) = "1 1 11 1" '/

bc(46) = "2 1121"'.

bc(64) = ""'@ cannot do!

'A Fix made by changing 65 to 42.

bc(42) = "1 1221"'*

bc_string = UCase(bc_string)

'dimensions

obj.ScaleMode = 3'pixels

obj.Cls

obj.Picture = Nothing

dw = CInt(obj.ScaleHeight / 40)'space between bars

If dw < 1 Then dw = 1

'Debug.Print dw

Th = obj.TextHeight(bc_string)'text height

tw = obj.TextWidth(bc_string)'text width

new_string = Chr$(1) & bc_string & Chr$(2)'add pre-amble, post-amble

Y1 = obj.ScaleTop

Y2 = obj.ScaleTop + obj.ScaleHeight - 1.5 * Th

obj.Width = 1.1 * Len(new_string) * (15 * dw) * obj.Width / obj.ScaleWidth

'draw each character in barcode string

xpos = obj.ScaleLeft

For n = 1 To Len(new_string)

c = Asc(Mid$(new_string, n, 1))

Debug.Print Mid$(new_string, n, 1) & ":" & c

If c > 90 Then c = 0

bc_pattern$ = bc(c)

'draw each bar

For i = 1 To Len(bc_pattern$)

Select Case Mid$(bc_pattern$, i, 1)

Case " "

'space

obj.Line (xpos, Y1)-(xpos + 1 * dw, Y2), &HFFFFFF, BF

xpos = xpos + dw

Case "1"

'space

obj.Line (xpos, Y1)-(xpos + 1 * dw, Y2), &HFFFFFF, BF

xpos = xpos + dw

'line

obj.Line (xpos, Y1)-(xpos + 1 * dw, Y2), &H0&, BF

xpos = xpos + dw

Case "2"

'space

obj.Line (xpos, Y1)-(xpos + 1 * dw, Y2), &HFFFFFF, BF

xpos = xpos + dw

'wide line

obj.Line (xpos, Y1)-(xpos + 2 * dw, Y2), &H0&, BF

xpos = xpos + 2 * dw

End Select

Next

Next

'1 more space

obj.Line (xpos, Y1)-(xpos + 1 * dw, Y2), &HFFFFFF, BF

xpos = xpos + dw

'final size and text

obj.Width = (xpos + dw) * obj.Width / obj.ScaleWidth

obj.CurrentX = (obj.ScaleWidth - tw) / 2

obj.CurrentY = Y2 + 0.25 * Th

obj.Print bc_string

End Sub

prakharbirlaa at 2007-7-14 22:18:24 > top of Java-index,Java Essentials,Java Programming...
# 2

> I have a code in VB (for code 39) and so i am trying

> to convert it into java. I reached the loop, but then

> i dont know how the find the Asc or Ascii of the

> character. In VB it returns a number like for 1 : 49

> etc.

So what? So does char. It is the mapping number of the glyph.

CeciNEstPasUnProgrammeura at 2007-7-14 22:18:24 > top of Java-index,Java Essentials,Java Programming...
# 3

Hi,

I did not exactly get what you meant, but i have solved my problem after a lot of pains. And to convert the char to ascii you can do:-

int ascii = (int)char;

--

Here's the final code for generating the 39 code

/*

Generates Barcode (type 39 or 3 to 9)!

Translated from VB6 code by Joyprakash Saikia. Thanks a lot Joyprakash.

By Prakhar Birla

www.dinitsoftwares.com

*/

/*

To run in web-browser put the following code:-

<applet code=barcode39.class width=1000 height=100><param name="BARCODE" value="THE VALUE TO BE ENCODED">alt="Your browser understands the <APPLET> tag but isn't running the applet, for some reason."Your browser is completely ignoring the <APPLET> tag!</applet>

*/

import java.awt.*;

import java.applet.*;

public class barcode39 extends Applet {

public void init( ) { }

public void paint( Graphics g ) {

String bar_code = getParameter("BARCODE");

String bc[] = new String[91];

bc[1] = "1 1221";

//pre-amble

bc[2] = "1 1221";

//post-amble

bc[48] = "11 221";

//digits

bc[49] = "21 112";

bc[50] = "12 112";

bc[51] = "22 111";

bc[52] = "11 212";

bc[53] = "21 211";

bc[54] = "12 211";

bc[55] = "11 122";

bc[56] = "21 121";

bc[57] = "12 121";

//capital letters

bc[65] = "211 12";

bc[66] = "121 12";

bc[67] = "221 11";

bc[68] = "112 12";

bc[69] = "212 11";

bc[70] = "122 11";

bc[71] = "111 22";

bc[72] = "211 21";

bc[73] = "121 21";

bc[74] = "112 21";

bc[75] = "2111 2";

bc[76] = "1211 2";

bc[77] = "2211 1";

bc[78] = "1121 2";

bc[79] = "2121 1";

bc[80] = "1221 1";

bc[81] = "1112 2";

bc[82] = "2112 1";

bc[83] = "1212 1";

bc[84] = "1122 1";

bc[85] = "2 1112";

bc[86] = "1 2112";

bc[87] = "2 2111";

bc[88] = "1 1212";

bc[89] = "2 1211";

bc[90] = "1 2211";

//Misc

bc[32] = "1 2121";

bc[35] = "";

bc[36] = "1 1 1 11";

bc[37] = "11 1 1 1";

bc[43] = "1 11 1 1";

bc[45] = "1 1122";

bc[47] = "1 1 11 1";

bc[46] = "2 1121";

bc[64] = "";

bc[42] = "1 1221";

int dw=2;

bar_code=(char)1 + bar_code + (char)2;

bar_code.toUpperCase();

int bc_lc=0;

for(int i=0;i<bar_code.length();i++)

{

int c=(int)bar_code.charAt(i);

if(c>90)

{

c=0;

}

String bc_pattern=bc[c];

for(int j=0;j<bc_pattern.length();j++)

{

char temp=bc_pattern.charAt(j);

if(temp==' ')

{

bc_lc=bc_lc+dw;

}

if(temp=='1')

{

bc_lc=bc_lc+dw;

drawLine (g, bc_lc, 0, bc_lc, getSize().height, dw, new Color(0).black);

bc_lc=bc_lc+dw;

}

if(temp=='2')

{

bc_lc=bc_lc+dw;

drawLine (g, bc_lc, 0, bc_lc, getSize().height, (2*dw), new Color(0).black);

bc_lc=bc_lc+(dw*2);

}

}

}

}

public void drawLine(

Graphics g, int x1, int y1, int x2, int y2, int thickness, Color c) {

g.setColor(c);

for(int x=0;x<thickness;x++)

{

g.drawLine(x1+x, y1, x1+x, y2);

}

}

}

>

prakharbirlaa at 2007-7-14 22:18:24 > top of Java-index,Java Essentials,Java Programming...