论坛首页 移动开发技术论坛

解决mxgraph导出图片的中文乱码和image节点无法显示的问题

浏览 4951 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-12-02  
1、写在前面,mxgraph原来它的jar包是有源码的啊,就在mxgraph\java\src里。
2、我改的部分:
\src\com\mxgraph\util\mxUtils.java
中的方法public static BufferedImage loadImage(String url)
/**
	 * Loads an image from the local filesystem, a data URI or any other URL.
*增加了处理本地图片资源的方法,因此在导入graph的xml之前,要将图片的src换为本地服务器的绝对地址哦!
	 */
	public static BufferedImage loadImage(String url)
	{
		BufferedImage img = null;
		if (url != null)
		{
			// Parses data URIs of the form data:image/format;base64,xxx
			if (url.startsWith("data:image/"))
			{
				try
				{
					int comma = url.indexOf(',');
					byte[] data = mxBase64.decode(url.substring(comma + 1));
					ByteArrayInputStream is = new ByteArrayInputStream(data);
					img = ImageIO.read(is);
				}
				catch (Exception e1)
				{
					// ignore
				}
			}else if(url.startsWith("http://")){
				URL realURL = null;

				try
				{
					realURL = new URL(url);
					
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				if (realURL != null)
				{
					try
					{
						img = ImageIO.read(realURL);
					}
					catch (Exception e1)
					{
						// ignore
					}
				}
			}else
			{//处理本地图片资源deal with the local image sourse
				File imageFile = null;

				try
				{
					imageFile = new File(url);
					System.out.println(imageFile.getAbsoluteFile());
					
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				if (imageFile != null)
				{
					try
					{
						img = ImageIO.read(imageFile);
					}
					catch (Exception e1)
					{
						// ignore
					}
				}
			}
		}

		return img;
	}



src\com\mxgraph\canvas\mxGraphicsCanvas2D.java
修改了方法public void text(double x, double y, double w, double h, String str,
String align, String valign, boolean vertical)
/**
	 * Draws the given text.
	 */
	public void text(double x, double y, double w, double h, String str,
			String align, String valign, boolean vertical)
	{
		if (!state.fontColorValue.equals(mxConstants.NONE))
		{
			x = state.dx + x * state.scale;
			y = state.dy + y * state.scale;
			w *= state.scale;
			h *= state.scale;
			Graphics2D g2 = createTextGraphics(x, y, w, h, vertical);
			Font font = new Font("宋体", Font.BOLD, 12);//增加这句后就能使打印的中文没有乱码了,这是参考activiti动态打印png图片的乱码问题解决滴!
		    g2.setFont(font);
			FontMetrics fm = g2.getFontMetrics();
			String[] lines = str.split("\n");

			y = getVerticalTextPosition(x, y, w, h, align, valign, vertical,
					fm, lines);
			x = getHorizontalTextPosition(x, y, w, h, align, valign, vertical,
					fm, lines);

			for (int i = 0; i < lines.length; i++)
			{
				double dx = 0;

				if (align != null)
				{
					if (align.equals(mxConstants.ALIGN_CENTER))
					{
						int sw = fm.stringWidth(lines[i]);
						dx = (w - sw) / 2;
					}
					else if (align.equals(mxConstants.ALIGN_RIGHT))
					{
						int sw = fm.stringWidth(lines[i]);
						dx = w - sw;
					}
				}
				g2.drawString(lines[i], (int) Math.round(x + dx),
						(int) Math.round(y));
				y += fm.getHeight() + mxConstants.LINESPACING;
			}
		}
	}


3.打包方法
将这两个文件编译后生成的class文件替换掉mxgraph-core.jar中的同名class文件即可。
特别注意:在你的工程中只需添加mxgraph-core.jar这个包,其他的mxgraph包不用加了啊。重复的。
   发表时间:2011-12-13  
亲们,中国移动统一开发环境(英文简称UDE)有新动态咯咩\(^o^)/~
从开发者的角度出发,开发了一套集成了Eclipse IDE、多种手机应用的开发包(SDK)、终端信息库、能力集市开发库和跨平台中间件的开发工具统一开发环境,支持Android、J2ME(K-Java)、Symbian的原生态和跨平台开发,一次性安装,还可以将开发出来的代码放在配好的模拟器上直接运行。开发者使用一门语言(Java)开发,在一套代码的基础上可以生成适用于多平台的手机应用。大家可以继续关注嘿~!!
http://dev.10086.cn/cmdn/bbs/thread-47163-1-1.html
而且最近又有更新内容哈,强势围观围观ing:
1)社区服务的IP地址更新,避免开发者无法正常访问社区服务。
2)为了支持139能力(移动微博)调整了部分运行库,保障在手机端登陆成功。
离线插件:(下载文件后,直接传到服务器上)
http://devfile.mmarket.com/upload/cmcc_ude/plugin/cmcc_ude_update_1.1.2.zip
在线插件:(文件上传后,解压到指定的目录)
http://devfile.mmarket.com/upload/cmcc_ude/update/
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics