image quality

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
rahul

image quality

Post by rahul » Sun Apr 10, 2005 3:47 pm

Hi all,

I want improve the quality of image generating by jfrrechart api.
How can we make charts with more clarity.

Thanks ion advance.

Tzaphkiel
Posts: 44
Joined: Tue May 13, 2003 1:37 pm
Location: Belgium

Post by Tzaphkiel » Mon Apr 11, 2005 10:02 am

Hi,

Do a search on the phorum for PNG-Encoder Performance and with my nick or use this: My solution; there is a code I wrote talking about image ouput at high resolution (that take into account resolution)

Be careful at the last post also ! (windows bug...)

This code is easily adapted for other output types

Regards
Tzaphkiel
Image

Rahul Gupta
Posts: 11
Joined: Wed Apr 20, 2005 1:55 pm
Location: India

image quality

Post by Rahul Gupta » Wed Apr 20, 2005 2:05 pm

Hi Tzaphkiel,

I tried ur PNGEncoder to improve image quality but this is not giving me better result than earlier. and i coun't find out where i'm wrong.
Can u provide me some more information about rendering images.

Thanks in advance..
Rahul K. Gupta

Tzaphkiel
Posts: 44
Joined: Tue May 13, 2003 1:37 pm
Location: Belgium

Post by Tzaphkiel » Wed Apr 20, 2005 3:33 pm

hi Rahul Gupta,

:?: Have you added the getHighResImage in the class you use to create your images ?
:?: Have you changed the dpi (resolution) ?
:arrow: Your screen res is 96dpi, normal press quality is 300dpi and very high quality is 600dpi usualy...

:?: What is your problem exactly ?
:?: Do you have a small image as output ?
:?: Do you have a large one but of poor quality ?

The principle used is the following:
You create a chart with JFreeChart, display it if you want (not required), get a hold of the _chart variable containing the JFreeChart and apply the getHighResImage to it...
What it does is create an image of the right scale (dpi*size) and asks the chart to redraw itself inside that space...
The highRes image is than scalled according to dpis and outputed in an image file using ImageIO.

I hope this helps but not knowing your problem exactly, I cannot do more... :?
Regards
Tzaphkiel
Image

Guest

Post by Guest » Thu Apr 21, 2005 12:42 pm

Hi Tzaphkiel,

I have added the getHighResImage in the class you use to create images and changed the dpi (resolution) 300.
My screen res is 96dpi.
But I coudn't understand why u r using dpi instead of ppi?
My image size is large enough.

Thanks a lot..

rahul

Post by rahul » Thu Apr 21, 2005 12:44 pm

Hi Tzaphkiel,

I have added the getHighResImage in the class you use to create images and changed the dpi (resolution) 300.
My screen res is 96dpi.
But I coudn't understand why u r using dpi instead of ppi?
My image size is large enough.

Thanks a lot..

Tzaphkiel
Posts: 44
Joined: Tue May 13, 2003 1:37 pm
Location: Belgium

Post by Tzaphkiel » Thu Apr 21, 2005 3:08 pm

Hi

Well just because I happen to use dpis and I could not find the ppi conversion at the moment of coding...

Have a nice day
Tzaphkiel
Image

Guest

?

Post by Guest » Mon Sep 26, 2005 6:18 pm

Hi Tzaphkiel

I was looking at the post you refer to. Which files do you actually add this to? How do I get the _chart from the image. My code so far looks like this:

Code: Select all

         CategoryAxis categoryAxis = new CategoryAxis ("category");
         ValueAxis valueAxis = new NumberAxis ("values");
         StackedBarRenderer renderer = new StackedBarRenderer();

         CategoryPlot cPlot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer);
         categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_90);
         
         cPlot.setForegroundAlpha(0.8F);
         JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, cPlot, true);
         
         chart.setBackgroundPaint(java.awt.Color.white);
         try
         {
            chart.setTitle ("PNG quality test");
         }
         catch (Exception e)
         {
            chart.setTitle ("error");
         }

         //chart.removeLegend ();
         //  Write the chart image to the temporary directory
         ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
         BufferedImage image = chart.createBufferedImage(2000, 1600, 500, 400, null);
         
         //ChartUtilities.saveChartAsJPEG ("chart.jpg", 1.0f, chart, 500, 300);
         
         byte[] encoded = null;
         EncoderUtil encoder = new EncoderUtil();
         
         try
         {
            encoded = encoder.encode (image, ImageFormat.PNG, 1.0f);
         }
         catch (Exception e)
         {
            //error
            System.out.println("there was an error making the image byte array\n\n");
         }
