It's not a secret anymore!
The moose likes Other Open Source Projects and the fly likes How to create a jpeg image using jasperreport? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "How to create a jpeg image using jasperreport?" Watch "How to create a jpeg image using jasperreport?" New topic

How to create a jpeg image using jasperreport?

garfild Baram
Ranch Hand

Joined: Mar 24, 2003
Posts: 60
Does anyone knows how to create a jpeg image using jasperreport object?


garfild Baram
Ranch Hand

Joined: Mar 24, 2003
Posts: 60
I have found the way and it works great.

I get the image by using:

JasperPrint print = JasperFillManager.fillReport(Compiled_file, parameters, new JRTableModelDataSource(new JRStickerDataSource(stickers,0)));
Image image = JasperPrintManager.printPageToImage(print, 0, 2.0f);

private void createJpeg(Image image,String imageName){
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
// determine thumbnail size from WIDTH and HEIGHT
int thumbWidth = Integer.parseInt("90");// size in pixels
int thumbHeight = Integer.parseInt("80");//size in pixles
double thumbRatio = (double)thumbWidth / (double)thumbHeight;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double)imageWidth / (double)imageHeight;
if (thumbRatio < imageRatio) {
thumbHeight = (int)(thumbWidth / imageRatio);
} else {
thumbWidth = (int)(thumbHeight * imageRatio);
// draw original image to thumbnail image object and
// scale it to the new size on-the-fly
BufferedImage thumbImage = new BufferedImage(thumbWidth,thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
// save thumbnail image to OUTFILE
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("C:\\" +imageName + ".jpg"));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
param.setQuality(1.0f, false); // Best quality - 1.0f, Lowest quality - 0.0f
catch(Exception ex){
logger.error("Jasper Error:\n"
+"Check if the jrxml file is the right one or check that the parameters names idetical.\n"
+"System message - " + ex.getMessage());

Good luck

I agree. Here's the link:
subject: How to create a jpeg image using jasperreport?
jQuery in Action, 3rd edition