2012-04-01 54 views
0

可能重复:
I am making some parts of java image transparent by some code, it works fine on the laptop I made, but not on others, Why?下面的java代码用于将透明度放入特定的颜色,但在我的笔记本电脑上运行,但不会在其他具有更好显卡的笔记本电脑上运行,为什么?

图片测试:原始图片,
图片testt:应用透明度

在我的笔记本电脑后的图像,颜色我想使透明变得透明,测试以及测试都被渲染,但是如果未绘制测试图像,则绘制测试,但是测试不是。

与透明度绘制的简单图像的完整代码:

import java.applet.Applet; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseMotionListener; 
import java.awt.image.FilteredImageSource; 
import java.awt.image.ImageFilter; 
import java.awt.image.ImageProducer; 
import java.awt.image.RGBImageFilter; 
import java.io.Serializable; 


public class test extends Applet implements Runnable 
{ 
    public static Image makeColorTransparent(Image im, final Color color) 
     { 
     ImageFilter filter = new RGBImageFilter() 
     { 

       public int markerRGB = color.getRGB() | 0xFF000000;    //color to make transparent 

       public final int filterRGB(int x, int y, int rgb) 
       { 
       if ((rgb | 0xFF000000) == markerRGB) 
       { 
        // Mark the alpha bits as zero - transparent 
        return 0x00FFFFFF & rgb; 
       } 

       else 
       { 
        // nothing to do 
        return rgb; 
       } 
      } 
     }; 

      ImageProducer ip = new FilteredImageSource(im.getSource(), filter); 
      return Toolkit.getDefaultToolkit().createImage(ip); 

     } 


    Image test; 
    public void init() 
    { 

     setSize(600,600); 

    } 

    public void update (Graphics g)            //overriding the update for double buffering 
    {  
     // initialize buffer 
     Image dbImage = createImage (this.getSize().width, this.getSize().height); 
     Graphics dbg = dbImage.getGraphics(); 


     // clear screen in background 
     dbg.setColor (getBackground()); 
     dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); 


     // draw elements in background 
     dbg.setColor (getForeground()); 
     paint (dbg); 

     // draw image on the screen 
     g.drawImage (dbImage, 0, 0, this);  
    } 


    public void paint(Graphics g) 
    { 
     test = getImage(getCodeBase(), "tt.gif"); 
     Image testt = makeColorTransparent(test, Color.white);    

     g.drawImage (testt,0,0, this); 
    } 


    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
     repaint(); 

    } 

} 
+0

难道有事可做以32/64位环境?我不确定64位Java是否使用64位颜色,但如果它确实如此,那么可能是您的问题,因为位掩码已被锁定。 – Accatyyc 2012-04-01 07:11:17

+0

你确定,文件“tt。gif“在您启动应用程序的其他计算机上是否存在?并且在应用程序找到它所需的路径上? – yggdraa 2012-04-01 07:16:47

+0

是的,tt.gif位于两台计算机的项目文件夹的bin文件夹中... – 2012-04-01 08:20:02

回答

0

使用PhiLho; S解决方案How to make a color transparent in a BufferedImage and save as PNG

private Image TransformColorToTransparency(BufferedImage image, Color c1, Color c2) 
    { 
    // Primitive test, just an example 
    final int r1 = c1.getRed(); 
    final int g1 = c1.getGreen(); 
    final int b1 = c1.getBlue(); 
    final int r2 = c2.getRed(); 
    final int g2 = c2.getGreen(); 
    final int b2 = c2.getBlue(); 
    ImageFilter filter = new RGBImageFilter() 
    { 
     public final int filterRGB(int x, int y, int rgb) 
     { 
     int r = (rgb & 0xFF0000) >> 16; 
     int g = (rgb & 0xFF00) >> 8; 
     int b = rgb & 0xFF; 
     if (r >= r1 && r <= r2 && 
      g >= g1 && g <= g2 && 
      b >= b1 && b <= b2) 
     { 
      // Set fully transparent but keep color 
      return rgb & 0xFFFFFF; 
     } 
     return rgb; 
     } 
    }; 

    ImageProducer ip = new FilteredImageSource(image.getSource(), filter); 
     return Toolkit.getDefaultToolkit().createImage(ip); 
    } 

或者,http://marxsoftware.blogspot.com/2011/04/making-white-image-backgrounds.html

public static Image makeColorTransparent(final BufferedImage im, final Color color) 
    { 
     final ImageFilter filter = new RGBImageFilter() 
     { 
     // the color we are looking for (white)... Alpha bits are set to opaque 
     public int markerRGB = color.getRGB() | 0xFFFFFFFF; 

     public final int filterRGB(final int x, final int y, final int rgb) 
     { 
      if ((rgb | 0xFF000000) == markerRGB) 
      { 
       // Mark the alpha bits as zero - transparent 
       return 0x00FFFFFF & rgb; 
      } 
      else 
      { 
       // nothing to do 
       return rgb; 
      } 
     } 
     }; 

     final ImageProducer ip = new FilteredImageSource(im.getSource(), filter); 
     return Toolkit.getDefaultToolkit().createImage(ip); 
    } 
} 

显然,差异在你的问题和第二个解决方案之间是这里:

public int markerRGB = color.getRGB() | 0xFF000000; 

其中,您正在寻找黑色以设置为透明色,而不是白色。

MIT的解决方案http://web.mit.edu/javalib/www/examples/if/start.html

class TransparentFilter extends RGBImageFilter { 

    final static int aShift=24; 
    final static int rShift=16; 
    final static int gShift=8; 
    final static int bShift=0; 
    final static int aMask=0xff<<aShift; 
    final static int rMask=0xff<<rShift; 
    final static int gMask=0xff<<gShift; 
    final static int bMask=0xff<<bShift; 
    final static int rgbMask=rMask|gMask|bMask; 

    // this fudge value is used in place of the harder (but proper) task of 
    // performing statistical analysis of the saturation values (and 
    // possibly distances) of highly-saturated pixels contained in the image 
    float saturationFudge; 

    TransparentFilter(float saturationFudge) { 
     this.saturationFudge=saturationFudge; 
     canFilterIndexColorModel=true; 
    } 

    public int filterRGB(int x, int y, int argb) { 
     // separate the three colour channels (ignore incoming alpha) 
     int r=(argb&rMask) >>> TransparentFilter.rShift; 
     int g=(argb&gMask) >>> TransparentFilter.gShift; 
     int b=(argb&bMask) >>> TransparentFilter.bShift; 
     // convert to hsb so that we can use saturation to 
     // establish the alpha (transparency) value of the pixel 
     float[] hsb=Color.RGBtoHSB(r,g,b,null); 
     float fa=255f*hsb[1]/this.saturationFudge; 
     int a=Math.max(0,Math.min(255,Math.round(fa)))<<TransparentFilter.aShift; 
     return a|(argb&TransparentFilter.rgbMask); 
    } 

} 

不过,如果所有的解决方案并没有在测试系统上正常工作,我们就可以怀疑这是我们不都根据你原来的职位足够的信息,其他可能的罪犯:

  1. 操作系统版本32/64位?
  2. JRE/JDK版本32/64位?
  3. GPU:硬件和驱动程序?
  4. 显示:256位/ 16位/ 24位?

此外,我们不知道你在这些系统之间观察到什么,因为没有提供截图。

最糟糕的是,您必须开始使用透明遮罩已设置的图像来解决问题,而不是依赖编码其透明度位(除非您尝试创建基于Java的照片编辑应用程序需要透明度过滤)。

可能解决的相关问题??:

  1. Java Swing transparency drawing issues
相关问题