Any pointers would be greatly appreciated

Tzaphkiel
Posts: 44
Joined: Tue May 13, 2003 1:37 pm
Location: Belgium

Post by Tzaphkiel » Tue Sep 27, 2005 8:15 am

Hi,

I'm not quite sure where you stand at the moment and what you've actually already coded...

My _chart object is actually an instance of JFreeChart class.

You must get the BufferedImage from the _chart and not the contrary!

Your line with BufferedImage gets the image buffer from the chart but doesn't account for the dpi you wish to encode the image in and does not take the screen resolution into account either! - that's where the getHighResChartImage() method comes into play! I don't remember well but this method is placed inside one of my own classes!

If saving the image as a png with resolution encoded into it (and doing so at higher than screen resolution) is your reason to live... :P I sugest you do something like this:

1) add the getHighResChartImage() method to your code/class using the chart instead of _chart object.
2) see my previous post for the code to write the png to file with resolution
3) your part starting at the byte[] encoded = null ... to the end should be replaced or adapted with the code mentionned in the above mentionned post!

Look at the end of page one of this above post and at page two for the constants values and the way to get the screen resolution...

If this doesn't help, send me a PM and i'll give you an email address where you can send me your complete code... to work with !

Regards
Tzaphkiel
Image

lucas luky
Posts: 14
Joined: Wed Sep 12, 2007 7:05 pm

Post by lucas luky » Tue Sep 18, 2007 9:37 pm

what if i want to put the image inside a pdf???

i tried the "saveChartToPDF" solution:

Code: Select all

/**
     * Save chart as PDF file. Requires iText library.
     *
     * @param chart JFreeChart to save.
     * @param fileName Name of file to save chart in.
     * @param width Width of chart graphic.
     * @param height Height of chart graphic.
     * @throws Exception if failed.
     * @see <a href="http://www.lowagie.com/iText">iText</a>
     */
    public static void saveChartToPDF(JFreeChart chart, String fileName, int width, int height) throws Exception {
        if (chart != null) {
            BufferedOutputStream out = null; 
            try {
                out = new BufferedOutputStream(new FileOutputStream(fileName));
                   
                //convert chart to PDF with iText:
                Rectangle pagesize = new Rectangle(width, height);
//                com.lowagie.text.Document document = new com.lowagie.text.Document(pagesize, 50.0, 50.0, 50.0, 50.0);
                com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.A4);
                try {
                    PdfWriter writer = PdfWriter.getInstance(document, out);
                    document.addAuthor("JFreeChart");
                    document.open();
           
                    PdfContentByte cb = writer.getDirectContent();
                    PdfTemplate tp = cb.createTemplate(width, height);
                    Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper());
           
                    Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);
                    chart.draw(g2, r2D, null);
                    g2.dispose();
                    cb.addTemplate(tp, 0, 0);
                } finally {
                    document.close();
                }
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        }//else: input values not availabel
    }//saveChartToPDF() 
but it doest fit to my needs, since i have a .jasper template and just want to pass the image to it.

any solution?

Tzaphkiel
Posts: 44
Joined: Tue May 13, 2003 1:37 pm
Location: Belgium

Post by Tzaphkiel » Wed Sep 19, 2007 6:57 am

Hi,

whilst I've never used Jasper, what about using the methods described above for the creation of a high quality image and save it to a temporary directory/file and pass it's resulting path to the .jasper parser ?

If the .jasper behaves like a jsp file, you could use also a static reference in the jasper file pointing to a server cgi, servlet, ... thats streams back the image ?

i.e.: in .jasper
<incImageTag src="domain/myImgGenerator?params=..." />

and myImgGenerator would be a servlet generating the image from the parameters and streaming it back with mime-type: image/jpg or image/png, ...

hoping this helps...
Tzaphkiel
Image

lucas luky
Posts: 14
Joined: Wed Sep 12, 2007 7:05 pm

Post by lucas luky » Wed Sep 19, 2007 8:13 pm

hi tza,

thanks for your reply, i finally got the solution.

http://www.jfree.org/phpBB2/viewtopic.php?t=616

Locked