JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java SE 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 請問如何處理GIF PNG的透明色 [精華]
liminjun

JavaDOCHelper真讚



發文: 496
積分: 5
於 2004-01-04 23:07 user profilesend a private message to usersend email to liminjunreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
小弟目前已知如何用ImageIO讀取,存成BufferedImage
但如果讀取近來的圖形包含透明色,該如何處理呢?
若直接去色(忽略底色),會造成顯示出來的圖檔破碎(邊緣雜質)

請問該怎麼處理有透明色的gif和PNG呢?
可否麻煩給個小範例或是提供相關資料網頁位址^_^
感激不盡

程式打算將多張圖(png gif jpg等) 讀取,並且排列合併到一張背景圖上
讀近來的圖片,有些會具有透明色,有些則否,總之最後會和底圖合成存成另一張圖


liminjun edited on 2004-01-05 16:06
reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:liminjun]
qop

Newbie



發文: 204
積分: 3
於 2004-01-04 23:58 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我以前用
Toolkit.getDefaultToolkit().getImage()
就可以由圖形檔取得 Image 物件, 而且若為 *.gif 直接使用 Griaphics 物件畫上去就都可以正常繪出去背圖了啊 ~

*.gif 和 *.png 都是 OK 的呀 !

不知道為什麼使用 ImageIO 和 BufferedImage ? ( 沒有試過這種方式 Tongue )


reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:qop]
liminjun

JavaDOCHelper真讚



發文: 496
積分: 5
於 2004-01-05 06:55 user profilesend a private message to usersend email to liminjunreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
qop wrote:
我以前用
Toolkit.getDefaultToolkit().getImage()
就可以由圖形檔取得 Image 物件, 而且若為 *.gif 直接使用 Griaphics 物件畫上去就都可以正常繪出去背圖了啊 ~

*.gif 和 *.png 都是 OK 的呀 !

不知道為什麼使用 ImageIO 和 BufferedImage ? ( 沒有試過這種方式 Tongue )


小弟需要做到以下功能:
從多個PNG GIF(20x20圖檔三個a.gif b.png jpg)讀取圖檔資料,
合成圖檔(三個檔案並列a+b+c = d.jpg 60x20 )存成另一個圖形檔

而且 gif png 存的圖檔含有透明色


reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:liminjun]
qop

Newbie



發文: 204
積分: 3
於 2004-01-05 13:10 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
liminjun wrote:
小弟需要做到以下功能:
從多個PNG GIF(20x20圖檔三個a.gif b.png jpg)讀取圖檔資料,
合成圖檔(三個檔案並列a+b+c = d.jpg 60x20 )存成另一個圖形檔

而且 gif png 存的圖檔含有透明色

使用 java.awt.Toolkit.getDefaultToolkit().getImage() 所取得的 Image 物件本身就有透明的性質...

依你要的功能, 跟本不需要去處理含有透明色的問題,
因為你要存成的是 JPG 而不是 GIF 或 PNG

可以試一下這個 package
com.sun.image.codec.jpeg
應該可以在 sun 的網站找到吧^^


reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:qop]
liminjun

JavaDOCHelper真讚



發文: 496
積分: 5
於 2004-01-05 15:57 user profilesend a private message to usersend email to liminjunreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
qop wrote:
使用 java.awt.Toolkit.getDefaultToolkit().getImage() 所取得的 Image 物件本身就有透明的性質...

依你要的功能, 跟本不需要去處理含有透明色的問題,
因為你要存成的是 JPG 而不是 GIF 或 PNG

可以試一下這個 package
com.sun.image.codec.jpeg
應該可以在 sun 的網站找到吧^^


抱歉,說的有點不清楚,可能造成一點小誤解
程式打算將多張圖(png gif jpg等) 讀取,並且排列合併到一張背景圖上
讀近來的圖片,有些會具有透明色,有些則否,總之最後會和底圖合成存成另一張圖(有透明色的用png,若沒有則用jpg)

