|
JSP, Servlet examples and image maps (data drill down and data popup)
JSP example (JPG)
JSP example (PNG)
Servlet example
Displaying text and barcodes on the same page
Saving an image as a file
JSP example (JPG)
Please find further details on how to install the swiftbarcode_app class on our products page.
[1] Set the content type of the output. This will depend on the graphic format you choose.
contentType="image/jpeg"
[2] Import Swiftbarcode application and the graphic encoder. In this example we use the widely available Sun jpeg encoder.
import="swiftbarcode.swiftbarcode_app.*,com.sun.image.codec.jpeg.*"
[2] Make a new instance of swiftbarcode_app. The chart width and chart height are required when creating a new instance (eg 400,300).
swiftbarcode.swiftbarcode_app mybarcode= new swiftbarcode.swiftbarcode_app();
[3] Set the barcode parameters.
mybarcode.setParam("symbology","EAN13");
[5] Output the barcode. This part will depend on the type of encoder you are using.
ServletOutputStream sos = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
encoder.encode(mybarcode.getBarcode());
Putting all together the JSP code will look like this:
<html>
<body>
<%@ page session="false"%>
<%@ page contentType="image/jpeg" import="com.sun.image.codec.jpeg.*,swiftbarcode.swiftbarcode_app.*" %>
<%
swiftbarcode.swiftbarcode_app mybarcode= new swiftbarcode.swiftbarcode_app();
mybarcode.setParam("bar_code_value","5060033952504");
mybarcode.setParam("symbology","EAN13");
mybarcode.setParam("bar_code_height","80");
mybarcode.setParam("bar_code_width","2");
mybarcode.setParam("check_digit_supplied","Y");
mybarcode.setParam("font_size","18");
mybarcode.setParam("text_display","Y");
ServletOutputStream sos = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
encoder.encode(mybarcode.getBarcode());
%>
</body>
</html>
Back to top
JSP example (PNG)
This example shows how to generate PNG images, using Sun's Java Advanced Imaging (JAI) API. We suggest to use PNG rather than JPG since JPG is ideal for photographic images while PNG is ideal for charts and text. all modern browsers do support PNG, but some older browser versions might not.
<html>
<body>
<%@ page session="false"%>
<%@ page contentType="image/png" import="com.sun.media.jai.codec.*,swiftbarcode.swiftbarcode_app.*" %>
<%
swiftbarcode.swiftbarcode_app mybarcode= new swiftbarcode.swiftbarcode_app();
mybarcode.setParam("bar_code_value","5060033952504");
mybarcode.setParam("symbology","EAN13");
mybarcode.setParam("bar_code_height","80");
mybarcode.setParam("bar_code_width","2");
mybarcode.setParam("check_digit_supplied","Y");
mybarcode.setParam("font_size","18");
mybarcode.setParam("text_display","Y");
ServletOutputStream sos = response.getOutputStream();
PNGEncodeParam encParam = new PNGEncodeParam.Gray();
ImageEncoder encoder2=ImageCodec.createImageEncoder ("PNG", sos, encParam);
encoder2.encode(mybarcode.getBarcode());
%>
</body>
</html>
Back to top
Servlet example
Please find further details on how to install the swiftbarcode_app class on our products page.
[1] Import Swiftbarcode application and the graphic encoder. In this example we use the widely available Sun jpeg encoder.
import swiftbarcode.swiftbarcode_app.*; import com.sun.image.codec.jpeg.*;
[2] Make a new instance of swiftbarcode_app.
swiftbarcode.swiftbarcode_app mybarcode= new swiftbarcode.swiftbarcode_app;
[3] Set the barcode parameters.
mybarcode.setParam("symbology","EAN13");
[4] Set the content type of the output. This will depend on the graphic format you choose.
sos.setContentType("image/jpeg");
[5] Output the barcode. This part will depend on the type of encoder you are using.
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos.getOutputStream()); encoder.encode(mybarcode.getBarcode());
Putting all together the Servlet code will look like this:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import com.sun.image.codec.jpeg.*;
import swiftbarcode.swiftbarcode_app.*;
public class mybarcode extends HttpServlet {
public void doGet (HttpServletRequest req, HttpServletResponse sos)
throws ServletException, IOException
{
swiftbarcode.swiftbarcode_app mybarcode= new swiftbarcode.swiftbarcode_app();
mybarcode.setParam("bar_code_value","5060033952504");
mybarcode.setParam("symbology","EAN13");
mybarcode.setParam("bar_code_height","80");
mybarcode.setParam("bar_code_width","2");
mybarcode.setParam("check_digit_supplied","Y");
mybarcode.setParam("font_size","18");
mybarcode.setParam("text_display","Y");
sos.setContentType("image/jpeg");
JPEGImageEncoder encoder=
JPEGCodec.createJPEGEncoder(sos.getOutputStream());
encoder.encode(mybarcode.getBarcode());
}
public String getServletInfo() {
return "Swiftbarcode demo";
}
}
Back to top
Displaying text and barcodes on the same page
In many cases barcodes are to be displayed on web pages together with text and HTML. It can be difficult to generate jSP pages with mixed types. The following example suggests to use the HTML image tag.
The following example shows how to implement mixed types on one page. The first JSP script or HTML document calls the second script using the HTML image tag.
Script 1 (example_1):
<Hello this is a mixed image and text example
<IMG SRC='example_2.jsp' border=0>
Script 2 (example_2):
<html>
<body>
<%@ page session="false"%>
<%@ page contentType="image/jpeg"
import="com.sun.image.codec.jpeg.*,swiftbarcode.swiftbarcode_app.*"
%>
<%
swiftbarcode.swiftbarcode_app mybarcode= new swiftbarcode.swiftbarcode_app;
mybarcode.setParam("bar_code_value","5060033952504");
mybarcode.setParam("symbology","EAN13");
mybarcode.setParam("bar_code_height","80");
mybarcode.setParam("bar_code_width","2");
mybarcode.setParam("check_digit_supplied","Y");
mybarcode.setParam("font_size","18");
mybarcode.setParam("text_display","Y");
ServletOutputStream sos = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
encoder.encode(mybarcode.getBarcode()); %>
</body>
</html>
It is possible to pass parameters to the script generating the actual image as followed:
<IMG SRC='example_2.jsp?var1=1&var2=xyz' border=0>
Back to top
Saving an image as a file
Sometimes the image needs to be saved rather than directly being displayed. This can be the case if the generated image needs to be integrated into other documents or is part of a bigger process.
Saving images is very similar to displaying images, but we use the FileOutputStream instead.
FileOutputStream fos = new FileOutputStream("out.jpg");
JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(fos);
jpeg.encode(mybarcode.getBarcode());
fos.close();
Putting all together the JSP code will look like this:
<html>
<body>
<%@ page session="false"%>
<%@ page contentType="image/jpeg"
import="com.sun.image.codec.jpeg.*,swiftbarcode.swiftbarcode_app.*"
%>
<%
swiftbarcode.swiftbarcode_app mybarcode= new swiftbarcode.swiftbarcode_app;
mybarcode.setParam("bar_code_value","5060033952504");
mybarcode.setParam("symbology","EAN13");
mybarcode.setParam("bar_code_height","80");
mybarcode.setParam("bar_code_width","2");
mybarcode.setParam("check_digit_supplied","Y");
mybarcode.setParam("font_size","18");
mybarcode.setParam("text_display","Y");
FileOutputStream fos = new FileOutputStream("out.jpg");
JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(fos);
jpeg.encode(mybarcode.getBarcode());
fos.close();
</body>
</html>
Back to top
|