快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

java生成JPEG 图像文件

java生成JPEG 图像文件. 代码如下:

我的这段代码主要提供应我自己编写的工作流设计器对象使用(EWorkFlowControl ewf), EWorkFlowControl 继续了JComponent重载了绘制的方式,假如有兄弟需要用到这个功能的,只需要将EWorkFlowControl 替换成自己的JComponent对象即可。

/** 使当前输入的工作流对象上的图形生成JPEG 图像文件.

* 每一个节点对应一个图片.

* 建立的文件存放在jar 文件文件夹下,并创建一个子文件夹

* 以节点的名称为文件夹的名称.

* @param ewf 需要生成jpeg图片文件的工作流对象.

* @return boolean: 返回一个布尔值,表示是否保存成功.

*/

public static boolean buildThePictrue(EWorkFlowControl ewf)

{

//modify by mw

//列出流程中所有的node节点.

//分别设置这些节点为选中状态,并存成图片(名称为节点的流水号)

INode rootNode = ewf.getNote(); Rectangle bounds = ewf.getBounds();

INode[] children = rootNode.getNodeChildren();

int childrenLen = rootNode.getNodeChildCount();

String nodeNumber = null;

boolean flag = true;

//消除所有节点得选中状态.

for(int j = 0; j < childrenLen; j++) {

children[j].setSelect(false); }

//生成总图(我是一个节点一个图像)

try {

java.awt.image.BufferedImage image; image = new java.awt.image.BufferedImage(bounds.width, bounds.height, java.awt.image.BufferedImage.TYPE_INT_RGB);

Graphics g = image.getGraphics(); g.setColor(Color.white);

g.fillRect(0, 0, bounds.width, bounds.height);

ewf.paintComponent(g);

String path = System.getProperty("user.dir") +System.getProperty("file.separator") +ewf.getWorkFlowName() +"["+ewf.getWorkFlowCode()+"]";

File fd = new File(path);

if (!fd.exists()) {

fd.mkdirs();

}

String filePath = path + System.getProperty("file.separator")+"0.jpeg";

File imgFile = new File(filePath);

FileOutputStream fos = new FileOutputStream(imgFile);

BufferedOutputStream bos = new BufferedOutputStream(fos);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);

encoder.encode(image); bos.close();

}

catch(Exception ex) {

flag = false;

}

//到此第一个图像已经生成!

//循环生成图片

for(int i = 0; i < childrenLen; i++)

{

nodeNumber = children[i].getNodeNumber();

if (nodeNumber == null)

{

flag = false; continue;

} else {

children[i].setSelect(true);

try {

java.awt.image.BufferedImage image; image = new java.awt.image.BufferedImage(bounds.width, bounds.height, java.awt.image.BufferedImage.TYPE_INT_RGB);

Graphics g = image.getGraphics();

g.setColor(Color.white);

g.fillRect(0, 0, bounds.width, bounds.height);

ewf.paintComponent(g);

String path = System.getProperty("user.dir") +System.getProperty("file.separator") +ewf.getWorkFlowName() +"["+ewf.getWorkFlowCode()+"]";

File fd = new File(path);

if (!fd.exists()) {

fd.mkdirs();

}

String filePath = path + System.getProperty("file.separator")+nodeNumber+".jpeg";

File imgFile = new File(filePath);

FileOutputStream fos = new FileOutputStream(imgFile);

BufferedOutputStream bos = new BufferedOutputStream(fos); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);

encoder.encode(image); bos.close();

children[i].setSelect(false);

} catch(Exception ex) {

flag = false;

} } }

if (flag) {

new ExceptionDialog(PIC_SAVE_OK,PIC_SAVE_DIALOG_TITLE, DesktopFrame.getCurrentInstance(), true).setVisible(true);

} else {

new ExceptionDialog(PIC_SAVE_ERROR,PIC_SAVE_DIALOG_TITLE, DesktopFrame.getCurrentInstance(), true).setVisible(true);

}

return flag;

}







返回类别: 教程
上一教程: Reading and Writing a Properties File(From Sun)
下一教程: 如何用开源软件构建 java 编程环境

您可以阅读与"java生成JPEG 图像文件"相关的教程:
· 如何将做好的JAVA程序生成可执行文件
· 好东西介绍,直接用 java 命令行动态生成jpg文件
· 用Java生成文本文件(收集)
· 使用hibernate扩展工具hbm2java根据配置文件生成持久化对象类(2.1.2)
· 头文件生成器-JAVAH.EXE
    微笑服务 优质保证 索取样品