mxgraph 中文
场景:解决mxgraph导出图片的中文乱码和image节点无法显示的有关问题
解决mxgraph导出图片的中文乱码和image节点无法显示的问题
1、写在前面,mxgraph原来它的jar包是有源码的啊,就在mxgraph\java\src里。
2、我改的部分:
\src\com\mxgraph\util\mxUtils.java
中的方法public static BufferedImage loadImage(String url)
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)
3.打包方法
将这两个文件编译后生成的class文件替换掉mxgraph-core.jar中的同名class文件即可。
特别注意:在你的工程中只需添加mxgraph-core.jar这个包,其他的mxgraph包不用加了啊。重复的。
*******************最最重要的分割线******************************
lysh如果爱是会过期的罐头,那么,我说过的保质期是不会变的,无论你怎样。保质期就在那里,不增不减。 不过,我多么希望,always have always will……
解决mxgraph导出图片的中文乱码和image节点无法显示的问题
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包不用加了啊。重复的。
*******************最最重要的分割线******************************
lysh如果爱是会过期的罐头,那么,我说过的保质期是不会变的,无论你怎样。保质期就在那里,不增不减。 不过,我多么希望,always have always will……