Java Tutorial/JSP/Image Creation

Материал из Java эксперт
Перейти к: навигация, поиск

Generate JPEG image in jsp code

<%@ page contentType="image/jpeg" import="java.io.*, java.awt.*, java.awt.image.*,com.sun.image.codec.jpeg.*" %>
<%
    try {
        BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.drawLine(0, 0, 199, 199);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response.getOutputStream());
        encoder.encode(image);
    } catch (Exception e) {}
%>





Return an Image From JSP Page

index.html



<HTML>
    <HEAD>
        <TITLE>Creating Images on the Fly</TITLE>
    </HEAD>
    <BODY>
        <H1>Creating Images on the Fly</H1>
        <IMG SRC="basic.jsp">
    </BODY>
</HTML>





Use passed in parameter to draw a JPEG image

index.html



<HTML>
    <HEAD>
    </HEAD>
    <BODY>
        <DIV STYLE="POSITION:ABSOLUTE; LEFT:10; TOP:10; WIDTH:480; HEIGHT:400; 
        BACKGROUND-COLOR:BLACK;"></DIV>
    </BODY>
        <SCRIPT>
            <!--
                var outString = "basic.jsp?x1=" + 10
                    outString += "&y1=" + 10
                    outString += "&x2=" + 100
                    outString += "&y2=" + 100
                    location.href = outString
            // -->
        </SCRIPT>
</HTML>