views:

1656

answers:

2

So I have been posting all over and have yet to get a solid answer:

I have created an image resizing class, with a crop method. The cropping works great. The issue that I am having is the background color that I specify in the drawImage function of Graphics is not working correctly. It defaults to black as the background regardless of what I supply (in this case Color.WHITE).

Also, the overlaying image or top most image (comes from a file) is being inverted (I think it is) or otherwise discolored. Just so you can conceptualize this a little bit better, I am taking a jpeg and overlaying it on top of a new BufferedImage, the new buffered image's background is not being set. Here is the code below that I am working with:

public void Crop(int Height, int Width, int SourceX, int SourceY) throws Exception {
    //output height and width
    int OutputWidth = this.OutputImage.getWidth();
    int OutputHeight = this.OutputImage.getHeight();

    //create output streams
    ByteArrayOutputStream MyByteArrayOutputStream = new ByteArrayOutputStream();
    MemoryCacheImageOutputStream MyMemoryCacheImageOutputStream = new MemoryCacheImageOutputStream(MyByteArrayOutputStream);

    //Create a new  BufferedImage
    BufferedImage NewImage = new BufferedImage(Width, Height, BufferedImage.TYPE_INT_RGB);
    Graphics MyGraphics = NewImage.createGraphics();

    MyGraphics.drawImage(this.OutputImage, -SourceX, -SourceY, OutputWidth, OutputHeight, Color.WHITE, null);

    // Get Writer and set compression
    Iterator MyIterator = ImageIO.getImageWritersByFormatName("png");
    if (MyIterator.hasNext()) {
        //get image writer
        ImageWriter MyImageWriter = (ImageWriter)MyIterator.next();

        //get params
        ImageWriteParam MyImageWriteParam = MyImageWriter.getDefaultWriteParam();

        //set outputstream
        MyImageWriter.setOutput(MyMemoryCacheImageOutputStream);

        //create new ioimage
        IIOImage MyIIOImage = new IIOImage(NewImage, null, null);

        //write new image
        MyImageWriter.write(null, MyIIOImage, MyImageWriteParam);
    }

    //convert output stream back to inputstream
    ByteArrayInputStream MyByteArrayInputStream = new ByteArrayInputStream(MyByteArrayOutputStream.toByteArray());
    MemoryCacheImageInputStream MyMemoryCacheImageInputStream = new MemoryCacheImageInputStream(MyByteArrayInputStream);

    //resassign as a buffered image
    this.OutputImage = ImageIO.read(MyMemoryCacheImageInputStream);
}
A: 

Can you isolate whether it's the Graphics methods or the ImageIO methods that are mangling your image? It looks like you could test this by short-circuiting the entire ImageIO process and simply assigning

this.OutputImage = NewImage;

For that matter, I assume there's something gained by the ImageIO operations? As the sample is written, it appears to be (ideally) a no-op.

Also, you don't dispose your Graphics2D before you begin the ImageIO process. It often doesn't make a difference, but you don't want to assume that.

Michael Brewer-Davis
Thanks for the reply, much appreciatedAs far as isolating, its weird, i removed all the over lay stuff and just created a new buffered image and setBackgroundColor and assigned to this.OutputImage and i still get a black background. Also, just to clearify, I am using Graphics - not Graphics2D.
What's the new code look like? Setting the background color alone won't change the image--it's only used in the clear operations (clearRect, possibly Graphics2D composites?).
Michael Brewer-Davis
A: 

On the overlay color distortion problem, make sure your graphics context is in paint mode and not xor mode. (Graphics.setPaintMode()). Otherwise the color bits are XOR'd together.

Software Monkey
Here is the solution: //set background MyGraphics.setBackground(Color.decode(this.BackgroundColor)); //clear rect MyGraphics.clearRect(0, 0, Width, Height);Thanks for all your help !