java 后台把数据以图片的形式传到前台

1:获取数据放map之后,调用WaterImage 

if (recordMap != null) {
    WaterImage waterImage = new WaterImage();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        ImageIO.write(waterImage.createImage(recordMap), "jpg", out);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return out.toByteArray();
}
2:createImage方法

package com.rjcloud.util;

import com.rjcloud.api.entity.*;
import gui.ava.html.image.generator.HtmlImageGenerator;
import org.apache.commons.collections.map.HashedMap;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Map;


public class WaterImage {
    private int imgWidth = 200;

    private int imgHeight = 20;

    private int size = 14;

    public void setImgHeight(int imgHeight) {
        this.imgHeight = imgHeight;
    }

    public int getImgWidth() {
        return this.imgWidth;
    }

    public void setImgWidth(int imgWidth) {
        this.imgWidth = imgWidth;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    String getShowStr(String str){
        return str==null?"":str;
    }
    public BufferedImage createImage(Object obj) {

        HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
        StringBuffer stringBuffer=new StringBuffer();


        //把值放在html里面
            Map map= (Map) obj;
            // stringBuffer.append("
"+entity.getCf_cfmc()+"

"); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); stringBuffer.append( "
" + "
" + "
" + "
" + "
" + "
"+""+"
" + "
" + "
" + "
" + " " ); int i=1; for(Object key2 : map.keySet()){ if(i%2!=0&&i!=map.keySet().size()) { stringBuffer.append("" + ""); }else if(i%2!=0&&i==map.keySet().size()){ stringBuffer.append("" + ""); }else{ stringBuffer.append("" + ""); } i++; } stringBuffer.append("
" + key2 + "" + map.get(key2) + "
" + key2 + "" + map.get(key2) + "
" + key2 + "" + map.get(key2) + "
" + "
" + "
" + "
" + "
" + "
" + "
" + "
"); } // stringBuffer.append(""); // stringBuffer.append(""); imageGenerator.loadHtml(stringBuffer.toString()); return imageGenerator.getBufferedImage(); } public static void main(String[] args) { HtmlImageGenerator imageGenerator = new HtmlImageGenerator(); imageGenerator.loadHtml("Hello World! Please goto Google."); imageGenerator.saveAsImage("hello-world.png"); imageGenerator.saveAsHtmlWithMap("hello-world.html", "hello-world.png"); // WaterImage create = new WaterImage(); // System.out.println(create.create("中华人民共和国万岁中华人民共和国万岁中华人民共和国万岁中华人民共和国万岁!", "c:/", 500, 38)); } }
3:把返回的byte处理
response.setCharacterEncoding("UTF-8");
response.setContentType("image/gif");
try {
    OutputStream out = response.getOutputStream();

    byte[] bytes = cmsService.getCreditImg(id,tableid,"","");
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    BufferedImage image = ImageIO.read(in);
    int h = image.getHeight();
    int w=image.getWidth();

    BufferedImage inputbig = new BufferedImage(w, h,BufferedImage.TYPE_INT_BGR);
    inputbig.getGraphics().drawImage(image, 0, 0, w, h, null); //画图

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(baos);

    ImageIO.write(inputbig, "jpg", imageOutputStream);
    out.write(baos.toByteArray());
    out.flush();
} catch (Exception e) {
    e.printStackTrace();
}

 
  

你可能感兴趣的:(JAVA)