(1)目前僅知道可以利用 ImageIO 搭配BufferedImage寫入圖檔,不知道還有哪些方法? 有搭配Image物件能存成物件的方法嗎?
(2)不久前曾經嚐試過您提供的方法,用Toolkit.getDefaultToolkit().getImage() 傳回Image物件,並且利用下列連結提供的範例,將Image轉成BufferedImage,只不過不知道為什麼回傳的是null
(3)雖然有點冒昧,希望知道怎麼做的大大,能夠提供一個小小的範例,或是相關資料網頁,感激不盡

http://javaalmanac.com/egs/java.awt.image/Image2Buf.html?l=find
http://javaalmanac.com/egs/java.awt.image/HasAlpha.html?l=find

1
2
3
Strint path = XXXXX; //略,總之我有指向正確目錄
Image temp = Toolkit.getDefaultToolkit().getImage( path + "inFile.gif");
BufferedImage myimage =  toBufferedImage( temp );//上面的範例連結有這個函式


liminjun edited on 2004-01-05 16:18
reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:liminjun]
qop

Newbie



發文: 204
積分: 3
於 2004-01-05 21:17 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我試出來可以存成 JPEG, PNG
不過 PNG 不曉得要如何設定成「去背圖」 Tongue 可能要再研究了...

我猜你的 BufferedImage 會得到 null 的原因很可能是 圖檔未完全載入
請使用 MediaTracker

附加檔是執行情形
若你還是沒弄出來, 我再貼 code Big Smile

TestPics.zip (9.64k)


reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:qop]
liminjun

JavaDOCHelper真讚



發文: 496
積分: 5
於 2004-01-06 14:35 user profilesend a private message to usersend email to liminjunreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
qop wrote:
我試出來可以存成 JPEG, PNG
不過 PNG 不曉得要如何設定成「去背圖」 Tongue 可能要再研究了...

我猜你的 BufferedImage 會得到 null 的原因很可能是 圖檔未完全載入
請使用 MediaTracker

附加檔是執行情形
若你還是沒弄出來, 我再貼 code Big Smile

感謝感謝感謝感謝感謝......實在真的太感謝了
你附的CLASS已經看的到原始碼了,這招用的真妙
原本我去背的方法是先將BufferedImage getRGB得到pixels,然後用自己用演算法忽略某些相近顏色達到去背景效果,剛開始封閉圖形和簡單的漸層色處理的還適當,可是後來碰到透明色的文字圖就開始出問題了@@||

最後,還是想請問一下,哪裡可以找到處理Image BufferedImage的網站資料和範例?或是台灣地區出版的java書籍?

1
2
3
4
5
            offg = offimg.getGraphics();
            offg.drawImage(img[2], 0, 0, null);
            offg.drawImage(img[0], 10, 10, null);
            offg.drawImage(img[1], 100, 50, null);
            offbi = toBufferedImage(offimg);


liminjun edited on 2004-01-06 14:39
reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:liminjun]
qop

Newbie



發文: 204
積分: 3
於 2004-01-06 17:13 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Big Smile 原來用了 DJ
也要謝謝你, 不然偶也不知道可以用 ImageIO Big Smile

有關 Image 的處理, 我手頭上都是很舊的資料...
書上也大多是蜻蜓點水... 沒有值得推的... 我想自己下手寫還是最實在

偶還是把 code 貼出來好了, 不然不會 DJ 的就看不到了 Smile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.awt.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.image.*;
import java.io.*;
 
public class TestPics extends JFrame{
    Image offimg;
    BufferedImage offbi;
    Image img[] = new Image[3];
    String [] imgname = {"face1.gif", "face2.png", "face3.jpg"};
    Graphics offg;
    Toolkit mytk;
    File savedFile;
    int fw = 149, fh = 190;
    MediaTracker mt;
    
