#15 Unable to set the pixel size to a constant one.

closed
nobody
5
2009-04-28
2009-04-24
Jugunu
No

I want the bar code to be generated in 74 X 14 size always, though the text length that i encode varies, is there any way i can assure this?

Discussion

    • status: open --> pending
     
  • Barcode4J doesn't support that directly. You can, however, paint the barcode yourself on a BufferedImage that you can create at the exact dimensions that you need. You can call createGraphics() on the BufferedImage to get a Graphics2D instance to paint on the bitmap. Then you can use the Java2DCanvasProvider to paint the barcode on that Graphics2D instance. See the following link for more information:
    http://barcode4j.sourceforge.net/2.0/embedding.html

    Please close this support request when you get this working. Thanks.

    HTH

     
  • Jugunu
    Jugunu
    2009-04-24

    Thanks Jeremias for the swift response.

    I could see that the bitmapbuilder is the place where the BufferedImage is created for the exact pixel size. I have read that the barcode4j calculates the smallest possible size, but if I hard code it to a constant one (74 X 14), will there be any issue? Please let me know.

     
  • Right, the BitmapBuilder should make your job easier. The only issue I see is if your bitmap is actually too small and you get a visual overflow. Otherwise, you can place the barcode inside the bitmap any way you want using the translate() methods on java.awt.Graphics/Graphics2D. I've done similar things and that worked just fine. Good luck!

     
  • Jugunu
    Jugunu
    2009-04-25

    • status: pending --> open
     
  • Jugunu
    Jugunu
    2009-04-25

    I tried translate but it gives me a white background. I will try tweaking it a little bit.

    I have to add some more information and send it as one gif, like a ticket or so.. Is there any way to do this? I have a template for this.. Its is dynamic... Say customer name + other details changes.

    I mean to keep this gif as a part of another picture.. If you know please let me know.

     
  • Here's some sample code that demonstrates creating a barcode with other Java2D content and saving it as a GIF image:

    Code128Bean bean = new Code128Bean();
    String msg = "12345";
    int orientation = 0;
    int resolution = 300;
    BarcodeDimension dim = bean.calcDimensions(msg);

    int bmw = UnitConv.mm2px(dim.getWidthPlusQuiet(orientation), resolution);
    int bmh = UnitConv.mm2px(dim.getHeightPlusQuiet(orientation), resolution);
    BufferedImage bi = new BufferedImage(bmw * 2, bmh, BufferedImage.TYPE_BYTE_BINARY);
    Graphics2D g2d = bi.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
    RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g2d.setBackground(Color.white);
    g2d.setColor(Color.black);
    g2d.clearRect(0, 0, bi.getWidth(), bi.getHeight());

    //Paint barcode more to the right
    Graphics2D copy = (Graphics2D)g2d.create();
    copy.translate(bmw, 0);
    //Set up coordinate system: Barcode4J calculates in millimeters internally
    copy.scale(bmw / dim.getWidthPlusQuiet(orientation),
    bmh / dim.getHeightPlusQuiet(orientation));
    Java2DCanvasProvider canvas = new Java2DCanvasProvider(copy, orientation);
    bean.generateBarcode(canvas, msg);
    copy.dispose();

    AffineTransform at = new AffineTransform();
    at.translate(25, bmh);
    at.rotate(Math.PI / -4);
    g2d.transform(at);
    g2d.setFont(g2d.getFont().deriveFont(bmh * 0.25f));
    g2d.drawString("Some text!", 0, 0);

    File outputFile = new File("D:\\out.gif");

    OutputStream out = new java.io.FileOutputStream(outputFile);
    try {
    BitmapBuilder.saveImage(bi, out, "image/gif", resolution);
    } finally {
    out.close();
    }

    HTH

     
  • Jugunu
    Jugunu
    2009-04-28

    Thanks a lot Jeremias, I will make use of it.. I am closing this request as well, since i am almost done with my changes. Thanks once again for the friendly support.

     
  • Jugunu
    Jugunu
    2009-04-28

    • status: open --> closed