Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

BufferedImage to file - Where is the file?

807569Jun 27 2006 — edited Jun 28 2006
I am trying to make a simple program that loads an image, transforms it to grayscale and then save it to a file. Although it seems to work the file isn't being created (or at least i can't find it in my computer...)
import javax.imageio.*;
import java.awt.*;
import java.awt.Image;
import java.io.*;
import javax.swing.*;
import java.awt.image.BufferedImage;

public class ImageProcessing {

    public static void main(String[] args) {
        try {
            ImageProcessing imageprocessing = new ImageProcessing();
        } catch (Exception e) {}
    }


    public ImageProcessing() throws Exception {
        try {
            BufferedImage inImage = ImageIO.read(new File("myImage.jpeg"));
            
            int width = inImage.getWidth(null);
            System.out.println(width);

            int height = inImage.getHeight(null);
            System.out.println(height);

            int[] rgbs = new int[width * height];
            inImage.getRGB(0, 0, width, height, rgbs, 0, width);

            int[] grayscale = new int[width * height];
            int[] value = new int[width * height];

            for (int j = 0; j < height; j++) {
                for (int i = 0; i < width; i++) {
                    int alpha = (rgbs[j * width + i] >> 24) & 0xff;
                    int red = (rgbs[j * width + i] >> 16) & 0xff;
                    int green = (rgbs[j * width + i] >> 8) & 0xff;
                    int blue = (rgbs[j * width + i]) & 0xff;
                    value[j * width + i] = (red + green + blue);
                    if (value[j * width + i] > 255) {
                        grayscale[j * width + i] = 85;
                    }else
                        grayscale[j * width + i] = value[j * width + i]/3;
                }
                
             }
            
            BufferedImage outImage = null;
            outImage.setRGB(0, 0, width, height, grayscale, 0, width);
            ImageIO.write(outImage, "jpg", new File("c:/myImage2.jpeg"));
            
        } catch (Exception e) {}

    }
}

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 26 2006
Added on Jun 27 2006
7 comments
194 views