Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing Filled Rectangle over a BufferedImage

So I am attempting to create an application that can black-out sections of a survey that contains sensitive information. However I've run into a bit of a problem.

What I want to do is draw filled black rectangles over a BufferedImage given x, y, width, and height of desired region to black out, then write that new image back to my filesystem. Here's my code.

File imageFile = new File("images/template.jpg");
BufferedImage img = ImageIO.read(imageFile);
        
Graphics2D graph = img.createGraphics();
graph.setColor(Color.BLACK);
graph.fill(new Rectangle(x, y, width, height));
graph.dispose();
        
ImageIO.write(img, "jpg", new File("images/template.jpg"));

For whatever reason the image in the resource doesn't change after this code segment. Any ideas on what I'm doing wrong?

like image 445
John Fox Avatar asked Jul 24 '12 16:07

John Fox


People also ask

How do you draw on BufferedImage?

To Draw on a BufferedImage To draw on anything, you need a Graphics or Graphics2D drawing context. You can get a graphics context from an image like this: Graphics2D gp = myImage. createGraphics(); gp.

What does BufferedImage mean?

A BufferedImage is comprised of a ColorModel and a Raster of image data. The number and types of bands in the SampleModel of the Raster must match the number and types required by the ColorModel to represent its color and alpha components. All BufferedImage objects have an upper left corner coordinate of (0, 0).

What is the difference between BufferedImage and image?

A BufferedImage is essentially an Image with an accessible data buffer. It is therefore more efficient to work directly with BufferedImage. A BufferedImage has a ColorModel and a Raster of image data. The ColorModel provides a color interpretation of the image's pixel data.

What is the use of BufferedImage?

It is used to handle and manipulate the image data. A BufferedImage is made of ColorModel of image data. All BufferedImage objects have an upper left corner coordinate of (0, 0).


1 Answers

I created a runnable example of your code, and it worked fine for me. I ran this code using Java 8.

Here's the altered image. I drew the black square on an image I had.

Altered Image

And here's the code I ran. I read the original image directly from my file system.

package com.ggl.testing;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageProcessing implements Runnable {

    public static void main(String[] args) {
        new ImageProcessing().run();
    }

    @Override
    public void run() {
        File imageFile = new File("C:\\Users\\Owner\\Pictures\\Saved Pictures\\Analog Clock Calendar.jpg");
        BufferedImage img;
        try {
            img = ImageIO.read(imageFile);
        } catch (IOException e1) {
            e1.printStackTrace();
            return;
        }

        Graphics2D graph = img.createGraphics();
        graph.setColor(Color.BLACK);
        graph.fill(new Rectangle(100, 100, 100, 100));
        graph.dispose();

        try {
            ImageIO.write(img, "jpg", 
                    new File("altered.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

My conclusion is that you either didn't read the image correctly, your x, y, width, and/or height were outside the limits of the image, or something else that I'm missing.

like image 136
Gilbert Le Blanc Avatar answered Oct 15 '22 14:10

Gilbert Le Blanc