    public TestPics(){
        super("Pics Test");
        mt = new MediaTracker(this);
        mytk = Toolkit.getDefaultToolkit();
        for(int i=0; i<imgname.length; i++){
            img[i] = mytk.getImage(imgname[i]);
            mt.addImage(img[i], i); //追蹤影像的載入
        }
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        savedFile = new File("save.jpg");
        setSize(fw+8, fh+34);  //校正視窗大小
    }
    public void paint(Graphics g){
        if(offimg == null){
            offimg = createImage(fw, fh);
            try{
                mt.waitForAll();    // 圖檔都載入後, 再繼續執行
            }catch(Exception ex){}
            offg = offimg.getGraphics();
            offg.drawImage(img[2], 0, 0, null);
            offg.drawImage(img[0], 10, 10, null);
            offg.drawImage(img[1], 100, 50, null);
            offbi = toBufferedImage(offimg);
            try{
                System.out.println("存檔結果:"+ImageIO.write(offbi, "jpeg", savedFile));
            }catch(IOException ioex){}
        }
        g.drawImage(offimg, 4, 30, this);
    }
    public static void main(String s[]){
        TestPics tp = new TestPics();
        tp.show();
    }
    //  http://javaalmanac.com/egs/java.awt.image/Image2Buf.html?l=find
    public static BufferedImage toBufferedImage(Image image) {
        if (image instanceof BufferedImage) {
            return (BufferedImage)image;
        }
        image = new ImageIcon(image).getImage();
        boolean hasAlpha = hasAlpha(image);
        BufferedImage bimage = null;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        try {
            int transparency = Transparency.OPAQUE;
            if (hasAlpha) {
                transparency = Transparency.BITMASK;
            }
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gs.getDefaultConfiguration();
            bimage = gc.createCompatibleImage(
                image.getWidth(null), image.getHeight(null), transparency);
        } catch (HeadlessException e) {}
        if (bimage == null) {
            int type = BufferedImage.TYPE_INT_RGB;
            if (hasAlpha) {
                type = BufferedImage.TYPE_INT_ARGB;
            }
            bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
        }
        Graphics g = bimage.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return bimage;
    }
    //  http://javaalmanac.com/egs/java.awt.image/HasAlpha.html?l=find
    public static boolean hasAlpha(Image image) {
        if (image instanceof BufferedImage) {
            BufferedImage bimage = (BufferedImage)image;
            return bimage.getColorModel().hasAlpha();
        }
         PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
        try {
            pg.grabPixels();
        } catch (InterruptedException e) { }
        ColorModel cm = pg.getColorModel();
        return cm.hasAlpha();
    }
}


reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:liminjun]
liminjun

JavaDOCHelper真讚



發文: 496
積分: 5
於 2004-01-06 19:20 user profilesend a private message to usersend email to liminjunreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
有個小問題

如果只是平常電腦裡的小程式(JFrame )合成圖片已經OK

(1)但是,如果我寫在JSP或是servlet時候,編譯到mt = new MediaTracker(this);這行就會出現錯誤cannot resolve symbol,而且我有正確的import,不知原因為何?
1
2
3
4
C:\Server\Tomcat\work\Catalina\localhost\_\org\apache\jsp\SafeCode_jsp.java:89: cannot resolve symbol
symbol  : constructor MediaTracker (org.apache.jsp.SafeCode_jsp)
location: class java.awt.MediaTracker
    MediaTracker mt = new MediaTracker(this);


(2)offimg = createImage(fw, fh);這行也會出現
1
2
3
4
5
Generated servlet error:
C:\Server\Tomcat\work\Catalina\localhost\_\org\apache\jsp\SafeCode_jsp.java:110: cannot resolve symbol
symbol  : method createImage (int,int)
location: class org.apache.jsp.SafeCode_jsp
    offimg = createImage(200, 20);


[color=yellow]
1
2
3
4
5
import java.awt.*;[/color]
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

不知道您是否曾寫過用 servlet , jsp 秀出image合成圖的經驗
方便的話,麻煩您分享一下經驗


liminjun edited on 2004-01-06 19:27
reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:liminjun]
qop

Newbie



發文: 204
積分: 3
於 2004-01-06 21:00 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
create 一個 Component 物件塞給它

reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:qop]
liminjun

JavaDOCHelper真讚



