java產生JPEG 影像檔
來源:互聯網
上載者:User
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;
}