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:
From this 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
@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.
– Bruno César
I’m a little lost. How is the association of the bar code with the register in the bank?
– Krismorte
@Krismorte include a question with your doubts, there is no relation to this issue. Thanks
– Bruno César
I created and was negative :( http://answall.com/questions/133834/gerar-e-ler-c%C3%B3digo-de-bars
– Krismorte