發文: 496
積分: 5
於 2004-01-06 22:09 user profilesend a private message to usersend email to liminjunreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
qop wrote:
create 一個 Component 物件塞給它


(1)不懂你的意思,Component不是一個abstract Class嗎
應該不能建立物件吧?我想到的方法是用Container類別
Container cr = new Container();
MediaTracker mt = new MediaTracker(cr);
不知道是否正確?

(2)下面這個問題該如何解決?
1
2
3
4
5
6
7
An error occurred at line: 10 in the jsp file: /SafeCode.jsp
 
Generated servlet error:
C:\Server\Tomcat\work\Catalina\localhost\_\org\apache\jsp\SafeCode_jsp.java:112: cannot resolve symbol
symbol  : method createImage (int,int)
location: class org.apache.jsp.SafeCode_jsp
    offimg = createImage(200, 20);


(3)java.awt.Component的 createImage  函式要怎麼才能在jsp , servlet中使用? 我用的方法是
Container cr2 = new Container();
offimg = cr2.createImage(200, 20);
不知道是否正確?

(4)請問有沒有jsp servlet處理image合成然後用response.getOutputStream()輸出的範例呢?


reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:liminjun]
qop

Newbie



發文: 204
積分: 3
於 2004-01-07 03:01 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
偶是 JSP 生生手...
把原本的程式轉成 JSP 就發現一脫拉庫的 error

debug 完後, 發現出來一片黑...
原來 Image 物件跟本就不能畫在 BufferedImage 上, 亂奇怪的 Disapproved

用 toBufferedImage() 會有一堆錯...

現在大概只能把希望放在 PixelGrabber 上了, 再試吧 >"<

原來 J2SE 就包含了 com.sun.image.codec.jpeg Package
為什麼 Doc 上找不到 >"<


reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:qop]
liminjun

JavaDOCHelper真讚



發文: 496
積分: 5
於 2004-01-07 03:23 user profilesend a private message to usersend email to liminjunreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
qop wrote:
偶是 JSP 生生手...
把原本的程式轉成 JSP 就發現一脫拉庫的 error

debug 完後, 發現出來一片黑...
原來 Image 物件跟本就不能畫在 BufferedImage 上, 亂奇怪的 Disapproved

用 toBufferedImage() 會有一堆錯...

現在大概只能把希望放在 PixelGrabber 上了, 再試吧 >"<

原來 J2SE 就包含了 com.sun.image.codec.jpeg Package
為什麼 Doc 上找不到 >"<


JSP Servlet單純只用ImageIO 合成和輸出圖檔的話,已經OK了(只是會有透明色的問題)

如果用Image轉BufferedImage的話,問題出在於Tookit的getImage讀取進來的居然是null
真不知道為什麼,路徑之類的都有設定正確...


reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:liminjun]
liminjun

JavaDOCHelper真讚



發文: 496
積分: 5
於 2004-01-07 10:28 user profilesend a private message to usersend email to liminjunreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你的程式了我一點靈感,非常感謝^^,我用下面列出的方法就可以繪出透明色圖形了,只需用到BufferedImage ImageIO Graphics2D 就OK了,不用花繁雜的手續將Image轉BufferedImage

Image,BufferedImage,Byte[] 都可以用drawImage 畫在 BufferedImage上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    int width = 200;
    int height = 20;
 
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB );
    Graphics2D g2d = bufferedImage.createGraphics();
 
    // Draw graphics
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, width, height);
    g2d.drawImage( bg , null , 0 , 0);
    g2d.drawImage( image0 , null ,0 , 0 );
    g2d.drawImage( image1 , null ,20 , 0 );
    g2d.drawImage( image2 , null ,40 , 0 );
    g2d.drawImage( image3 , null ,60 , 0 );
    g2d.drawImage( image4 , null ,80 , 0 );
    g2d.drawImage( image5 , null ,100 , 0 );
    g2d.drawImage( image6 , null ,120 , 0 );
    g2d.drawImage( image7 , null ,140 , 0 );
    g2d.drawImage( image8 , null ,160 , 0 );
    g2d.drawImage( image9 , null ,180 , 0 );
 
    // Graphics context no longer needed so dispose it
    g2d.dispose();


