如何在Java / iReport中旋转条形码?

时间:2010-10-25 12:38:47

标签: java jasper-reports barcode

据我所知,这样的条形码无法旋转(iReport没有该属性,Java类中的烧烤条形码也没有)。我已经看过一些例子,但它们不完整,我不明白如何使用它们,例如:

public class BarbecueRenderer extends JRAbstractSvgRenderer
{

private boolean rotate;
private Barcode barcode = null;

public BarbecueRenderer(Barcode barcode) 
{
    this(barcode, false);
}

public BarbecueRenderer(Barcode barcode, boolean rotate) 
{
    this.barcode = barcode;
    this.rotate = rotate;
}

// What should I use as the grx and rectangle objects?
public void render(Graphics2D grx, Rectangle2D rectangle) 
{
    if (barcode != null) 
    {
        Graphics2D graphics = (Graphics2D) grx.create();
        graphics.translate(rectangle.getX(), rectangle.getY());
        if (rotate)
        {
            graphics.translate(barcode.getBounds().getHeight(), 0);
            graphics.rotate(Math.PI / 2);
        }
        barcode.draw(graphics, 0, 0);
    }
}
}

我需要的是这样的事情:

 Barcode barcode = BarcodeFactory.createCode39("128", false);
 // rotate the barcode
 File f = new File ("c:\\barcode.jpg");
 BarcodeImageHandler.saveJPEG(barcode, f);

2 个答案:

答案 0 :(得分:2)

在jasper reports 4.0.2中,您只需编辑jrxml并将rotation属性添加到jr:barbecue元素。

<jr:barbecue xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" type="Code128" drawText="true" checksumRequired="true" rotation="Right">

可能的值是任何有效的net.sf.jasperreports.engine.type.RotationEnum

答案 1 :(得分:1)

尝试:

public BufferedImage rotate90DX(BufferedImage bi)
    {
        int width = bi.getWidth();
        int height = bi.getHeight();

        BufferedImage biFlip = new BufferedImage(height, width, bi.getType());

        for(int i=0; i<width; i++)
            for(int j=0; j<height; j++)
                biFlip.setRGB(height-1-j, width-1-i, bi.getRGB(i, j));

        return biFlip;
    }

发现于:

http://snippets.dzone.com/posts/show/2936