Roni,

Estou lhe enviando em anexo um exempo de geração do código de barras e
exibição na tela!
Bom proveito!

Márcio Pedro Leichtweis.

> 
> 
>     Caro Marcio..
>     
>         
>         Como devo utilizar a chamada da classe:
> 
>                   String codigoBarras="12345678"; // o numero de       
>      
>             digitos deve ser sempre par
>                  //--------------BarCode
>                  yaBarCode2of5 bc2 = new yaBarCode2of5();
>                  bc2.createBarCode(codigoBarras);
>                  g.drawImage(bc2.getImagem(),10,275,null);
>         
>         Sendo que não consigo instanciar (g)
>         Agradeco desde ja aguardando novos contatos..
> 
> 
> 
> 
> 
> -------Mensagem original-------
> 
> De: [EMAIL PROTECTED]
> Data: segunda-feira, 2 de dezembro de 2002 14:36:30
> Para: [EMAIL PROTECTED]
> Assunto: Fwd: Re: Res: Re: [java-list] Codigo de Barras padrão FE BRABA
> N
> 
> Roni!
> 
> Tenho mandado o seguinte código à baixo para a lista! - talvez tu nã
> o o
> tenhas recebido!
> Este código permite que não seja nescessário ler do disco o códig
> o de
> barras. A classe que o gera guarda um atributo que, referenciado por uma
> classe que
> o chame, mostre na tela o codigo de barras através do método drawImag
> e da
> classe Graphics.
> 
> 
> Ok?
> 
> Qualquer coisa, retorne!
> 
> Márcio.
> 
> --- Weitergeleitete Nachricht / Forwarded Message ---
> Date: Fri, 29 Nov 2002 17:56:17 +0100 (MET)
> From: [EMAIL PROTECTED]
> To: "Roni César Araújo" <[EMAIL PROTECTED]>
> Subject: Re: Res: Re: [java-list] Codigo de Barras padrão FEBRABAN
> 
> > Bem,
> > 
> > tenho feito algo similar....
> > 
> > Eu modifiquei a classe BarCode2of5 de forma que guarde um atributo de
> > classe
> > que é utilizado pela classe que instancia BarCode20f5 (agora chamada 
> de
> > yaBarCode2of5).
> > 
> > Veja a baixo o código para a classe que instancia yaBarCode2of5 e a
> > própria
> > classe. Qualquer coisa entre em contato e bom proveito...
> > 
> > Márcio Pedro Leichtweis.
> > [EMAIL PROTECTED]
> > 
> > //------------------------------------------------------------
> > //------------------------------------------------------------
> > //linhas para a classe que instancia yaBarCode2of5
> > import java.awt.*;
> > import javax.swing.*;
> > import java.lang.String;
> > ...
> > ...
> > ...
> > //-------------- codigo de barras
> > String codigoBarras="12345678"; // o numero de digitos deve ser sempr
> e par
> > //--------------BarCode
> > yaBarCode2of5 bc2 = new yaBarCode2of5();
> > bc2.createBarCode(codigoBarras);
> > g.drawImage(bc2.getImagem(),10,275,null);
> > 
> > 
> > //------------------------------------------------------------
> > //------------------------------------------------------------
> > //classe que gera o codigo de barras
> > //package barcode;
> > 
> > import com.sun.image.codec.jpeg.*;
> > import java.awt.*;
> > import java.awt.image.BufferedImage;
> > import java.io.*;
> > 
> > /**
> > * A class that produces a Barcode component.
> > */
> > public class yaBarCode2of5 extends Canvas {
> > 
> > private BufferedImage imagem;
> > 
> > public BufferedImage getImagem(){
> > return this.imagem;
> > }
> > 
> > 
> > private static String alphabet2of5 = "0123456789";
> > private static String coded2of5Char[] = {"00110", "10001", "01001",
> > "11000",
> > "00101", "10100", "01100",
> > "00011",
> > "10010", "01010"};
> > 
> > /** Constant for variant of the code. */
> > public static final int CODE2OF5 = 0;
> > 
> > /** Constant for variant of the code. */
> > public static final int CODE2OF5CHK = 1;
> > 
> > /** Constant for size of the narrowest bar. */
> > public static final int SMALL = 1;
> > 
> > /** Costant for size of the narrowest bar. */
> > public static final int MEDIUM = 2;
> > 
> > /** Constant for size of the narrowest bar. */
> > public static final int LARGE = 3;
> > 
> > /** Constant for text alignment. */
> > public static final int BASELINE = 0;
> > 
> > /** Constant for text alignment. */
> > public static final int MIDDLELINE = 1;
> > 
> > /** Constant for text alignment. */
> > public static final int TOPLINE = 2;
> > private static final int DEFWIDTH = 50;
> > private static final int DEFHEIGHT = 25;
> > private static final boolean DEFTEXTINS = true;
> > private static final int DEFSIZE = 1;
> > private static final Color DEFBACKCOLOR;
> > private static final Color DEFFORECOLOR;
> > private static final Font DEFFONT = new Font("Courier", 0, 12);
> > private String strStartPattern;
> > private String strStopPattern;
> > private double wideToNarrowRatio;
> > private double marginWidth;
> > private double marginHeight;
> > private double labelLength;
> > private double labelHeight;
> > private String stringToEncode;
> > private String filledStringToEncode;
> > private String encodedString;
> > private int narrowestDim;
> > private boolean textInside;
> > private int style;
> > private int initialWidth;
> > private int initialHeight;
> > private Color backColor;
> > private Color foreColor;
> > private Font font;
> > private int textAlign;
> > 
> > static {
> > DEFBACKCOLOR = Color.white;
> > DEFFORECOLOR = Color.black;
> > }
> > 
> > /**
> > * <P>Constructs a Barcode object with an empty string and the following
> 
> > defaults:
> > * <BR>size 100x50;
> > * <BR>label inside;
> > * <BR>small rendering;
> > * <BR>black on white color;
> > * <BR>Courier,12 font;
> > */
> > public yaBarCode2of5() {
> > this("012345", 50, 25, 1, 0, true, DEFBACKCOLOR, DEFFORECOLOR,
> > DEFFONT,
> > 2);
> > }
> > 
> > private yaBarCode2of5(String s, int i, int j, int k, int l, boolean
> > flag,
> > Color color, Color color1, Font font1, int i1) {
> > strStartPattern = "0000";
> > strStopPattern = "100";
> > //wideToNarrowRatio = 3D;
> > wideToNarrowRatio = 2D; //*******
> > stringToEncode = "";
> > filledStringToEncode = "";
> > encodedString = "";
> > narrowestDim = 1;
> > textInside = true;
> > style = 0;
> > initialWidth = 50;
> > initialHeight = 25;
> > backColor = DEFBACKCOLOR;
> > foreColor = DEFFORECOLOR;
> > font = DEFFONT;
> > textAlign = 1;
> > stringToEncode = s;
> > narrowestDim = k;
> > initialWidth = i;
> > initialHeight = j;
> > textInside = flag;
> > style = l;
> > backColor = color;
> > foreColor = color1;
> > font = font1;
> > textAlign = i1;
> > Encode();
> > }
> > 
> > 
> >
> /////////////////////////////////////////////////////////////////////////
> ////
> 
> > // Public methods
> > 
> >
> /////////////////////////////////////////////////////////////////////////
> ////
> 
> > 
> > /**
> > * Returns the mininimum size of this component.
> > */
> > public Dimension getMinimumSize() {
> > return minimumSize();
> > }
> > 
> > /**
> > * Returns the minimum size of this component.
> > */
> > public Dimension minimumSize() {
> > Dimension dimension = new Dimension(initialWidth, initialHeight);
> > return dimension;
> > }
> > 
> > public Dimension getPreferredSize() {
> > return preferredSize();
> > }
> > 
> > public Dimension preferredSize() {
> > return minimumSize();
> > }
> > 
> > public void setSize(int i, int j) {
> > initialWidth = i;
> > initialHeight = j;
> > super.setSize(i, j);
> > repaint();
> > }
> > 
> > public void setSize(Dimension dimension) {
> > initialWidth = dimension.width;
> > initialHeight = dimension.height;
> > super.setSize(dimension.width, dimension.height);
> > repaint();
> > }
> > 
> > public String toString() {
> > return getClass().getName() + "[" + paramString() + "]";
> > }
> > 
> > public Dimension requestedMinimunSize(String s) {
> > int i = s.length() * 16 * narrowestDim + 31 * narrowestDim;
> > if(style == 1)
> > i += 16 * narrowestDim;
> > int j = Math.max((int)(0.14999999999999999D * (double)i), 35);
> > return new Dimension(i, j);
> > }
> > 
> > public void setString(String s) throws IllegalArgumentException {
> > stringToEncode = s;
> > stringValidate();
> > Encode();
> > }
> > 
> > public String getString() {
> > return stringToEncode;
> > }
> > 
> > public void setDimension(int i) {
> > switch(i) {
> > case 1: // '\001'
> > case 2: // '\002'
> > case 3: // '\003'
> > narrowestDim = i;
> > repaint();
> > return;
> > }
> > narrowestDim = 1;
> > repaint();
> > }
> > 
> > public int getDimension() {
> > return narrowestDim;
> > }
> > 
> > public void setTextInside(boolean flag) {
> > textInside = flag;
> > repaint();
> > }
> > 
> > public boolean isTextInside() {
> > return textInside;
> > }
> > 
> > public void setStyle(int i) {
> > switch(i) {
> > case 0: // '\0'
> > case 1: // '\001'
> > style = i;
> > Encode();
> > return;
> > }
> > style = 0;
> > Encode();
> > }
> > 
> > public int getStyle() {
> > return style;
> > }
> > 
> > public void setBackground(Color color) {
> > backColor = color;
> > repaint();
> > }
> > 
> > public Color getBackground() {
> > return backColor;
> > }
> > 
> > public void setForeground(Color color) {
> > foreColor = color;
> > repaint();
> > }
> > 
> > public Color getForeground() {
> > return foreColor;
> > }
> > 
> > public void setFont(Font font1) {
> > font = font1;
> > repaint();
> > }
> > 
> > public Font getFont() {
> > return font;
> > }
> > 
> > public void setTextAlign(int i) {
> > switch(i) {
> > case 0: // '\0'
> > case 1: // '\001'
> > case 2: // '\002'
> > textAlign = i;
> > repaint();
> > return;
> > }
> > textAlign = 1;
> > repaint();
> > }
> > 
> > public int getTextAlign() {
> > return textAlign;
> > }
> > 
> > public synchronized void paint(Graphics g) {
> > int i = getSize().width;
> > int j = getSize().height;
> > g.setFont(font);
> > FontMetrics fontmetrics = g.getFontMetrics(font);
> > int k = fontmetrics.getAscent();
> > labelLength = filledStringToEncode.length() * 8 * narrowestDim;
> > //marginWidth = ((double)i - labelLength) / 2D;
> > //marginHeight = ((double)j - labelLength) / 2D;
> > marginWidth = 0;
> > marginHeight = 0;
> > if(textInside) {
> > if(textAlign == 0) {
> > labelHeight = j;
> > }
> > 
> > if(textAlign == 1) {
> > labelHeight = j - k / 2;
> > }
> > 
> > if(textAlign == 2) {
> > labelHeight = j - k;
> > }
> > }
> > else {
> > labelHeight = j;
> > }
> > 
> > g.setColor(backColor);
> > g.fillRect(0, 0, i, j);
> > int l = encodedString.length();
> > int i1 = 0;
> > int j1 = 0;
> > int k1 = 0;
> > int l1 = 0;
> > boolean flag = false;
> > i1 = (int)marginWidth;
> > j1 = 0;
> > k1 = 0;
> > l1 = 0;
> > for(int j2 = 0; j2 < l; j2++) {
> > if(j2 % 2 == 0) {
> > g.setColor(foreColor);
> > }
> > else {
> > g.setColor(backColor);
> > }
> > 
> > int i2;
> > if(encodedString.charAt(j2) == '1') {
> > i2 = (int)((double)narrowestDim * wideToNarrowRatio);
> > }
> > else {
> > i2 = narrowestDim;
> > }
> > g.fillRect(k1 + i1, l1 + j1, i2, (int)labelHeight);
> > k1 += i2;
> > } //************
> > 
> > if(textInside) {
> > int k2 = fontmetrics.stringWidth(filledStringToEncode);
> > int l2 = getSize().height;
> > int i3 = (int)((marginWidth + labelLength / 2D) - (double)(k2 / 2));
> > g.setColor(backColor);
> > g.fillRect(i3, l2 - k, k2, k);
> > g.setColor(foreColor);
> > g.drawString(filledStringToEncode, i3, l2);
> > }
> > }
> > 
> > 
> >
> /////////////////////////////////////////////////////////////////////////
> ////
> 
> > // Protected methods
> > 
> >
> /////////////////////////////////////////////////////////////////////////
> ////
> 
> > 
> > protected String paramString() {
> > String s = filledStringToEncode + "," + initialWidth + "x" +
> > initialHeight;
> > return s;
> > }
> > 
> > 
> >
> /////////////////////////////////////////////////////////////////////////
> ////
> 
> > // Private methods
> > 
> >
> /////////////////////////////////////////////////////////////////////////
> ////
> 
> > 
> > private void stringValidate() throws IllegalArgumentException {
> > int i = stringToEncode.length();
> > for(int j = 0; j < i; j++) {
> > if(alphabet2of5.indexOf(stringToEncode.charAt(j)) == -1) {
> > throw new IllegalArgumentException("only digits for 2of5 code");
> > }
> > }
> > }
> > 
> > private char addCheckChar(String s) {
> > int i = 0;
> > int j = 0;
> > int l = 0;
> > int k = 0;
> > int i1 = s.length();
> > for(int j1 = 0; j1 < i1; j1++) {
> > if(j1 % 2 == 0) {
> > l += alphabet2of5.indexOf(stringToEncode.charAt(j1));
> > }
> > else {
> > k += alphabet2of5.indexOf(stringToEncode.charAt(j1));
> > }
> > }
> > 
> > if(i1 % 2 == 0) {
> > i = l;
> > j = k;
> > }
> > else {
> > i = k;
> > j = l;
> > }
> > 
> > j *= 3;
> > j += i;
> > if(j % 10 == 0) {
> > return alphabet2of5.charAt(0);
> > }
> > else {
> > return alphabet2of5.charAt(10 - j % 10);
> > }
> > }
> > 
> > private void Encode() {
> > filledStringToEncode = new String(stringToEncode);
> > 
> > if(style == 1) {
> > filledStringToEncode += addCheckChar(stringToEncode);
> > }
> > 
> > if(filledStringToEncode.length() % 2 != 0) {
> > filledStringToEncode = "0" + filledStringToEncode;
> > }
> > 
> > StringBuffer stringbuffer = new StringBuffer();
> > for(int i = 0; i < filledStringToEncode.length(); i += 2) {
> > stringbuffer.append(funInterleave(filledStringToEncode.charAt(i),
> > filledStringToEncode.charAt(i + 1)));
> > }
> > 
> > encodedString = stringbuffer.toString();
> > encodedString = strStartPattern + stringbuffer.toString();
> > encodedString += strStopPattern;
> > repaint();
> > }
> > 
> > private String funInterleave(char c, char c1) {
> > StringBuffer stringbuffer = new StringBuffer();
> > for(int i = 0; i < 5; i++) {
> > 
> > stringbuffer.append(coded2of5Char[alphabet2of5.indexOf(c)].charAt(i));
> > 
> > stringbuffer.append(coded2of5Char[alphabet2of5.indexOf(c1)].charAt(i));
> 
> > }
> > 
> > return stringbuffer.toString();
> > }
> > 
> > /**
> > * Constrói uma imagem contendo o código de barra.
> > * <p>
> > * @param Stream onde será retornada a imagem
> > * @param Codigo que dará origem a imagem
> > * <p>
> > * @return imagem no formato stream
> > */
> > // public void createBarCode(OutputStream imageStream, String codigo) {
> 
> > public void createBarCode(String codigo) {
> > this.setDimension (yaBarCode2of5.SMALL);
> > // this.setTextInside (true);
> > this.setTextInside (false);
> > this.setStyle (yaBarCode2of5.CODE2OF5);
> > this.setForeground (Color.black);
> > this.setTextAlign (yaBarCode2of5.TOPLINE);
> > 
> > this.setSize(360, 35);
> > // this.setSize(360, 45);
> > 
> > try {
> > this.setString(codigo);
> > }
> > catch (Exception e){
> > e.printStackTrace(System.err);
> > throw new RuntimeException("Erro ao configurar o Código de Barras:
> > \n"
> > +e.getMessage());
> > }
> > 
> > BufferedImage image = createComponentImage(this);
> > 
> > this.imagem=image;//###
> > 
> > }
> > 
> > /**
> > * Cosntrói uma imagem conforme o componente informado.
> > *
> > * @param component desenho que será transformado
> > * @return imagem desenhada
> > */
> > private BufferedImage createComponentImage(Component component) {
> > Rectangle rect = component.getBounds();
> > 
> > BufferedImage image = new BufferedImage(rect.width
> > ,rect.height
> > ,BufferedImage.TYPE_INT_RGB);
> > Graphics g = image.getGraphics();
> > 
> > //write to the image
> > component.paint(g);
> > 
> > //dispose of the graphics content
> > g.dispose();
> > 
> > return image;
> > }
> > 
> > }
> > 
> > 
> > 
> > 
> > > Caro amigo Marcio...
> > > 
> > > Agradeco muito por ter me mandado o codigo para impressao de cod.
> > bar
> > > ras
> > > como sou iniciante em java gostaria ja que a funcao cria um arquivo
> > cham
> > > ado
> > > cb1.jpg, joga -la para um label na tela sem ter que chama-la no
> > direto
> > > rio
> > > .
> > > Agradeço desde ja aguardando novos contatos
> > > 
> > > 
> > > -------Mensagem original-------
> > > 
> > > De: [EMAIL PROTECTED]
> > > Data: terça-feira, 26 de novembro de 2002 00:52:40
> > > Para: [EMAIL PROTECTED]
> > > Assunto: Re: [java-list] Codigo de Barras padrão FEBRABAN
> > > 
> > > Outro dia circulou pela lista a biblioteca em anexo que, é simples 
> de u
> > > sar e
> > > faz o que deve!
> > > 
> > > Bom uso!
> > > Márcio.
> > > 
> > > > Caros Amigos Javaneses....
> > > > 
> > > > Preciso gerar um codigo de barras no padrao dos bancos (FEBRABAN) p
> a
> > > > ra
> > > > impressao de boleto bancario, se alguem poder me ajudar, por favor
> > ....
> > > ..
> > > > 
> > > > 
> > > > Muito Obrigado
> > > > 
> > > > Roni César
> > > 
> > > -- 
> > > +++ GMX - Mail, Messaging & more http://www.gmx.net +++
> > > NEU: Mit GMX ins Internet. Rund um die Uhr für 1 ct/ Min. surfen!
> > 
> > -- 
> > +++ GMX - Mail, Messaging & more http://www.gmx.net +++
> > NEU: Mit GMX ins Internet. Rund um die Uhr für 1 ct/ Min. surfen!
> > 
> > 
> 
> -- 
> +++ GMX - Mail, Messaging & more http://www.gmx.net +++
> NEU: Mit GMX ins Internet. Rund um die Uhr für 1 ct/ Min. surfen!
> .

-- 
+++ GMX - Mail, Messaging & more  http://www.gmx.net +++
NEU: Mit GMX ins Internet. Rund um die Uhr für 1 ct/ Min. surfen!

Attachment: codbarras.zip
Description: Zip compressed data

------------------------------ LISTA SOUJAVA ----------------------------
http://www.soujava.org.br  -  Sociedade de Usuários Java da Sucesu-SP
dúvidas mais comuns: http://www.soujava.org.br/faq.htm
regras da lista: http://www.soujava.org.br/regras.htm
historico: http://www.mail-archive.com/java-list%40soujava.org.br
para sair da lista: envie email para [EMAIL PROTECTED]
-------------------------------------------------------------------------

Responder a