zoukankan      html  css  js  c++  java
  • java生成图片

    在一张图片上绘制别的图片以及文字。

    public String GenerateImage(WebCast_baseinfo base,String code,String customName,String customImg,String customId){
    Log.debug("------GenerateImage----start");
    String ret = null;
    try {
    InputStream is = new FileInputStream("/usr/share/tomcat7/webapps/worklite/promotionImage/card.jpg");
    JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
    BufferedImage buffImg=null;
    try{
    buffImg = jpegDecoder.decodeAsBufferedImage();
    }catch(Exception e){
    Log.error(this.getClass().getSimpleName()+"----"+e.getMessage());
    }
    Log.debug("------GenerateImage----Graphics");
    Graphics g = buffImg.getGraphics();
    g.setColor(new Color(255,140,0));
    double rate=1.0;
    Font f = new Font("楷体",Font.ROMAN_BASELINE,30);
    FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(f);

    g.setFont(f);

    String str="";
    if(base.getIntroduction()!=null){
    str=base.getIntroduction();
    }
    if(str.length()>16){
    String s=str.substring(0,16);
    String ss=str.substring(16);
    g.drawString(s,(640-fm.stringWidth(s))/2,326);
    //MyDrawString(s,(640-fm.stringWidth(s))/2,326,rate,g);
    g.drawString(ss,(640-fm.stringWidth(ss))/2,366);
    //MyDrawString(ss,(640-fm.stringWidth(ss))/2,366,rate,g);
    }else{
    g.drawString(str,(640-fm.stringWidth(str))/2,366);
    //MyDrawString(str,(640-fm.stringWidth(str))/2,326,rate,g);
    }
    Font f3 = new Font("楷体",Font.ROMAN_BASELINE,30);
    FontMetrics fm3 = sun.font.FontDesignMetrics.getMetrics(f3);
    g.setFont(f3);
    g.setColor(Color.white);
    String str2=base.getwName();

    if(str2.length()>16){
    String s2=str2.substring(0,16);
    String ss2=str2.substring(16);
    g.drawString(s2,(640-fm3.stringWidth(s2))/2,426);
    g.drawString(ss2,(640-fm3.stringWidth(ss2))/2,466);
    //MyDrawString(s2,(640-fm.stringWidth(s2))/2,426,rate,g);
    //g.drawString(ss,(640-fm.stringWidth(ss))/2,366);
    //MyDrawString(ss2,(640-fm.stringWidth(ss2))/2,466,rate,g);
    }else{
    g.drawString(str2,(640-fm3.stringWidth(str2))/2,446);
    //MyDrawString(str2,(640-fm.stringWidth(str2))/2,446,rate,g);
    }

    g.setColor(Color.white);
    String str3="主讲人:"+base.getSpeaker();
    if(str2.length()>16){
    g.drawString(str3,(640-fm3.stringWidth(str3))/2,526);
    }else{
    g.drawString(str3,(640-fm3.stringWidth(str3))/2,506);
    }
    g.setColor(Color.white);

    String str4=DateUtil.toString(base.getStartTime(), "yyyy年MM月dd日 HH:mm")+" 开讲";
    if(str2.length()>16){
    g.drawString(str4,(640-fm3.stringWidth(str4))/2,586);
    }else{
    g.drawString(str4,(640-fm3.stringWidth(str4))/2,566);
    }

    ImageIcon imgIcon3 = new ImageIcon(new URL(customImg),"customImg");

    Image img3 = imgIcon3.getImage();

    g.drawImage(img3,(640-80)/2,700,80,80,null);

    Font f1 = new Font("楷体",Font.BOLD,20);
    FontMetrics fm1 = sun.font.FontDesignMetrics.getMetrics(f1);
    Font f2 = new Font("楷体",Font.BOLD,24);
    FontMetrics fm2 = sun.font.FontDesignMetrics.getMetrics(f2);
    g.setFont(f1);
    g.setColor(Color.white);
    String str6=customName;
    String str5="来自 ";
    String str7=" 的邀请";
    g.drawString(str5,(640-fm1.stringWidth(str5)-fm2.stringWidth(str6)-fm1.stringWidth(str7))/2,816);
    //g.setColor(new Color(30,144,255));
    g.setFont(f2);
    g.drawString(str6,(640-fm1.stringWidth(str5)-fm2.stringWidth(str6)-fm1.stringWidth(str7))/2+fm1.stringWidth(str5),816);
    //g.setColor(Color.gray);
    g.setFont(f1);
    g.drawString(str7,(640-fm1.stringWidth(str5)-fm2.stringWidth(str6)-fm1.stringWidth(str7))/2+fm1.stringWidth(str5)+fm2.stringWidth(str6),816);
    ImageIcon imgIcon2 = new ImageIcon(new URL(code),"qrcode");

    Image img2 = imgIcon2.getImage();

    g.drawImage(img2,(640-185)/2,848,180,180,null);
    g.dispose();
    String fileName = base.getCompanyId()+"_"+customId+"_"+base.getwId()+".jpg";
    LocalFileManager tmpfileMgr = new LocalFileManager();
    File tmpFile = tmpfileMgr.getFile(FileConstants.TYPE_DATA, FileConstants.SUBTYPE_HB, fileName, true);
    OutputStream os = new FileOutputStream(tmpFile);
    JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
    en.encode(buffImg);

    is.close();

    os.close();

    IFileManager fileMgr = FileFactory.getFileManager();
    fileMgr.moveTempFile(FileConstants.TYPE_DATA, FileConstants.SUBTYPE_HB, fileName, tmpFile);

    ret = fileMgr.getHref(FileConstants.TYPE_DATA, FileConstants.SUBTYPE_HB, fileName);
    Log.debug("------GenerateImage----end");
    } catch (Exception e) {
    Log.error(this.getClass().getSimpleName()+"----"+e.getMessage());
    }
    Log.debug("------GenerateImage---ret--"+ret);
    return ret;

    }

  • 相关阅读:
    普通javaBean获取Spring托管对象
    java 线程安全问题
    MySQL之alter语句用法总结
    Maven pom.xml配置
    Srping框架初识
    win7(64位)下memcache安装时报错“ failed to install service or service already installed”
    activemq 使用
    elasticsearch plugin
    logback.xml 实例
    Intellij IDEA 插件
  • 原文地址:https://www.cnblogs.com/momo1210/p/6283854.html
Copyright © 2011-2022 走看看