Adjust bar code size Barbecue

5

I'm generating a bar code using Barbecue, but where I'm going to print the code is rather low or the paper is 8 cm wide and the information I have to generate the barcode has 44 digits.

I've tried to scale my generated code but I can not.

Here's an example of how I'm generating my code.

GeneratorBar.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 size of the code is 35150648847396000214590000020310033320636124

Thank you

    
asked by anonymous 18.08.2015 / 20:41

2 answers

4

I do not know a way to do this directly on an object of type Barcode , since it does not respect the size, the preferred size, the minimum size , etc. and even if it has some utility for it. I looked at version 1.5 and did not have it.

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

You can retrieve BufferedImage from object Barcode like this:

final BufferedImage originalImage = BarcodeImageHandler.getImage(barcode);

Now we need to create the new image representation, using 302 ( equivalent approximate value to 8cm ) of width and the 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();

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

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);

You generated this bar code:

Fromthisoriginal:

This is an example and you can see other things like scale in the new image, you will find lots of reference on the internet how to do this =)

    
19.08.2015 / 02:18
0

I saw in another post that it is possible to set height and width in barcode:

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

However, when playing on the screen it was "huge", so I downloaded the height values to 100 and width to 3, so that was great.

    
01.08.2017 / 21:29