简体   繁体   中英

RGBImageFilter Produces All Black Empty Images

I'm trying to use the RGBImageFilter to produce a simple transparency effect on an image, however during testing I found that all of my filtered images come out as blank with black backgound. I simplified my filter to a simple "No Operation" filter that just returns the RGB value that was passed in. Even this simplified NoOp filter produces a blank image. I've tried this with importing and exporting both JPG and PNG with the same affect. I've tried various example filters off the internet that produce the same all black image problem. Has anyone encountered this before? The "load status" and the "write status" below are both returning true so I know from MediaTracker that the original image is loading fully and that the produced image is writing successfully. Thanks in advance for any help!

Here is the code:

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

class NoOpFilter extends RGBImageFilter {
public NoOpFilter() {
    canFilterIndexColorModel = true;
}

    public int filterRGB(int x, int y, int rgb) {       
    return rgb;
    }




    public static void main(String[] args) throws Exception{
    String originalImageLocation = args[0];
    String baseFileName = args[1];
    String directory = args[2];         
    ImageIcon originalImage = new               ImageIcon(Toolkit.getDefaultToolkit().getImage(originalImageLocation));

    NoOpFilter filter2 = new NoOpFilter();


    ImageProducer ip = new FilteredImageSource(originalImage.getImage().getSource(), filter2);
        Image filteredImage = Toolkit.getDefaultToolkit().createImage(ip);

        System.out.println("Load Status: " + ( originalImage.getImageLoadStatus() == java.awt.MediaTracker.COMPLETE));

        BufferedImage bim = new BufferedImage( originalImage.getIconWidth(),originalImage.getIconHeight(), BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2 = bim.createGraphics();
        g2.drawImage(filteredImage, 0, 0, null);
        g2.dispose();
        File file = new File(directory + File.separator + baseFileName + ".png");
        file.createNewFile();
        boolean status = ImageIO.write(bim, "png", file);
        System.out.print("write() status: " + status);

    }

}

Here is the fix that worked for me. Right after creating the fileredImage above, it was necessary to actually add the filtered image to a JLabel and the JLabel to a JFrame in order to force it to be rendered to screen first before writing it into my buffer and saving to disk:

...
Image filteredImage = Toolkit.getDefaultToolkit().createImage(ip);          
ImageIcon swingImage = new ImageIcon(filteredImage);
JFrame frame = new JFrame();
JLabel label = new JLabel(swingImage);
frame.add(label);
frame.setVisible(true);
frame.dispose();
BufferedImage bim = new BufferedImage( originalImage.getIconWidth(),originalImage.getIconHeight(), BufferedImage.TYPE_INT_ARGB);

Graphics2D g2 = bim.createGraphics();
g2.drawImage(filteredImage, 0, 0, null);
g2.dispose();
File file = new File(directory + File.separator + baseFileName + ".png");
file.createNewFile();
boolean status = ImageIO.write(bim, "png", file);
System.out.print("write() status: " + status);
}

I don't quite yet understand why this can't be done completely off-screen like I wanted to do, however it was easy enough to dispose the JFrame right after opening it. A better solution would not require using UI components to do this since I don't really need anything displayed on screen in this case and just want the end product.

Thanks to Hovercraft for pointing me in the right direction.

You have to call prepareImage, then you don't have to use the JLabel-workaround.

Image filteredImage = Toolkit.getDefaultToolkit().createImage(ip);
Toolkit.getDefaultToolkit().prepareImage(filteredImage, -1, -1, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM