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

时间:2022-11-15 08:14:42

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<String, Object> map= (Map<String, Object>) obj;
            // stringBuffer.append("<br /><div style='text-align: center ;font-size:18px'><b>"+entity.getCf_cfmc()+"</b></div><br />");
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

            stringBuffer.append(
                    " <div id='frame_article_l_warp' class='article_dt' style='display: block;width: 100%;" +
                            "background-image: url(../images/article_06.gif);background-repeat: repeat-y;" +
                            "background-position: 0px 0px;height: auto;!important;height: 100%;overflow: hidden;'>" +
                            "<div class='article_2_s' id='frame_list_s' style='background-image: url(../images/article_03.gif);background-repeat: no-repeat;" +
                            "background-position: 0px top;display: block;width: 100%;height: 100%;height: auto;!important;overflow: hidden;'>" +
                            "<div class='article_2_x' id='frame_list_x' style='background-image: url(../images/article_19.gif);background-repeat: no-repeat;" +
                            "background-position: 0px bottom;display: block;width: 100%;" +
                            "height: 100%;height: auto;!important;overflow: hidden;'>" +
                            "<div class='article_con_bg' id='mod_article_con' >" +

                            "            <div id='data_article_title'>" +

                            "              <div class='title' id='cms_article_title' style='line-height:100px;width:1000px;" +
                            "border-width:0 0 1px 0;text-align:center;padding-top: 20px;word-wrap: break-word;word-break:break-all;" +
                            "padding-right: 20px;padding-bottom: 0;padding-left: 20px;font-size:33px;'>"+""+"</div>" +
                            "            </div>" +

                            "            <div id='data_article_content'>" +

                            "              <div class='con' id='cms_article_content' style='display: block;font-size:14px;line-height: 1.7;text-align: left;/*(火狐)*/" +
                            "text-justify:inter-ideograph;/*(IE)*/font-size:16px;'>" +
                            "                <table width='1000px' border='0' cellspacing='0' cellpadding='0' class='x_x' style='margin-top: 15px;margin-bottom: 15px;border-collapse: collapse;'>" );
           int i=1;
            for(Object key2 : map.keySet()){
                if(i%2!=0&&i!=map.keySet().size()) {
                    stringBuffer.append("<tr><td align='center' class='d_sk' style='background-color: #D9F0FF;color: #006699;border: 1px dashed #999;" +
                            "padding: 5px;'><font style='background-color: #D9F0FF;display: block;'>" + key2 + "</font></td>" +
                            "<td style='color: #006699;border: 1px dashed #999;" +
                            "padding: 5px;'>" + map.get(key2) + "</td>");
                }else if(i%2!=0&&i==map.keySet().size()){
                    stringBuffer.append("<tr><td align='center' class='d_sk' style='background-color: #D9F0FF;color: #006699;border: 1px dashed #999;" +
                            "padding: 5px;'><font style='background-color: #D9F0FF;display: block;'>" + key2 + "</font></td>" +
                            "<td style='color: #006699;border: 1px dashed #999;" +
                            "padding: 5px;'>" + map.get(key2) + "</td></tr>");
                }else{
                    stringBuffer.append("<td align='center' class='d_sk' style='background-color: #D9F0FF;color: #006699;border: 1px dashed #999;" +
                            "padding: 5px;'><font style='background-color: #D9F0FF;display: block;'>" + key2 + "</font></td>" +
                            "<td style='color: #006699;border: 1px dashed #999;" +
                            "padding: 5px;'>" + map.get(key2) + "</td></tr>");
                }
                i++;
            }
            stringBuffer.append(" </table>" +
                                           " </div>" +
                                            " </div>" +
                                               "</div>" +
                                          "</div>" +
                                              "</div>" +
                                              " </div>" +
                                               "</div>");
        }


//        stringBuffer.append("</body>");
//        stringBuffer.append("</html>");
        imageGenerator.loadHtml(stringBuffer.toString());
        return imageGenerator.getBufferedImage();
    }
   
    public static void main(String[] args) {

        HtmlImageGenerator imageGenerator = new HtmlImageGenerator();

        imageGenerator.loadHtml("<b>Hello World!</b> Please goto <a title=\"Goto Google\" href=\"http://www.google.com\">Google</a>.");

        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();
}