Adjust Barcode Size Barbecue

Asked

Viewed 1,415 times

5

I’m generating a barcode using the BBQ, but where I print the code is reduced or is the paper has dimensions 8 cm wide and the information I have to generate the barcode has 44 digits.

I’ve tried resizing my generated code but I can’t.

Below is an example of how I am generating my code.

Geradorbarras.java

public void GeraCodBarra(String chave) {

    try {
        Barcode barcode = BarcodeFactory.createCode128C(chave);
        barcode.setDrawingText(false);
        BufferedImage image = new BufferedImage(50, 75, BufferedImage.TYPE_BYTE_GRAY);
        Graphics2D g = (Graphics2D) image.getGraphics();
        g.setBackground(Color.BLUE);
        barcode.setBarWidth(180);
        barcode.setBarHeight(30);
        barcode.draw(g, 10, 56);
        File f = new File("C:/codBarra.png");
        BarcodeImageHandler.savePNG(barcode, f);

    } catch (Exception ex) {
        ex.getMessage();
    }
}

and the code size is 35150648847396000214590000020310033320636124

Thank you

2 answers

4


I don’t know a way to do this directly on an object like Barcode, since he does not respect the size, Preferred size, minimum size, etc and not even if it has any utility for this. I looked at version 1.5 and have not.

One way to do this is to change the size of the original image it generates by recovering it from the Barcode.

You can recover the BufferedImage of the object Barcode in this way:

final BufferedImage originalImage = BarcodeImageHandler.getImage(barcode);

We now need to create the representation of the new image, using 302 (approximate value equivalent to 8cm) width and original height. It would look something like this:

final int originalHeight = originalImage.getHeight();

final BufferedImage resizedImage = new BufferedImage(302, originalHeight, originalImage.getType());

Then we need to create the graphic representation of this image and draw it with the new measurements in the new image:

final Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, 302, originalHeight, null);
g.dispose();

Okay, here we already have the resized image, just do whatever it takes. To save to disk, as you are doing, can do so:

final File f = new File("F:/barcode.png");
ImageIO.write(resizedImage, "PNG", f);

A complete example would be this:

final Barcode barcode = BarcodeFactory.createCode128C(chave);
barcode.setDrawingText(false);
barcode.setBarHeight(60);
barcode.setBarWidth(2);

final BufferedImage originalImage = BarcodeImageHandler.getImage(barcode);
final int originalHeight = originalImage.getHeight();

final BufferedImage resizedImage = new BufferedImage(302, originalHeight, originalImage.getType());

final Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, 302, originalHeight, null);
g.dispose();

final File f = new File("F:/barcode.png");
ImageIO.write(resizedImage, "PNG", f);

That generated this barcode:

Código de barras redimensionado

From this original:

Código de barras original

This is an example and you can see other things like scaling in the new image, you’ll find a lot of reference on the internet of how to do this =)

  • She was angry. A doubt in the database query by barcode is Stream comparison?

  • @Krismorte I don’t know if I understand this correctly, but normally images like this are not persisted, but the information that generated it, like a digitable line, so the search would be for this generating information.

  • I’m a little lost. How is the association of the bar code with the register in the bank?

  • @Krismorte include a question with your doubts, there is no relation to this issue. Thanks

  • I created and was negative :( http://answall.com/questions/133834/gerar-e-ler-c%C3%B3digo-de-bars

0

I saw in another post that it is possible to define height and width in Barcode:

barcode.setBarHeight(1200);
barcode.setBarWidth(30);

But when playing on screen it was "huge", so I lowered the height values to 100 and width to 3, then it was great.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.