• 使用graphics2D给图片上画字符



    //读取图片
    BufferedImage frontImage = ImageIO.read(name)); Graphics2D frontG = frontImage.createGraphics(); frontG.dispose(); frontImage.flush();
    //给图片画数据,eCardForm为图片中fields的模板类
    private void setFormInformation(eCardForm eForm, Graphics2D g, DataSet ds)throws IOException{ Font plainFont = new Font(eForm.getFontType(), Font.LAYOUT_LEFT_TO_RIGHT, eForm.getFontSize()); String drawString = null ; if(TypeChecker.isTrue(eForm.isConstant())){ drawString = eForm.getValue().trim(); } else{ try{ drawString = ds.getString(eForm.getValue()).trim(); } catch(Exception e){ return ; } } //check value if(TypeChecker.isEmpty(drawString)){ return ; } if(TypeChecker.isTrue(eForm.getImage()) ) { if(TypeChecker.isTrue(drawString)){ String eCardColor = ds.getString("CARDCOLOR"); String MedpassLogo = eCardXMLConfigManager.geteCardXMLConfigManager().getRealPath()+ "/com."+suffix ; if(!TypeChecker.isEmpty(eCardColor) && eCardColor.equalsIgnoreCase("eCARDCOLOR.Red")){ MedpassLogo = eCardXMLConfigManager.geteCardXMLConfigManager().getRealPath()+ "/com."+suffix ; } Image imagewritted = ImageIO.read(new File(MedpassLogo)); g.drawImage(imagewritted,eForm.getxPosition(), eForm.getyPosition(), eForm.getWidth(), eForm.getHeight(), null, null); } return ; } //color g.setColor(Color.BLACK); if(TypeChecker.isEmpty(eForm.getColor())){ if(eForm.getColor().equals("red")){ g.setColor(Color.BLACK); } else if(eForm.getColor().equals("blue")){ g.setColor(Color.BLUE); } else if(eForm.getColor().equals("gray")){ g.setColor(Color.GRAY); } else if(eForm.getColor().equals("yellow")){ g.setColor(Color.YELLOW); } else if(eForm.getColor().equals("dark_gray")){ g.setColor(Color.DARK_GRAY); } else if(eForm.getColor().equals("pink")){ g.setColor(Color.PINK); } } AttributedString as = new AttributedString(drawString); as.addAttribute(TextAttribute.FONT, plainFont);
      //设置加黑加粗属性weight,一直无法生效,没找到原因。
    if(TypeChecker.isTrue(eForm.isBold())){ g.setFont(plainFont); Font boldFont = new Font(eForm.getFontType(), Font.BOLD, eForm.getFontSize()); as.addAttribute(TextAttribute.FONT, boldFont); as.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_LIGHT); //as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); } if(TypeChecker.isTrue(eForm.isAlignRight())){ as.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL); } else{ as.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_LTR); } if(TypeChecker.isTrue(eForm.isUnderLine())){ as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); } //as.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON, 18, 25); int xPos = eForm.getxPosition();
    //左右对齐
    if(TypeChecker.isTrue(eForm.isAlignRight())){ g.setFont(plainFont); FontMetrics fm = g.getFontMetrics(); Rectangle2D rc = fm.getStringBounds(drawString.trim(), g); xPos = eForm.getxPosition() - (int)(rc.getWidth()); if(xPos < 0 ){ xPos = 0; } }
    //居中
    if(TypeChecker.isTrue(eForm.isAlignCenter())){ g.setFont(plainFont); FontMetrics fm = g.getFontMetrics(); Rectangle2D rc = fm.getStringBounds(drawString.trim(), g); xPos = Math.round((1130-(int)(rc.getWidth()))/2); if(xPos < 0 ){ xPos = 0; } } int yxPos = eForm.getyPosition(); g.drawString(as.getIterator(), xPos, yxPos); }

    此方法主要是往图片的相应位置画数据。

  • 相关阅读:
    (六)知识蒸馏
    tensorflow(三十一):数据分割与K折交叉验证
    📚面试题 1 (46题)
    🍖drf 路由组件
    🍖drf 视图组件
    🍖drf 请求与响应
    🍖drf 序列化组件
    🍖DRF框架入门介绍
    如何在大学里脱颖而出(其一)
    reshape()改变数组的形状
  • 原文地址:https://www.cnblogs.com/jingRegina/p/5421966.html
Copyright © 2020-2023  润新知