Spring MVC 文字轉圖片到img標籤

前端引入一個動態圖片,後臺讀取數據庫的文字生成並返回

<img src="http://localhost:8080/get" />

後臺代碼:

工具類

package com.example.demo.util;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Created with IntelliJ IDEA.
 * Project:springboothelloworld
 * User: Bluce Young
 * Date: 2019/12/20 9:45
 * Description: www.teacheryoung.com
 */
public class ImageUtil {

    /**
     * 字符串轉成BufferedImage
     * @param width
     * @param height
     * @param str
     * @return
     */
    public BufferedImage stringToImage(int width,int height,String str){
        BufferedImage bi = new BufferedImage
                (width,height,BufferedImage.TYPE_INT_RGB);//INT精確度達到一定,RGB三原色,高度70,寬度150

        //得到它的繪製環境(這張圖片的筆)
        Graphics2D g2 = (Graphics2D) bi.getGraphics();

        g2.fillRect(0,0,width,height);//填充一個矩形 左上角座標(0,0),寬70,高150;填充整張圖片
        //設置顏色
        g2.setColor(Color.WHITE);
        g2.fillRect(0,0,width,height);//填充整張圖片(其實就是設置背景顏色)

        g2.setColor(Color.RED);
        g2.drawRect(0,0,width-1,height-1); //畫邊框

        g2.setFont(new Font("Consolas", Font.PLAIN,18)); //設置字體:字體、字號、大小
        g2.setColor(Color.BLACK);//設置背景顏色

        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);//設置抗鋸齒
        g2.setPaint(new Color(0, 0, 0, 64));//陰影顏色
        g2.drawString(str,3,26);//先繪製陰影
        g2.setPaint(Color.BLACK);//正文顏色
        //g2.drawString(txt, x, y);//用正文顏色覆蓋上去
        g2.drawString(str,3,26); //向圖片上寫字符串

        return bi;
    }

    /**
     * 輸出一個圖像到httpResponse流
     * @param bi
     * @param httpServletResponse
     * @throws IOException
     */
    public void writeImageToResponse(BufferedImage bi, HttpServletResponse httpServletResponse) throws IOException {
        ByteArrayOutputStream stream=new ByteArrayOutputStream();
        ImageIO.write(bi,"JPEG",stream);
        byte[] img =stream.toByteArray();
        stream.close();
        httpServletResponse.setContentType("image/png");
        OutputStream os = httpServletResponse.getOutputStream();
        os.write(img);
        os.flush();
        os.close();
    }
}

控制器

package com.example.demo.controller;

import com.example.demo.util.ImageUtil;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Project:springboothelloworld
 * User: Bluce Young
 * Date: 2019/11/29 16:37
 * Description: www.teacheryoung.com
 */
@RestController
public class MainController {

    @RequestMapping(value = "/get",produces = MediaType.IMAGE_JPEG_VALUE)
    public String test(HttpServletResponse httpServletResponse) throws IOException {
        ImageUtil imageUtil=new ImageUtil();
        String str="select hahah fewff -name -p -ss";//這裏讀取數據庫
        BufferedImage bufferedImage = imageUtil.stringToImage(777, 41, str);
        imageUtil.writeImageToResponse(bufferedImage,httpServletResponse);
        return "success";
    }
}

 

發佈了27 篇原創文章 · 獲贊 22 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章