java

位置:IT落伍者 >> java >> 浏览文章

java文字转成图片显示


发布日期:2021年07月28日
 
java文字转成图片显示

package rge;

import javaioIOException;

import javaioPrintWriter;

import javaio*;

import javaawt*;

import javaawtimage*;

import javaxservlet*;

import javaxservlethttp*;

import decjpeg*;

/**

* @author Liao Xue Feng

*/

public class CreateImageServlet extends HttpServlet {

protected void doGet(HttpServletRequest request HttpServletResponse response)

throws ServletException IOException

{

responsesetContentType(image/jpeg);

createImage(responsegetOutputStream());

}

private void createImage(OutputStream out) {

String text = 测试;

int width = ;

int height = textlength()/*;

BufferedImage bi = new BufferedImage(width height BufferedImageTYPE_INT_RGB);

GraphicsD g = bicreateGraphics();

// set background:

gsetBackground(ColorGREEN);

gclearRect( width height);

Font mFont = new Font(宋体FontPLAIN);//默认字体

gsetColor(new Color(IntegerparseInt()));

gsetFont(mFont);

int x=;

int y=;

int count = textlength();

int i = ;

String getone = null;

while(count >)

{

getone = textsubstring(ii+);

if(i % == )

{

y = y + ;

x = ;

}

//gdrawString(IntegertoString(i)xy);

gdrawString(getonexy);

x = x + ;

i++ ;

count;

}

// end draw:

gdispose();

biflush();

// encode:

JPEGImageEncoder encoder = JPEGCodeccreateJPEGEncoder(out);

JPEGEncodeParam param = encodergetDefaultJPEGEncodeParam(bi);

paramsetQuality(f false);

encodersetJPEGEncodeParam(param);

try {

encoderencode(bi);

}

catch(IOException ioe) {

ioeprintStackTrace();

}

}

}

indejsp页面:

<%@ page language=java import=javautil* pageEncoding=GB%>

<!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN>

<html>

<head>

<title>系统测试(From:网络大本营Http://)</title>

<meta httpequiv=pragma content=nocache>

<meta httpequiv=cachecontrol content=nocache>

<meta httpequiv=expires content=>

<meta httpequiv=keywords content=keywordkeywordkeyword>

<meta httpequiv=description content=This is my page>

<!

<link rel=stylesheet type=text/css >

>

<meta httpequiv=ContentType content=text/html; charset=gb><style type=text/css>

<!

body {

backgroundcolor: #FF;

}

>

</style></head>

<body>

<img src=servlet/CreateImageServlet>

</body>

</html>

上一篇:浅析Java语言的事件处理快捷回复

下一篇:网络编程语言——Java初级学习指南