15

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?

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
John Fox
  • 171
  • 1
  • 1
  • 6

4 Answers4

2

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.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
-2

I know is an old question, but maybe it can be useful to someone, I think you shoud use this

graph.drawImage(x,y,width,height); //First you draw the image
graph.setColor(Color.black); //Then set the color to black
graph.fillRect(img.getX(), img.getY(), img.getWidth(), img.getHeight());// Finally draw a black rectangle on it

By the way is hard to find a solution without a little more code. Hope it will be usefull.

Lucarnosky
  • 514
  • 4
  • 18
-2

Very late to this answer but you are saving the image and not the graph you are creating. I think it must be a BufferedImage again to save

-3

You have just to replace this line:

Graphics2D graph = img.createGraphics();

with this:

Graphics2D graph = img.getGraphics();
Barett
  • 5,826
  • 6
  • 51
  • 55
  • 1
    Not according to the docs http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html The op has it right. – nycynik Jul 19 '15 at 19:06