Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to display Buffered Image object in jsp

 
Susmitha Metlapalli
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have an image of type BufferedImage. I have used <img> tag of html to display the same in jsp. Nothing is getting displayed...

what is the way to print / display a BufferedImage object in jsp...

Thanks in advance,
Susmitha.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Susmitha Metlapalli:
I have used <img> tag of html to display the same in jsp. Nothing is getting displayed...

How have you used the <img> tag to display the image? Without details we can only guess. My guess is that your image doesn't have enough yellow.

Seriously, your <img> tag should reference the URL that will serve the image. And that should not be a JSP -- the image data should be streamed by a servlet.
 
Susmitha Metlapalli
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear Bibeault for the reply...

I have a java class method which returns a BufferdImage Object. Here is the code.

package chart;
import java.io.*;
import java.awt.*;
import java.util.*;
import java.awt.image.*;
import javax.servlet.http.*;
import java.io.File;
import java.io.IOException;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JFrame;

import org.jfree.data.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.urls.*;
import org.jfree.chart.entity.*;

import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.util.ShapeUtilities;
import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartUtilities;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.*;
import org.jfree.ui.*;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;

public class XYChartExample2
{
BufferedImage chartImage;

public XYChartExample2() {
}

private DefaultCategoryDataset getDataset() {
// categories...
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
defaultcategorydataset.addValue(0.0D, "Series 1", "Jan");
defaultcategorydataset.addValue(2D, "Series 1", "Feb");
defaultcategorydataset.addValue(1.0D, "Series 1", "Mar");
defaultcategorydataset.addValue(3D, "Series 1", "Apr");
defaultcategorydataset.addValue(5D, "Series 1", "May");
defaultcategorydataset.addValue(7D, "Series 1", "Jun");

return defaultcategorydataset;
}

public BufferedImage getChartViewer() {
DefaultCategoryDataset xydataset = getDataset();

JFreeChart jfreechart = ChartFactory.createLineChart("Chart", "Month", "Values", xydataset, PlotOrientation.VERTICAL, false, false, false);
jfreechart.setBackgroundPaint(Color.cyan);
CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();

categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
categoryplot.setDomainGridlinePaint(Color.white);
categoryplot.setRangeGridlinePaint(Color.white);

LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)categoryplot.getRenderer();
lineandshaperenderer.setSeriesShapesVisible(0, true);

lineandshaperenderer.setSeriesShape(0, ShapeUtilities.createDiamond(4F));
lineandshaperenderer.setDrawOutlines(true);
lineandshaperenderer.setUseFillPaint(true);
lineandshaperenderer.setBaseFillPaint(Color.white);

NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

// OPTIONAL CUSTOMISATION COMPLETED.

ChartRenderingInfo info = null;

try {


info = new ChartRenderingInfo(new StandardEntityCollection());
chartImage = jfreechart.createBufferedImage(550, 350, info);


}
catch (Exception e) {
// handel your exception here
}


return chartImage;
}
}


I am extracting the BufferedImage Object in jsp in the following way.

<html>
<head>
<title>XY Chart Demo</title>
</head>
<body>

<%BufferedImage chartViewer = myChart.getChartViewer();

//System.out.println("getType() " +chartViewer.getType() );
//System.out.println("getType() " +chartViewer.toString() );
%>
<Table>

<tr>
<td align=center><img src="<%=chartViewer%>" border=0 > </td>

</tr>
</table>
</body>
</html>

My work is to generate certain number of charts dynamically and then display all the charts generated at the same time in same jsp... For testing i have given some dummy values. Still need to implement extraction of values for databse and setting the dataset.

If i use servlet URL mechanism, i am able to write only one chart data to response outputstream... following is the procedure i followed.

Servlet

ChartViewer.java

/**
*
* Need to produce some chart prior to this action call in a Java bean
* Need a session attribute named "chartImage";
*
*/

package myapp.webwork.servlets;

import java.io.*;
import java.awt.image.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.keypoint.PngEncoder;

public class ChartViewer extends HttpServlet {

public void init() throws ServletException {
}

//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// get the chart from session
HttpSession session = request.getSession();
BufferedImage chartImage = (BufferedImage) session.getAttribute("chartImage");

// set the content type so the browser can see this as a picture
response.setContentType("image/png");

// send the picture
PngEncoder encoder = new PngEncoder(chartImage, false, 0, 9);
response.getOutputStream().write(encoder.pngEncode());

}

//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

//Process the HTTP Put request
public void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}

//Clean up resources
public void destroy() {
}

}

Java Class which generates a chart

XYChartEample2.java

/**
*
* JFreeChart version 0.9.20
* Called by Pie3DDemo.jsp
*
*/

package myapp.webwork.beans;

