Jump to content

JRGraphics2DExporter image quality


broschb

Recommended Posts

I have an application in which i am using the JRGraphics2DExporter to export the graphics object of the report. This works and I can see my report, but it appears that the quality of the image returned is very low. The text is pixelated and grainy looking, and when i print it is even worse. Is there anyway to improve the quality. I am using the following to export the Graphics2D Object.

 

 

 

Code:


BufferedImage image = new BufferedImage(
width,
height, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D graphics = image.createGraphics();

try {
JRGraphics2DExporter exporter = new JRGraphics2DExporter();
exporter.setParameter(
JRGraphics2DExporterParameter.GRAPHICS_2D,
(Graphics2D) graphics);
exporter.setParameter(JRExporterParameter.JASPER_PRINT,
ivrPrint);
exporter.setParameter(JRExporterParameter.PAGE_INDEX, i);
exporter.exportReport();

} catch (JRException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Link to comment
Share on other sites

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

Since you BufferedImage is dynamically generated and its image type is unknown, the JasperReports engine will somehow compromise its quality.

However, the engine does preserver the quality of the image whose type is known. So saving the bufferedimage temporarily to a known format might solve the issue, this can be achieved as:

***************************************

ByteArrayOutputStream baos = new ByteArrayOutputStream();

ImageIO.write(image, "png", baos);

ByteArrayInputStream inputStream = new ByteArrayInputStream(baos.toByteArray());

***************************************

Then you wrap up the inputStream and feed it to the report.

 

Michael

Link to comment
Share on other sites

Thanks for the suggestion, I did the following below and received the same results, is this what you were referring to?

 

 

Code:

BufferedImage image = new BufferedImage(
width,
height, BufferedImage.TYPE_INT_RGB«»);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
ByteArrayInputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
Graphics2D graphics = image.createGraphics();
try {
JRGraphics2DExporter exporter = new JRGraphics2DExporter();
exporter.setParameter(
JRGraphics2DExporterParameter.GRAPHICS_2D,
(Graphics2D) graphics);
exporter.setParameter(JRGraphics2DExporterParameter.INPUT_STREAM,inputStream);
exporter.setParameter(JRExporterParameter.JASPER_PRINT,
ivrPrint);
exporter.setParameter(JRExporterParameter.PAGE_INDEX, i);
exporter.exportReport();

} catch (JRException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Link to comment
Share on other sites

It seems with your code you are still using your original image object (with the image type unspecified) to create that Graphics2D. Try creating the Graphics2D from the inputStream you have obtained:

 

*************************************************

Graphics2D graphics =

ImageIO.read(inputStream).createGraphics();

*************************************************

 

You probably don't have to worry about the JRGraphics2DExporterParameter.INPUT_STREAM if you have gone through the above code.

 

Cheers,

Michael

Link to comment
Share on other sites

  • 7 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...