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;

}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.