Search code examples
javaimagejasper-reportsireport

Rotating 90º In a Blob Image in JasperReports


So I have a small jrxml with this:

Images

The two images are of the field type BLOB in a firebird database and to show it correctly I'm using new ByteArrayInputStream((byte[])$F{FOFU}) on the image expression.

After reading a bit I figured the only way to rotate this image is to do it programmatically on java and I have no idea how to do, even after reading some posts here and other places. Can anyone help me with this ?


Solution

  • private byte[] rotateImage(byte[] originalImageAsBytes , double radians) throws InternalException {
    ByteArrayOutputStream rotatedImageStream = null;
    
    try {
      BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(originalImageAsBytes)); // read the original image
      AffineTransform rotationTransform = new AffineTransform();
      rotationTransform.rotate(radians, originalImage.getWidth() / 2.0 , originalImage.getHeight() / 2.0);
      AffineTransformOp rotationTransformOp = 
        new AffineTransformOp(rotationTransform , AffineTransformOp.TYPE_NEAREST_NEIGHBOR); 
      BufferedImage rotatedImage = rotationTransformOp.filter(originalImage,null); 
    
      rotatedImageStream = new ByteArrayOutputStream();
      ImageIO.write(rotatedImage, "jpg" , rotatedImageStream); 
    } catch (IOException e) {
      throw new InternalException(e);
    }
    return rotatedImageStream.toByteArray();
    }
    

    and on the Jasper I'm doing

    new ByteArrayInputStream(path.to.rotateImage((byte[])$F{IMAGE}, 100.00))
    

    as the image expression. Its working.