简体   繁体   中英

Java: How do I detect the format and read the RGB values of each pixel from an image?

I have been writing a program for batch image processing. The images come in various formats, but all are either JPEG, TIFF, or PNG. I wrote the following class for storing and accessing pixel data:

package imageReader;

import misc.NumberFunctions;

/**
 * A class to store pixel data and provide access to some of the pixel's     basic
 * attributes
 * 
 * @author Thomas
 *
 */
public class Pixel {

    /**
     * The X-coordinate of the pixel The left-most column of the image     is x=0
     */
    private int x;

    /**
     * The Y-coordinate of the pixel The top row of the image is y=0
     */
    private int y;

    /**
     * The red value of the pixel (from 0 to 255)
     */
    private int r;

    /**
     * The green value of the pixel (from 0 to 255)
     */
    private int g;

    /**
     * The blue value of the pixel (from 0 to 255)
     */
    private int b;

    /**
     * The highest color value in the pixel, which determine its brightness
     * (from 0 to 255)
     */
    private int value;

    /**
     * Creates an empty pixel object, setting it to black (0,0,0)
     * 
     * @param x
     *            the x-coordinate of the pixel
     * @param y
     *            the y-coordinate of the pixel
     */
    public Pixel(int x, int y) {
        this.x = x;
        this.y = y;
        this.r = 0;
        this.g = 0;
        this.b = 0;
        this.setValue();
    }

    /**
     * Get the x-coordinate of the pixel
     * 
     * @return the x-coordinate
     */
    public int getX() {
        return this.x;
    }

    /**
     * Get the y-coordinate of the pixel
     * 
     * @return the y-coordinate
     */
    public int getY() {
        return this.y;
    }

    /**
     * Get the red value of the pixel
     * 
     * @return the red value (0 - 255)
     */
    public int getRed() {
        return this.r;
    }

    /**
     * Get the green value of the pixel
     * 
     * @return the green value (0 - 255)
     */
    public int getGreen() {
        return this.g;
    }

    /**
     * Get the blue value of the pixel
     * 
     * @return the blue value (0 - 255)
     */
    public int getBlue() {
        return this.b;
    }

    /**
     * Get the brightness of the pixel
     * 
     * @return the value (0 - 255)
     */
    public int getValue() {
        return this.value;
    }

    /**
     * Get the brightness of the pixel
     * 
     * @return the value (0 - 100)
     */
    public double getValue100() {
        double value100 = ((double) this.value) / 2.55;
        return value100;
    }

    /**
     * Check if this Pixel object is set to the same location as another
     * 
     * @param otherPixel
     *            the other pixel
     * @return whether they share the same coordinates or not
     */
    public boolean hasSameLocation(Pixel otherPixel) {
        boolean xMatches = (this.getX() == otherPixel.getX());
        boolean yMatches = (this.getY() == otherPixel.getY());
        return (xMatches && yMatches);
    }

    /**
     * Checks if this pixel has the same color as another pixel
     * 
     * @param otherPixel
     *            the other pixel
     * @return whether they share the same color or not
     */
    public boolean hasSameColor(Pixel otherPixel) {
        if (this.getValue() != otherPixel.getValue())
            return false;
        boolean redMatches = (this.getRed() == otherPixel.getRed());
        boolean greenMatches = (this.getGreen() == otherPixel.getGreen());
        boolean blueMatches = (this.getBlue() == otherPixel.getBlue());
        if (redMatches && greenMatches && blueMatches)
            return true;
        else
            return false;
    }

    /**
     * An internal function for finding the brightness value of the pixel
     */
    private void setValue() {
        this.value = NumberFunctions.getMax(r, g, b);
    }
}

I would like to import the images (I can already get the paths for all of them) and then record each image's data in a two dimensional array of Pixel objects (Pixel[][]). To do this, I need your guys' help to do three things:

  1. Determine the image type and import it

  2. Find out the image's dimensions

  3. Read the RGB values of each individual pixel

I appreciate any help you guys can give me. Thanks!

Use a BufferedImage :

try {
    BufferedImage image = ImageIO.read(new File("monkey.jpg")); // TODO actual file
    int width = image.getWidth();
    int height = image.getHeight();

    // TODO do whatever you want with image, for example get an rgb:
    Color color = new Color(image.getRgb(0,0));
} catch (IOException e) {
    // TODO handle
}

By the way, this is ugly code:

if (redMatches && greenMatches && blueMatches)
        return true;
    else
        return false;

Replace it by:

return redMatches && greenMatches && blueMatches;

You can use this code to import an image given a file name. This can throw an IOException so be mindful of that.

String path = "Image1.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);

Image type is not important for this, ImageIO will automatically detect it. If you want type after reading use image.getType()

The image dimensions can be taken from BufferedImage

int width = image.getWidth();
int height = image.getHeight();

Pixels can be read with image.getRGB(int x, int y) which returns an integer color. You can convert this to java.awt.color with the constructor Color c = new Color(int rgb) Then it is a simple process to get the channels with c.getRed() c.getGreen() c.getBlue(0)

If I can give you some advice, don't duplicate the data. Instead of building Pixel s, just write your methods to get data using the methods above. As other people pointed out, your implementation will take up a lot more memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM