Example to explain the servlet image processing

Source: Internet
Author: User

When we're working with data, we sometimes want to be able to use visual representations of images, here is a clever way to quickly and easily implement some simple graphics (not called image), such as bar, we do not have to use Java to generate and display the image, (Java generation image is very slow), we can do this, Use a drawing tool to make a small picture of what you need, and then add it in real time based on the amount of data you're dealing with, and you'll get the legend you want to represent. For example, we get a set of data in the database, we find the largest one, according to the ratio of the length of the label, the other data graphics can be compared to it, the length, so that a simple bar chart out. But sometimes some simple graphics can't solve the situation we actually encounter, for example, the graph can not be used in this way, then we need to generate a Java image, perhaps we have used the applet such a program, if the traffic is small, and real time is very special (such as stock system), you must use it. But in fact, most of our web programs have front and rear points, front view, background maintenance. This allows us to generate image files dynamically and periodically in the background with the servlet, and the front desk just to view the static picture, which is more than you use the applet to dynamically produce the image of how many times faster, because the applet to dynamically produce images, there are two of places very time-consuming, one is the database query time, The second is that the applet itself generates the image is very slow. Here's a simple example of how to generate and write an image file, and this example focuses on how to write the image file, believing that a friend who has written the applet will produce a more beautiful image.

Package test;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.io.*;
Import java.util.*;
Import Java.awt.image.BufferedImage;
Import com.sun.image.codec.jpeg.*;
Import java.awt.image.*;
Import java.awt.*;
public class Servlet2 extends HttpServlet {
public void init (ServletConfig config) throws servletexception {
Super.init (config);
}
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
String sfilename = "E:/temp/name.jpg";
try{
FileOutputStream fos = new
FileOutputStream (sFileName);
BufferedImage myimage = new BufferedImage (225,BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics g = myimage.getgraphics ();
G.setcolor (Color.White);
G.fillrect (0,0,225,225);
G.setcolor (Color.Black);
g.DrawString ("Finance Balance Summary", 40, 15);
g.DrawString ("Primary", 90, 30);
G.setcolor (Color.darkgray);
&g.fillrect (15,193,7,7);
G.setcolor (Color.Black);
g.DrawString ("% operating", 25, 200);
G.setcolor (Color.yellow);
G.fillrect (130,193,7,7);
G.setcolor (Color.Black);
g.DrawString ("% Term", 140, 200);
G.setcolor (Color.lightgray);
G.fillrect (15,213,7,7);
G.setcolor (Color.Black);
g.DrawString ("% mortgage", 25, 220);
G.setcolor (Color.green);
G.fillrect (130,213,7,7);
G.setcolor (Color.Black);
g.DrawString ("% Lease", 140, 220);
JPEGImageEncoder jpg =
Jpegcodec.createjpegencoder (FOS);
Jpg.encode (MyImage);
}catch (Exception e) {
String ExceptionThrown = e.tostring ();
String sourceofexception = "Method";
System.out.println ("origional Exception thrown:" +
ExceptionThrown + '
' + '
');
System.out.println ("origional sourceofexception:" +
Sourceofexception + '
' + '
');
}//Catchstatementend
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.