1. //读取图片
    BufferedImage frontImage = ImageIO.read(name));
  2. Graphics2D frontG = frontImage.createGraphics();
  3. frontG.dispose();
  4. frontImage.flush();
    //给图片画数据,eCardForm为图片中fields的模板类
  5. private void setFormInformation(eCardForm eForm, Graphics2D g, DataSet ds)throws IOException{
  6. Font plainFont = new Font(eForm.getFontType(), Font.LAYOUT_LEFT_TO_RIGHT, eForm.getFontSize());
  7. String drawString = null ;
  8. if(TypeChecker.isTrue(eForm.isConstant())){
  9. drawString = eForm.getValue().trim();
  10. }
  11. else{
  12. try{
  13. drawString = ds.getString(eForm.getValue()).trim();
  14. }
  15. catch(Exception e){
  16. return ;
  17. }
  18. }
  19.  
  20. //check value
  21. if(TypeChecker.isEmpty(drawString)){
  22. return ;
  23. }
  24.  
  25. if(TypeChecker.isTrue(eForm.getImage()) ) {
  26. if(TypeChecker.isTrue(drawString)){
  27. String eCardColor = ds.getString("CARDCOLOR");
  28. String MedpassLogo = eCardXMLConfigManager.geteCardXMLConfigManager().getRealPath()+ "/com."+suffix ;
  29. if(!TypeChecker.isEmpty(eCardColor) && eCardColor.equalsIgnoreCase("eCARDCOLOR.Red")){
  30. MedpassLogo = eCardXMLConfigManager.geteCardXMLConfigManager().getRealPath()+ "/com."+suffix ;
  31. }
  32. Image imagewritted = ImageIO.read(new File(MedpassLogo));
  33. g.drawImage(imagewritted,eForm.getxPosition(), eForm.getyPosition(), eForm.getWidth(), eForm.getHeight(), null, null);
  34. }
  35. return ;
  36. }
  37.  
  38. //color
  39. g.setColor(Color.BLACK);
  40. if(TypeChecker.isEmpty(eForm.getColor())){
  41. if(eForm.getColor().equals("red")){
  42. g.setColor(Color.BLACK);
  43. }
  44. else if(eForm.getColor().equals("blue")){
  45. g.setColor(Color.BLUE);
  46. }
  47. else if(eForm.getColor().equals("gray")){
  48. g.setColor(Color.GRAY);
  49. }
  50. else if(eForm.getColor().equals("yellow")){
  51. g.setColor(Color.YELLOW);
  52. }
  53. else if(eForm.getColor().equals("dark_gray")){
  54. g.setColor(Color.DARK_GRAY);
  55. }
  56. else if(eForm.getColor().equals("pink")){
  57. g.setColor(Color.PINK);
  58. }
  59. }
  60.  
  61. AttributedString as = new AttributedString(drawString);
  62. as.addAttribute(TextAttribute.FONT, plainFont);

  63.   //设置加黑加粗属性weight,一直无法生效,没找到原因。
  64. if(TypeChecker.isTrue(eForm.isBold())){
  65. g.setFont(plainFont);
  66. Font boldFont = new Font(eForm.getFontType(), Font.BOLD, eForm.getFontSize());
  67. as.addAttribute(TextAttribute.FONT, boldFont);
  68. as.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_LIGHT);
  69. //as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
  70. }
  71. if(TypeChecker.isTrue(eForm.isAlignRight())){
  72. as.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL);
  73. }
  74. else{
  75. as.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_LTR);
  76. }
  77. if(TypeChecker.isTrue(eForm.isUnderLine())){
  78. as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
  79. }
  80.  
  81. //as.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON, 18, 25);
  82. int xPos = eForm.getxPosition();
    //左右对齐
  83. if(TypeChecker.isTrue(eForm.isAlignRight())){
  84. g.setFont(plainFont);
  85. FontMetrics fm = g.getFontMetrics();
  86. Rectangle2D rc = fm.getStringBounds(drawString.trim(), g);
  87. xPos = eForm.getxPosition() - (int)(rc.getWidth());
  88. if(xPos < 0 ){
  89. xPos = 0;
  90. }
  91. }
    //居中
  92. if(TypeChecker.isTrue(eForm.isAlignCenter())){
  93. g.setFont(plainFont);
  94. FontMetrics fm = g.getFontMetrics();
  95. Rectangle2D rc = fm.getStringBounds(drawString.trim(), g);
  96. xPos = Math.round((1130-(int)(rc.getWidth()))/2);
  97. if(xPos < 0 ){
  98. xPos = 0;
  99. }
  100. }
  101.  
  102. int yxPos = eForm.getyPosition();
  103.  
  104. g.drawString(as.getIterator(), xPos, yxPos);
  105.  
  106. }

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

