-3

Here is the code of the concerned method and it's use :

Image newImage = null;
convertToGrayscale(image, newImage);

private Image convertToGrayscale(BufferedImage image, Image newImage) {
//  try {
    for (int i = 0; i < image.getHeight(); i++) {
        for (int j = 0; j < image.getWidth(); j++) {
            Color imageColor = new Color(image.getRGB(j, i));
            int rgb = (int) (imageColor.getRed() * 0.299)
               + (int) (imageColor.getGreen() * 0.587)
               + (int) (imageColor.getBlue() * 0.114);
            Color newColor = new Color(rgb, rgb, rgb);
            image.setRGB(j, i, newColor.getRGB());
        }
    }
    newImage = image;
    return newImage;
    //ImageIO.write(image, "jpg", (ImageOutputStream) newImage);
    //ImageIO.write(image, "jpg", new Image(newImage));
    /*} catch (IOException e) {
        e.printStackTrace();
    }*/
}

I'm passing an image to method convertToGrayScale and want to return the modified image.

Joachim Huet
  • 422
  • 3
  • 10
  • what is your question? what did you try what doesn't work? – Vad1mo Nov 06 '17 at 11:30
  • I'm unable to get the modified image and draw it next to the original. The convert method isn't returning the image. Cause im able to print the image with out the converttoGrayscale method – Mokgetheng Mothibedi Nov 06 '17 at 12:38

1 Answers1

0

Your question was quite a mess, try being clearer next time ^^

Otherwise you are returning the newImage so you don't have to pass it as an argument of the function but what you should be doing is getting this return value in the other function :

Image newImage = convertToGrayscale(image/*, newImage --> No need for this one here*/);

You can read more about by-value or by-reference passing in Java methods in this SO question if you really want to pass your Image to be changed as argument of the method.

Hope it helps

Joachim Huet
  • 422
  • 3
  • 10