>lets say i got an image stored in the Image
> format or byte[].
Alright
>How can i make it display the
> image on the screen by taking the values in byte
> array or Image field?
which screen ru talking about ? is it a servlet which you are refering to which displays Image Data ? if tht is the case... take a look of the example code which i have posted in the below thread.
http://forum.java.sun.com/thread.jspa?threadID=5137711&messageID=9504191#9504191
Hope this might help...
REGARDS,
RaHuL
Thanks rahul,
The thing is, i am generating a chart in a servlet and setting the image in the form of a byte [] to the view bean ( which is binded to the jsp, springs framework ). The servlet would return the view bean to the jsp and in the jsp, i am suppose to print this byte array so as to give me the image..
I hope this makes sense.. pls help me ou!
> Thanks rahul,
> The thing is, i am generating a chart in a servlet
> and setting the image in the form of a byte [] to the
> view bean ( which is binded to the jsp, springs
> framework ). The servlet would return the view bean
> to the jsp and in the jsp, i am suppose to print this
> byte array so as to give me the image..
> I hope this makes sense.. pls help me ou!
Well letme see if i got tht right or not,
you are trying to call Your MODEL (Business layer / Spring Container) from a servlet and you are expressing that logic in form of chart (Image) and trying to save it as a byte array in a view bean and you want to print /display that as an image in a jsp (After Servlet fwd / redirect action) which includes other data using a ViewBean.
If this is the case...
As the forwaded JSP can include both image and Textual (hypertext too)..we can try a work around hear...Lets dedicate a Servlet which retreives byte [] from a view bean and gives us an image output. hear is an example and this could be a way.
Prior to that i'm trying to make few assumptions here....
1).The chart image which we are trying to express would of format JPEG.
2).we are trying to take help of<img> tag to display the image from the image generating servlet.
here is my approach....
ViewBean.java:
============
public class ViewBean implements serializable{
byte piechart[];
byte barchart[];
byte chart3D[];
public ViewBean(){
--
-
}
public byte[] getPieChart(){
return(this.piechart);
}
public byte[] getBarChart(){
return(this.barchart);
}
public byte[] get3DChart(){
return(this.chart3D);
}
public void setPieChart(byte piechart[]){
this.piechart = piechart;
}
public void setBarChart(byte barchart[]){
this.barchart = barchart;
}
public void set3DChart(byte chart3D[]){
this.chart3D = chart3D;
}
}
ControllerServlet.java:
=================
(This could also be an ActionClass(Ref Struts) a Backing Bean(Ref JSF) or anything which stays at the Controller Layer)
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{
--
--
/* There are few different implementations of gettingBeanFactory Resource
In,the below example i have used XmlBeanFactory Object to create an instance of (Spring) BeanFactory */
BeanFactory factory =
new XmlBeanFactory(new FileInputStream("SpringResource.xml"));
//write a Util Logic in your Implementation class using JFreeChart (or some open source chart library) and express the images by returning a byte[]
ChartService chartService =
(GreetingService) factory.getBean("chartService");
ViewBean vb = new ViewBean();
vb.setPieChart(chartService.generatePieChart(request.getParameter("<someparam>"));
vb.setBarChart(chartService.generateBarChart(request.getParameter("<someparam1>"));
vb.set3DChart(chartService.generate3DChart(request.getParameter("<someparam2>"));
chartService = null;
HttpSession session = request.getSession(false);
session.setAttribute("ViewBean",vb);
response.sendRedirect("jsp/DisplayReports.jsp");
}
DisplayReports.jsp :
================
<%@ page language="java" %>
<html>
<head>
<title>reports</title>
</head>
<body>
<h1 align="center">Pie Chart </h1>
<center><img src="ImageServlet?req=1" /></center>
<h1 align="center">Bar Chart </h1>
<center><img src="ImageServlet?req=2" /></center>
<h1 align="center">3D Chart</h1>
<center><img src="ImageServlet?req=3" /></center>
</body>
</html>
ImageServlet.java
==============
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
byte buffer[];
HttpSession session = request.getSession(false);
ViewBean vb = (ViewBean) session.getAttribute("ViewBean");
String req = request.getParameter("req");
if(req.equals("1") == true)
buffer = vb.getPieChart();
else if(req.equals("2") == true)
buffer = vb.getBarChart();
else if(req.equals("3") == true)
buffer = vb.get3DChart();
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(new ByteArrayInputStream(buffer));
BufferedImage image =decoder.decodeAsBufferedImage() ;
response.setContentType("image/jpeg");
// Send back image
ServletOutputStream sos = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
encoder.encode(image);
}
Note: Through ImageServlet is a Servlet i would categorise it under presentation layer rather to be a part of Controller and added to it all this could be easily relaced by a reporting(BI) server like JasperServer,Pentaho,Actuate................
Hope the stated implementation had given some idea to you....
However,If you want to further look into similar implementations take a look at
http://www.swiftchart.com/exampleapp.htm#e5
which i believe to be a wonderful tutor for such implementations...
However, there are many simple (Open) solutions to the stated problem.. if you are Using MyFaces along with spring... i would recommend usage of JSF Chart Tag which is very simple to use all it requires need is to write a chart Object generating methos inside our backing bean.
For further reference have a look at the below links
http://www.jroller.com/page/cagataycivici?entry=acegi_jsf_components_hit_the
http://jsf-comp.sourceforge.net/components/chartcreator/index.html
NOTE:I've tried it personally using MyFaces it was working gr8 but i had a hardtime on deploying my appln on a Portal Server(Liferay).If you find a workaround i'd be glad to know about it.
& there are many BI Open Source Server Appls that can take care of this work too.(Maintainace wud be a tough ask when we go for this)
For, the design perspective had i've been ur PM i wud have choose BI Server if it was corporate web appln on which we work on.
Hope this might be of some help :)
REGARDS,
RaHuL