Home Backend Development PHP Tutorial JSP技巧:发送动态图像_PHP

JSP技巧:发送动态图像_PHP

Jun 01, 2016 pm 12:35 PM
dynamic send image Skill draw

页面(或者servlet)中发送动态产生的图像?这篇技巧告诉你如何做。要运行这里的代码,你需要一个Tomcat或者其他支持JSP 1.1的web服务器。

  当一个web页面带有image/jpeg (或者其他的图像格式)的MIME类型被发送时,你的浏览器将那个返回结果当作一个图像,然后浏览器显示图像,作为页面的一部分或者完全作为图像自身。要为你的jsp页面设置MIME类型,你需要设置页面的contentType属性:
  

  然后你需要创建一个BufferedImage绘制你的动态图像:
  BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

  创建完一个BufferedImage后,你需要得到图形环境进行绘制,一个Graphics或者Graphics2D对象:

  Graphics g = image.getGraphics();
  // or
  Graphics2d g2d = image.createGraphics();

   从现在起你就可以绘制图像内容了。对图形环境绘制就会画到BufferedImage。最开始这个图像都是黑色的,因此用你希望的背景颜色填充图像是一个不错的主意,然后,当你完成图像的绘制,你需要dispose图形环境:

  g.dispose();
  // or
  g2d.dispose();


 一旦完成图像的绘制,你在response中返回那个图像。你可以使用非标准的com.sun.image.codec.jpeg包中的JPEGImageEncoder类编码图像,或者如果你使用JDK1.4,你可以使用标准的ImageIO类。在使用JPEGImageEncoder时有一个技巧,你必须从ServletResponse取来ServletOutputStream而不能使用隐含的JSP输出变量out。

  ServletOutputStream sos = response.getOutputStream();
  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
  encoder.encode(image);
  // or
  ImageIO.write(image, "JPEG", out);

  这里有一个从所有的可能方案中(例如g.dispose();或者g2d.dispose();)选取的一个完整的范例.这个例子使用Graphics对象绘制一个随机的多边形,图像通过JPEGImageEncoder绘制,你可以自由设置多边形的顶点数得到更复杂的形状,换言之,有更多顶点和边。

  要运行这个范例,将从""之间的jsp代码放到一个名为image.jsp的文件中,将那个文件放到你的web服务器可以找到的地方,在使用Tomcat的情况下是ROOT目录,启动Tomcat,访问http://localhost:8080/image.jsp
   import="java.awt.*,java.awt.image.*,
  com.sun.image.codec.jpeg.*,java.util.*"
  %>  
    // Create image
  int width=200, height=200;
  BufferedImage image = new BufferedImage(width,
  height, BufferedImage.TYPE_INT_RGB);
  // Get drawing context  (代码实验室)
  Graphics g = image.getGraphics();
  // Fill background
  g.setColor(Color.white);
  g.fillRect(0, 0, width, height);
  // Create random polygon
  Polygon poly = new Polygon();
  Random random = new Random();
  for (int i=0; i < 5; i++) {
  poly.addPoint(random.nextInt(width),
  random.nextInt(height));
  }
  // Fill polygon
  g.setColor(Color.cyan);
  g.fillPolygon(poly);
  // Dispose context
  g.dispose();
  // Send back image
  ServletOutputStream sos = response.getOutputStream();
  JPEGImageEncoder encoder =
  JPEGCodec.createJPEGEncoder(sos);
  encoder.encode(image);
  %> (代码实验室)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Convert VirtualBox fixed disk to dynamic disk and vice versa Convert VirtualBox fixed disk to dynamic disk and vice versa Mar 25, 2024 am 09:36 AM

When creating a virtual machine, you will be asked to select a disk type, you can select fixed disk or dynamic disk. What if you choose fixed disks and later realize you need dynamic disks, or vice versa? Good! You can convert one to the other. In this post, we will see how to convert VirtualBox fixed disk to dynamic disk and vice versa. A dynamic disk is a virtual hard disk that initially has a small size and grows in size as you store data in the virtual machine. Dynamic disks are very efficient at saving storage space because they only take up as much host storage space as needed. However, as disk capacity expands, your computer's performance may be slightly affected. Fixed disks and dynamic disks are commonly used in virtual machines

How to send files to others on TikTok? How to delete files sent to others? How to send files to others on TikTok? How to delete files sent to others? Mar 22, 2024 am 08:30 AM