後記:sun java DOC 在某些情況查資料的確方便,但是因為範例不完整,寫程式的時候無法從範例中得到完整的架構觀念,以致於常常為了做某些動作而海底撈針...這點比起Ms 的 MSDN文檔的確差滿多的,進階技術學習門檻感覺稍微高了點..


liminjun edited on 2004-01-07 10:47
reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:liminjun]
liminjun

JavaDOCHelper真讚



發文: 496
積分: 5
於 2004-01-07 10:43 user profilesend a private message to usersend email to liminjunreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
續接上則

我已經完成gif 透明圖 + jpg png (不透明圖) 的合成動作
但是碰到 png 透明圖時候 卻出現下列問題,不知道該如何解決

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
exception 
 
org.apache.jasper.JasperException: LUT has improper length!
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
 
root cause 
 
java.lang.IllegalArgumentException: LUT has improper length!
  javax.imageio.ImageTypeSpecifier$Indexed.(Unknown Source)
  javax.imageio.ImageTypeSpecifier.createIndexed(Unknown Source)
  com.sun.imageio.plugins.png.PNGImageReader.getImageTypes(Unknown Source)
  com.sun.imageio.plugins.png.PNGImageReader.readImage(Unknown Source)
  com.sun.imageio.plugins.png.PNGImageReader.read(Unknown Source)
  javax.imageio.ImageIO.read(Unknown Source)
  javax.imageio.ImageIO.read(Unknown Source)
  org.apache.jsp.SafeCode_jsp._jspService(SafeCode_jsp.java:96)
  org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:liminjun]
qop

Newbie



發文: 204
積分: 3
於 2004-01-07 11:53 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
liminjun wrote:
續接上則

我已經完成gif 透明圖 + jpg png (不透明圖) 的合成動作
但是碰到 png 透明圖時候 卻出現下列問題,不知道該如何解決


我已經做出來了 ^^' 後來才發現是路徑問題.... 圖檔根本沒載入
而且 toBufferedImage() 我也覺得是多此一舉, 以前都是圖沒載入惹的禍

發現 ImageIcon 比較好用, 把路徑問題解決一下再 po

突然發現正事都沒做... Oh, My God !!!Black Eye


reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:qop]
qop

Newbie



發文: 204
積分: 3
於 2004-01-07 12:13 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Big Smile Big Smile Big Smile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<%@page contentType="image/jpeg;charset=big5"%>
<%@page import="java.awt.*"
        import="javax.swing.*"
        import="javax.imageio.*"
        import="java.awt.image.*" %>
<%
    String [] imgname = {"face1.gif", "face2.png", "face3.jpg"};
    ImageIcon icon[] = new ImageIcon[imgname.length];
    Image img[] = new Image[imgname.length];
 
    for(int i=0; i<imgname.length; i++){
        // ImageIcon 物件在完全讀入圖檔或發生讀取錯誤時才會繼續
        icon[i] = new ImageIcon(application.getRealPath(imgname[i]));
        img[i] = icon[i].getImage();
    }
 
/*  // 測試圖檔是否有載入啦
    System.out.println(icon[2].getImageLoadStatus()==MediaTracker.ABORTED);
    System.out.println(icon[2].getImageLoadStatus()==MediaTracker.ERRORED);
    System.out.println(icon[2].getImageLoadStatus()==MediaTracker.COMPLETE);
*/
 
    BufferedImage bi = new BufferedImage(149, 190, BufferedImage.TYPE_INT_RGB );
    Graphics g = bi.getGraphics();
 
    // 是否有畫進去啦
    System.out.println("draw img[2]:"+g.drawImage(img[2], 0, 0, null));
    System.out.println("draw img[0]:"+g.drawImage(img[0], 10, 10, null));
    System.out.println("draw img[1]:"+g.drawImage(img[1], 100, 50, null));
 
    g.setColor(Color.white);
    g.drawString("終於成功 ^^y", 50, 100);
    g.dispose();
    response.reset();
 
    ServletOutputStream os = response.getOutputStream();
    ImageIO.write(bi, "jpeg", os);
    os.close();