使用graphics2D给图片上画字符的更多相关文章

  1. 在图片上加字符-base64转图片-图片转base64

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. HTML5将图片转化成字符画

    HTML5将图片转化成字符画 字符画大家一定非常熟悉了,那么如何把一张现有的图片转成字符画呢?HTML5让这个可能变成了现实,通过canvas,可以很轻松实现这个功能.其实原理很简单:扫描图片相应位置 ...

  3. 基于 canvas 将图片转化成字符画

    字符画大家一定非常熟悉了,那么如何把一张现有的图片转成字符画呢? HTML5 让这个可能变成了现实,通过 canvas,可以很轻松实现这个功能. 其实原理很简单:扫描图片相应位置的像素点,再计算出其灰 ...

  4. 将图片转为ASCII字符画

    原文:将图片转为ASCII字符画 Copyright 2012 Conmajia 源代码下载:点击这里 什么是字符画?就是用ASCII字符来近似组成图像,就像这样: ╭╮ ╭╮ ││ ││ ╭┴┴—— ...

  5. python 图片在线转字符画预览

    文章链接:https://mp.weixin.qq.com/s/yiFOmljhyalE8ssAgwo6Jw 关于python图片转字符画,相信大家都不陌生,经常出现在 n个超有趣的python项目中 ...

  6. 基于canvas将图片转化成字符画

    字符画大家一定非常熟悉了,那么如何把一张现有的图片转成字符画呢?HTML5让这个可能变成了现实,通过canvas,可以很轻松实现这个功能.其实原理很简单:扫描图片相应位置的像素点,再计算出其灰度值,根 ...

  7. Java实现把图片转成字符画

    1,先看效果图:

  8. Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解

    Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全   Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...

  9. wxWidgets学习笔记——在屏幕上画简单的图形和文字

    在屏幕上画简单图形和显示图片.处理简单鼠标键盘事件 /*************************************************************** * Name: M ...

随机推荐

  1. 牛刀小试、用SharePoint 实现请假管理功能

    转:http://www.cr173.com/html/15518_1.html "请假管理"应用,应该算是 SharePoint 的"Hello World!" ...

  2. SharePoint 2010 安装简介及相关补丁下载

    转:http://www.cnblogs.com/jianyus/archive/2011/10/28/2228212.html 1.安装Windows Server 2008 系统,这个我就不说了, ...

  3. ubuntu相关软件合集(持续更新中)

    本人使用的是Ubuntu-Kylin14.04,自带了日历.输入法.优客助手等易于上手的应用.省的每次安装完原生的系统再麻烦的安装,下面介绍默认应用外的相关常用软件: 一.Keylock Applic ...

  4. ldr指令总结

    LDR/STR字和无符号字节加载/存储 1,LDR Rd,[Rn]   2, LDR Rd,[Rn,Flexoffset] 3, LDR Rd,[Rn],Flexoffset 4, LDR Rd,la ...

  5. 在Ubuntu 中安装eclipse, eclipse 文件已经下载好!

    If you've downloaded Eclipse from their official website, follow these steps for the installation. E ...

  6. extjs的button控件fireEvent('click')无效问题

    extjs的button控件fireEvent('click')无效问题 2012-06-06 16:39 891人阅读 评论(0) 收藏 举报 extjsbuttoncallbackfunction ...

  7. Java中static和final的区别

    static是静态修饰关键字,可以修饰变量和程序块以及类方法: 当定义一个static的变量的时候jvm会将将其分配在内存堆上,所有程序对它的引用都会指向这一个地址而不会重新分配内存: 当修饰一个程序 ...

  8. Bash For Loop Examples

    How do I use bash for loop to repeat certain task under Linux / UNIX operating system? How do I set ...

  9. Yii2 behavior运用

    class ReturnDataTypeBehaviors extends Behavior { public $type = 'json'; public $pcOrMobile = 'pc'; / ...

  10. Java分布式优秀资源集合

    这里充分尊重原作者的版本,学习了知识要感激原博主 Runnable.Callable.Executor.Future.FutureTask关系解读 http://blog.csdn.net/zhang ...