2012-09-10 146 views
1

我有一個帶有透明背景的PNG圖像,將被添加到另一個圖像。具有透明背景的javacv圖像

我的問題是,當我加載了IplImage背景是不是透明的了 - 會變成白色。

如何在javacv中使用具有透明背景的圖像?

IplImage src = cvLoadImage("2.png"); 
IplImage tmp = cvLoadImage("1.png"); 
cvSetImageROI(src, cvRect(41,28,tmp.width(),tmp.height())); // not the same size 
cvShowImage("1", src); //before 
cvCopy(src, tmp); 
cvShowImage("2", src); //after 
cvWaitKey(0); 
cvResetImageROI(src); 

tryed添加阿爾法CHANNL但沒有工作:

Graphics g=src.getBufferedImage().getGraphics(); 
     Graphics2D g2d = (Graphics2D)g; 
     g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 
                10 * 0.1f)); 
     BufferedImage a = new BufferedImage(tmp.width(), tmp.height(), BufferedImage.TYPE_INT_ARGB); 

     src = IplImage.createFrom(a); 
+1

如果BG圖像沒有alpha通道,則Graphics對象將不支持繪製其他圖像的透明度。嘗試先將BG圖像寫入相同大小但帶有alpha的圖像,然後將FG圖像寫入該圖像。 –

+0

我該怎麼做:)首先將BG圖像寫入相同大小但帶有alpha的圖像? – user1246950

+0

我嘗試了java2d(從未使用它,因此不是100%確定我做的是正確的事情)添加代碼發佈我嘗試過但它沒有工作,當我只做到一個它給了我excption敏感他們沒有相同的數字的信道所以我做了另一個,但我只是得到了2個黑色圖像;/ – user1246950

回答

2

THX安德魯你是對阿爾法thinge:)沒帶我多一點serching發現,但在這裏它的工作原理相同的事情: )

public static void combine() 
{ 
    try{ 
       File path = new File("D:/proj2/javacv2"); 

    // load source images 

     BufferedImage image = ImageIO.read(new File(path, "3.jpg")); 

     BufferedImage overlay = ImageIO.read(new File(path, "test4a.png")); 

    // BufferedImage image=src.getBufferedImage(); 
    // BufferedImage overlay =tmp.getBufferedImage(); 
    // create the new image, canvas size is the max. of both image sizes 
    int w = Math.max(image.getWidth(), overlay.getWidth()); 
    int h = Math.max(image.getHeight(), overlay.getHeight()); 
     //int w=tmp.width(); 
    // int h=tmp.height(); 
    BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 

    // paint both images, preserving the alpha channels 
    Graphics g = combined.getGraphics(); 
    g.drawImage(image, 0, 0, null); 
    g.drawImage(overlay, 41, 30, null); 

    // Save as new image 

     ImageIO.write(combined, "PNG", new File(path, "combined.png")); 
    }catch(Exception e) 
    { 
     System.out.println("exception "); 
    } 

} 
+0

很高興你把它分類。 :)當你有機會時,請[接受](http://meta.stackexchange.com/a/65088/155831)答案。 –

+0

我不能發表評論,我不能認爲我自己的答案:P – user1246950

+0

我已經接受了我自己的答案。 SO讓你等得更久,才能做到這一點。 ;) –