%>


(縮略圖,點擊圖片鏈接看原圖)


reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:qop]
liminjun

JavaDOCHelper真讚



發文: 496
積分: 5
於 2004-01-07 12:26 user profilesend a private message to usersend email to liminjunreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
用 ImageIcon 的話png讀取的時候沒有出現之前那個問題
不過用ImageIO 讀取同一張png 卻出現java.lang.IllegalArgumentException: LUT has improper length!

您曾經嚐試過 ImageIO 讀取 透明底色PNG 出現上述問題嗎?
實在很想知道該怎麼解決


liminjun edited on 2004-01-07 13:19
reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:liminjun]
Duncan

還隱隱作痛

版主

發文: 7816
積分: 39
於 2004-01-07 12:57 user profilesend a private message to usersend email to Duncanreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
liminjun wrote:
(2)下面這個問題該如何解決?
1
2
3
4
5
6
7
An error occurred at line: 10 in the jsp file: /SafeCode.jsp
 
Generated servlet error:
C:\Server\Tomcat\work\Catalina\localhost\_\org\apache\jsp\SafeCode_jsp.java:112: cannot resolve symbol
symbol  : method createImage (int,int)
location: class org.apache.jsp.SafeCode_jsp
    offimg = createImage(200, 20);



因為你的 jsp 本來就沒有 createImage method(之前你的程式 extends Frame/JFrame 沒問題,是由於 java.awt.Component 有 createImage 這一規格)。


reply to postreply to post

給我
辣味豆腐 其餘免談
作者 Re:請問如何處理GIF PNG的透明色 [Re:liminjun]
qop

Newbie



發文: 204
積分: 3
於 2004-01-07 13:15 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
liminjun wrote:
用 ImageIcon 的話png讀取的時候沒有出現之前那個問題
不過用ImageIO 讀取同一張png 卻出現java.lang.IllegalArgumentException: LUT has improper length!

您曾經嚐試過 ImageIO 讀取 透明底色PNG 出現上述問題嗎?
該怎麼解決呢

沒哩
偶覺得用 ImageIO 讀圖檔有點麻煩....Smile


reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:liminjun]
liminjun

JavaDOCHelper真讚



發文: 496
積分: 5
於 2004-01-07 13:23 user profilesend a private message to usersend email to liminjunreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
雖然已經可以用 ImageIcon 讀PNG圖檔,但是使用ImageIO 讀取透明底圖PNG仍然還是有點小問題 ,很想知道如何解決

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
exception 
 
org.apache.jasper.JasperException: LUT has improper length!
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
 
root cause 
 
java.lang.IllegalArgumentException: LUT has improper length!
  javax.imageio.ImageTypeSpecifier$Indexed.(Unknown Source)
  javax.imageio.ImageTypeSpecifier.createIndexed(Unknown Source)
  com.sun.imageio.plugins.png.PNGImageReader.getImageTypes(Unknown Source)
  com.sun.imageio.plugins.png.PNGImageReader.readImage(Unknown Source)
  com.sun.imageio.plugins.png.PNGImageReader.read(Unknown Source)
  javax.imageio.ImageIO.read(Unknown Source)
  javax.imageio.ImageIO.read(Unknown Source)
  org.apache.jsp.SafeCode_jsp._jspService(SafeCode_jsp.java:84)
  org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


最後,想請問的是,ImageIO 和 ImageIcon 的效率哪個比較好?


liminjun edited on 2004-01-07 13:26
reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:liminjun]
liminjun

JavaDOCHelper真讚



發文: 496
積分: 5
於 2004-01-07 16:20 user profilesend a private message to usersend email to liminjunreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
liminjun wrote:
雖然已經可以用 ImageIcon 讀PNG圖檔,但是使用ImageIO 讀取透明底圖PNG仍然還是有點小問題 ,很想知道如何解決

最後,想請問的是,ImageIO 和 ImageIcon 的效率哪個比較好?


我自己寫了兩個jsp小程式來測試(同樣的圖,同樣的合成動作,同樣的輸出檔案)
(1)ImageIO讀取圖檔 => BufferedImage => Graphics合成 =>BufferedImage => ImageIO輸出jpg,
執行1000次,總共花費16485 milli-seconds
(2)ImageIcon讀取圖檔 => Image => Graphics合成 =>BufferedImage => ImageIO輸出jpg,
執行1000次,總共花費1172 milli-seconds

效率上差了10倍之多

不知道這個結論是否錯誤,能否請知道的大大解答一下


liminjun edited on 2004-01-07 17:15
reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:liminjun]
qop

Newbie



發文: 204
積分: 3
於 2004-01-07 16:38 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
BufferedImage 佔用較多資源, 慢是一定的啦
而且從「ImageIO讀取圖檔 => BufferedImage => Graphics合成 」沒看到你的 code 也不知道你做了什麼處理。

ps:1. 從 ImageIcon 取得的 Image 物件居然屬於 sun.awt.windows.WImage 類別
這是 sun 暗康的類別嗎 ? 查不到其 doc~

ps:2. 為什麼 BufferedImage 沒有以下的這類建構子 >"<
BufferedImage(File filename)
BufferedImage(URL url)
這樣應該會容易粉多......Tongue


reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:qop]
liminjun

JavaDOCHelper真讚



發文: 496
積分: 5
於 2004-01-07 17:20 user profilesend a private message to usersend email to liminjunreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
qop wrote:
ps:1. 從 ImageIcon 取得的 Image 物件居然屬於 sun.awt.windows.WImage 類別
這是 sun 暗康的類別嗎 ? 查不到其 doc~

ps:2. 為什麼 BufferedImage 沒有以下的這類建構子 >"<
BufferedImage(File filename)
BufferedImage(URL url)
這樣應該會容易粉多......Tongue


我在java doc 查到以下結果
javax.swing.ImageIcon

public Image getImage()
Returns this icon's Image.
Returns:the Image object for this ImageIcon


liminjun edited on 2004-01-07 17:26
reply to postreply to post
作者 Re:請問如何處理GIF PNG的透明色 [Re:liminjun]
th_kai





發文: 79
積分: 0
於 2007-03-05 16:24 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
liminjun wrote:
你的程式了我一點靈感,非常感謝^^,我用下面列出的方法就可以繪出透明色圖形了,只需用到BufferedImage ImageIO Graphics2D 就OK了,不用花繁雜的手續將Image轉BufferedImage

Image,BufferedImage,Byte[] 都可以用drawImage 畫在 BufferedImage上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    int width = 200;
    int height = 20;
 
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB );
    Graphics2D g2d = bufferedImage.createGraphics();
 
    // Draw graphics
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, width, height);
    g2d.drawImage( bg , null , 0 , 0);
    g2d.drawImage( image0 , null ,0 , 0 );
    g2d.drawImage( image1 , null ,20 , 0 );
    g2d.drawImage( image2 , null ,40 , 0 );
    g2d.drawImage( image3 , null ,60 , 0 );
    g2d.drawImage( image4 , null ,80 , 0 );
    g2d.drawImage( image5 , null ,100 , 0 );
    g2d.drawImage( image6 , null ,120 , 0 );
    g2d.drawImage( image7 , null ,140 , 0 );
    g2d.drawImage( image8 , null ,160 , 0 );
    g2d.drawImage( image9 , null ,180 , 0 );
 
    // Graphics context no longer needed so dispose it
    g2d.dispose();


後記:sun java DOC 在某些情況查資料的確方便,但是因為範例不完整,寫程式的時候無法從範例中得到完整的架構觀念,以致於常常為了做某些動作而海底撈針...這點比起Ms 的 MSDN文檔的確差滿多的,進階技術學習門檻感覺稍微高了點..


請問~~
這個的結果是透明的底,還是白色的底??
@@" 我用出來都是白底,用不出透明

謝謝


reply to postreply to post
» JWorld@TW »  Java SE 討論區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8