On Douyin, users can not only share their life details and talents, but also interact with other users. In this process, sometimes we need to send files to other users, such as pictures, videos, etc. So, how to send files to others on Douyin? 1. How to send files to others on Douyin? 1. Open Douyin and enter the chat interface where you want to send files. 2. Click the "+" sign in the chat interface and select "File". 3. In the file options, you can choose to send pictures, videos, audio and other files. After selecting the file you want to send, click "Send". 4. Wait for the other party to accept your file. Once the other party accepts it, the file will be transferred successfully. 2. How to delete files sent to others on Douyin? 1. Open Douyin and enter the text you sent.

Win11 Tips Sharing: Skip Microsoft Account Login with One Trick Win11 Tips Sharing: Skip Microsoft Account Login with One Trick Mar 27, 2024 pm 02:57 PM

Win11 Tips Sharing: One trick to skip Microsoft account login Windows 11 is the latest operating system launched by Microsoft, with a new design style and many practical functions. However, for some users, having to log in to their Microsoft account every time they boot up the system can be a bit annoying. If you are one of them, you might as well try the following tips, which will allow you to skip logging in with a Microsoft account and enter the desktop interface directly. First, we need to create a local account in the system to log in instead of a Microsoft account. The advantage of doing this is

Can the floor plan be drawn directly in architectural ppt? Can the floor plan be drawn directly in architectural ppt? Mar 20, 2024 am 08:43 AM

ppt is widely used in many fields and work, especially in education, architecture, etc. When it comes to architectural ppt, we must first think of the presentation of some architectural drawings. If we do not use professional drawing software, can we directly draw a simple architectural plan? In fact, we can complete the operation here. Below, we will draw a relatively simple floor plan to give you an idea. I hope you can complete better floor plan drawings based on this idea. 1. First, we double-click to open the ppt software on the desktop and click to create a new presentation blank document. 2. We find Insert→Shape→Rectangle in the menu bar. 3. After drawing the rectangle, double-click the graphic and modify the fill color type. Here we can modify

A must-have for veterans: Tips and precautions for * and & in C language A must-have for veterans: Tips and precautions for * and & in C language Apr 04, 2024 am 08:21 AM

In C language, it represents a pointer, which stores the address of other variables; & represents the address operator, which returns the memory address of a variable. Tips for using pointers include defining pointers, dereferencing pointers, and ensuring that pointers point to valid addresses; tips for using address operators & include obtaining variable addresses, and returning the address of the first element of the array when obtaining the address of an array element. A practical example demonstrating the use of pointer and address operators to reverse a string.

What are the tips for novices to create forms? What are the tips for novices to create forms? Mar 21, 2024 am 09:11 AM

We often create and edit tables in excel, but as a novice who has just come into contact with the software, how to use excel to create tables is not as easy as it is for us. Below, we will conduct some drills on some steps of table creation that novices, that is, beginners, need to master. We hope it will be helpful to those in need. A sample form for beginners is shown below: Let’s see how to complete it! 1. There are two methods to create a new excel document. You can right-click the mouse on a blank location on the [Desktop] - [New] - [xls] file. You can also [Start]-[All Programs]-[Microsoft Office]-[Microsoft Excel 20**] 2. Double-click our new ex

VSCode Getting Started Guide: A must-read for beginners to quickly master usage skills! VSCode Getting Started Guide: A must-read for beginners to quickly master usage skills! Mar 26, 2024 am 08:21 AM

VSCode (Visual Studio Code) is an open source code editor developed by Microsoft. It has powerful functions and rich plug-in support, making it one of the preferred tools for developers. This article will provide an introductory guide for beginners to help them quickly master the skills of using VSCode. In this article, we will introduce how to install VSCode, basic editing operations, shortcut keys, plug-in installation, etc., and provide readers with specific code examples. 1. Install VSCode first, we need

Win11 Tricks Revealed: How to Bypass Microsoft Account Login Win11 Tricks Revealed: How to Bypass Microsoft Account Login Mar 27, 2024 pm 07:57 PM

Win11 tricks revealed: How to bypass Microsoft account login Recently, Microsoft launched a new operating system Windows11, which has attracted widespread attention. Compared with previous versions, Windows 11 has made many new adjustments in terms of interface design and functional improvements, but it has also caused some controversy. The most eye-catching point is that it forces users to log in to the system with a Microsoft account. For some users, they may be more accustomed to logging in with a local account and are unwilling to bind their personal information to a Microsoft account.

See all articles