2012-09-10 141 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让你等得更久,才能做到这一点。 ;) –