import java.io.*;
import java.awt.*;
import java.util.*;
import java.awt.image.*;
import javax.servlet.http.*;
import java.io.File;
import java.io.IOException;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JFrame;

import org.jfree.data.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.urls.*;
import org.jfree.chart.entity.*;

import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.util.ShapeUtilities;
import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartUtilities;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.*;
import org.jfree.ui.*;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;

public class XYChartExample2
{
// how many values are there in the chart
public static final int NUM_VALUES = 60;
public XYChartExample2() {
}

private DefaultCategoryDataset getDataset() {
// categories...
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
defaultcategorydataset.addValue(0.0D, "Series 1", "Jan");
defaultcategorydataset.addValue(2D, "Series 1", "Feb");
defaultcategorydataset.addValue(1.0D, "Series 1", "Mar");
defaultcategorydataset.addValue(3D, "Series 1", "Apr");
defaultcategorydataset.addValue(5D, "Series 1", "May");
defaultcategorydataset.addValue(7D, "Series 1", "Jun");

return defaultcategorydataset;
}

public String getChartViewer(HttpServletRequest request, HttpServletResponse response) {
DefaultCategoryDataset xydataset = getDataset();

JFreeChart jfreechart = ChartFactory.createLineChart("Chart", "Month", "Values", xydataset, PlotOrientation.VERTICAL, false, false, false);
jfreechart.setBackgroundPaint(Color.cyan);
CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();

categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
categoryplot.setDomainGridlinePaint(Color.white);
categoryplot.setRangeGridlinePaint(Color.white);

LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)categoryplot.getRenderer();
lineandshaperenderer.setSeriesShapesVisible(0, true);
// lineandshaperenderer.setSeriesShapesVisible(1, false);
// lineandshaperenderer.setSeriesShapesVisible(2, true);
// lineandshaperenderer.setSeriesLinesVisible(2, false);
lineandshaperenderer.setSeriesShape(0, ShapeUtilities.createDiamond(4F));
lineandshaperenderer.setDrawOutlines(true);
lineandshaperenderer.setUseFillPaint(true);
lineandshaperenderer.setBaseFillPaint(Color.white);
//lineandshaperenderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
// categoryplot.setRenderer(lineandshaperenderer);

NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

// OPTIONAL CUSTOMISATION COMPLETED.

ChartRenderingInfo info = null;
HttpSession session = request.getSession();
try {

//Create RenderingInfo object
response.setContentType("text/html");
info = new ChartRenderingInfo(new StandardEntityCollection());
BufferedImage chartImage = jfreechart.createBufferedImage(550, 350, info);

// putting chart as BufferedImage in session,
// thus making it available for the image reading action Action.
session.setAttribute("chartImage", chartImage);

// PrintWriter writer = new PrintWriter(response.getWriter());
// ChartUtilities.writeImageMap(writer, "imageMap", info, true);
// writer.flush();

}
catch (Exception e) {
// handel your exception here
}

String pathInfo = "http://";
pathInfo += request.getServerName();
int port = request.getServerPort();
pathInfo += ":"+String.valueOf(port);
pathInfo += request.getContextPath();
String chartViewer = pathInfo + "/servlet/ChartViewer";
return chartViewer;
}
}


then in jsp following to display the chart.

<%String chartViewer = myChart.getChartViewer(request, response);%>
<Table>

<tr>
<td align=center><img src="<%=chartViewer%>" border=0 usemap="#imageMap"> </td>

Please suggest me how to proceed further inorder to send back to jsp todisplay several charts generated in java class...

Thanks in advance,
susmitha.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<img src="<%=chartViewer%>" border=0 usemap="#imageMap">

Remember that JSP just sends an HTML page to the browser. What does the browser expect for the src attribute of the image tag?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's some code that saves a BufferedImage into a file. That should be helpful for implementing Bear's suggestion.
 
Susmitha Metlapalli
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Ulf Dittmer and Bear Bibeault

for your valuable suggestions... I am able to save the BufferedImage and use that URL for displaying the respective chart...
 
vaji
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friend(Susmitha),

I am also wanted to do it the same way!
Can you share your code here which is used to convert the buffered image to url and show it in html

so that i can use it

Regards
Vaji
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this help?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"vaji vaji",
Please check your private messages.
-DOM
 
farhath farook
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
how can we display if it is Inputstream
i am sending the image as --

OutputStream os = response.getOutputStream();
while ((read = pic.read(bytes)) != -1){
os.write(bytes, 0, read);
}
RequestDispatcher view = request.getRequestDispatcher("view.jsp");
view.forward(request, response);
from a servlet .
and i was it to be displayed in "view.jsp" how can i do it.
please help
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should direct the OutputStream to a file, and then you can proceed as described above.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
farhath farook
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
can you please tell me how to do it. i mean about the way of calling it in the JSP. Because i am new to JSP
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic