📄 webcamservlet.java
字号:
/** * JMF/Webcam Frame Grabber Demo * * @author S.Ritter 17.06.2002 * @version 1.0 * * ALL EXAMPLES OF CODE AND/OR COMMAND-LINE INSTRUCTIONS ARE BEING * PROVIDED BY SUN AS A COURTESY, "AS IS," AND SUN DISCLAIMS ANY AND * ALL WARRANTIES PERTAINING THERETO, INCLUDING ANY WARRANTIES OF * MERCHANTABILTY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. * SUN IS NOT LICENSING THIS EXAMPLE FOR ANY USE OTHER THAN FOR THE * EDUCATIONAL PURPOSE OF SHOWING THE FUNCTIONALITY CONTAINED * THEREIN, BY WAY OF EXAMPLE. **/import java.awt.*;import java.awt.image.BufferedImage;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import sun.awt.image.codec.JPEGImageEncoderImpl;import jmfdemo.*;/** * Servlet to deliver a frame from the webcam to a web browser, thus * providing simple remote visual monitoring. **/public class WebCamServlet extends HttpServlet { private FrameGrabber vision; /** * Initialisation code. Create a new FrameGrabber ready to get images * from our camera. **/ public void init() { String videoPropFile = getServletContext().getInitParameter("videoconfigfile"); try { if (videoPropFile != null) vision = new FrameGrabber(videoPropFile); else vision = new FrameGrabber(); vision.start(); } catch (FrameGrabberException fge) { System.out.println("Error creating frame grabber"); System.out.println(fge.getMessage()); } } /** * Handle a GET call from the client * * @param request HTTP request information from the client * @param response HTTP channel back to the client browser * @throws IOException If there is an IO problem * @throws ServletException if there is a proble with the servlet processing **/ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { /* Handle a GET in the same way as we handle a POST */ doPost(request, response); doGet(request, response); } /** * Handle a POST call from the client * * @param request HTTP request information from the client * @param response HTTP channel back to the client browser * @throws IOException If there is an IO problem * @throws ServletException if there is a proble with the servlet processing **/ public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { /* If there is no FrameGrabber quietly do nothing */ if (vision == null) return; BufferedImage bImage = null; /* Get a BufferedImage from the webcam. If this fails send an error * message to the web browser */ if ((bImage = vision.getBufferedImage()) == null) { response.setContentType("text/html"); PrintWriter pw = response.getWriter(); pw.println("<html>NULL image returned</html>"); return; } /* Set the response type to a JPEG image so the browser will interpret * the information correctly */ response.setContentType("image/jpeg"); /* Use a ServletOutputStream which is for sending binary data rather * than the usual PrintWriter */ ServletOutputStream out = null; try { out = response.getOutputStream(); } catch (IOException ioe) { /* If this fails we can only really record the information in the * servlet engine log file */ System.out.println("Unable to open an output stream to the client"); System.out.println(ioe.getMessage()); } /* Conversion from a BufferedImage to a JPEG is simple using the Sun * implementation of the com.sun.image.codec.jpeg interface. We * create a new encoder that sends the JPEG image to the * ServletOutputStream */ JPEGImageEncoderImpl encoder = new JPEGImageEncoderImpl(out); encoder.encode(bImage); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -