使用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); }
此方法主要是往图片的相应位置画数据。
使用graphics2D给图片上画字符的更多相关文章
- 在图片上加字符-base64转图片-图片转base64
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- HTML5将图片转化成字符画
HTML5将图片转化成字符画 字符画大家一定非常熟悉了,那么如何把一张现有的图片转成字符画呢?HTML5让这个可能变成了现实,通过canvas,可以很轻松实现这个功能.其实原理很简单:扫描图片相应位置 ...
- 基于 canvas 将图片转化成字符画
字符画大家一定非常熟悉了,那么如何把一张现有的图片转成字符画呢? HTML5 让这个可能变成了现实,通过 canvas,可以很轻松实现这个功能. 其实原理很简单:扫描图片相应位置的像素点,再计算出其灰 ...
- 将图片转为ASCII字符画
原文:将图片转为ASCII字符画 Copyright 2012 Conmajia 源代码下载:点击这里 什么是字符画?就是用ASCII字符来近似组成图像,就像这样: ╭╮ ╭╮ ││ ││ ╭┴┴—— ...
- python 图片在线转字符画预览
文章链接:https://mp.weixin.qq.com/s/yiFOmljhyalE8ssAgwo6Jw 关于python图片转字符画,相信大家都不陌生,经常出现在 n个超有趣的python项目中 ...
- 基于canvas将图片转化成字符画
字符画大家一定非常熟悉了,那么如何把一张现有的图片转成字符画呢?HTML5让这个可能变成了现实,通过canvas,可以很轻松实现这个功能.其实原理很简单:扫描图片相应位置的像素点,再计算出其灰度值,根 ...
- Java实现把图片转成字符画
1,先看效果图:
- Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解
Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全 Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...
- wxWidgets学习笔记——在屏幕上画简单的图形和文字
在屏幕上画简单图形和显示图片.处理简单鼠标键盘事件 /*************************************************************** * Name: M ...
随机推荐
- word在线编辑\生成图片(包含截图与合并)
1.业务原因 word编辑后的文章复制到html编辑器(fck等)会发生排版错乱的情况,于是混沌了.需要有一个新的方法来终结,于是产生了word能不能在线编辑,后台保存,前台显示灯一系列问题. 2.首 ...
- BZOJ1935: [Shoi2007]Tree 园丁的烦恼
1935: [Shoi2007]Tree 园丁的烦恼 Time Limit: 15 Sec Memory Limit: 357 MBSubmit: 552 Solved: 220[Submit][ ...
- Flash正则例子
var jack:String = "zhuhai"; var arr:Array = ["23", "4", "67" ...
- Android项目开发全程(四)-- 将网络返回的json字符串轻松转换成listview列表
前面几篇博文介绍了从项目搭建到获取网络字符串,对一个项目的前期整体工作进行了详细的介绍,本篇接着上篇介绍一下怎么样优雅将网络返回的json字符串轻松转换成listview列表. 先上图,看一下效果. ...
- Oracle 行拼接 wmsys.wm_concat扩展
将多行数据拼接成一行: --wmsys.wm_concat group by a.flowid; 但有大小限制:字符串缓冲区太小,超过varchar 4000长度.扩展:更改返回类型为clob --T ...
- 网页局部刷新的办法。javascript+frame 或者ajax原理
<frame>标签 + javascriptajax (DWR \ jquery \ extjs \ vs 下的ScriptManager和UpdatePanel控件)
- 搜索算法:深度优先搜索(DFS)
关于深搜的介绍,在网上有很多,不再赘述.主要以题目形式实例讲解. POJ - 1321 (http://poj.org/problem?id=1321) 题目大意:给出一个棋盘,棋子不能同行同列,求放 ...
- In Java, what is the default location for newly created files?
If the current directory of the application. If e.g. you create a File by using new FileOutputStream ...
- python的import与from...import的不同之处
在python用import或者from...import来导入相应的模块.模块其实就是一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的模块导入到我们的程序中 ...
- codeforces 732D
D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...