package com.hacker; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.URLEncoder; import java.util.Date; import java.util.Locale; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.bean.Student; public class HackerServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String name=req.getParameter("name"); draw(name); } private void draw(String name) throws IOException { File file=new File("F:/McGrady.jpg"); //载入底图 BufferedImage image = ImageIO.read(file); System.out.println(image); //开始绘画 Graphics2D g = image.createGraphics(); //设置文字颜色,写文字 g.setColor(new Color(233,2,2)); g.drawString(name, 50, 50); //结束绘画 g.dispose(); //获取图片生成的字节数组 ByteArrayOutputStream array = new ByteArrayOutputStream(); ImageIO.write(image, "PNG", array); byte[] b=array.toByteArray(); //写入文件中 这里的效率可能有点低 FileOutputStream fos=new FileOutputStream(new File("F://text.jpg")); fos.write(b); fos.close(); array.close(); } }
演示:
测试一下中文:
时间: 2024-10-13 01:07:41