Image Processing in Java

Go To StackoverFlow.com

0

I want to extract the pixel values of the jpeg image using the JAVA language, and need to store it in array(bufferdArray) for further manipulation. So how i can extract the pixel values from jpeg image format?

2012-04-04 21:57
by Bhagirath Hapse
So simple, no one answering? Try some your own effort first. Try googling ImageIO, BufferedImade.getRgb().. - B.Gen.Jack.O.Neill 2012-04-04 22:00
Working with Images tutorialJeffrey 2012-04-04 22:01
possible duplicate of How to convert Java Image into JPEG array of bytes?Simon Groenewolt 2012-04-04 22:03


1

Have a look at BufferedImage.getRGB().

Here is a stripped-down instructional example of how to pull apart an image to do a conditional check/modify on the pixels. Add error/exception handling as necessary.

public static BufferedImage exampleForSO(BufferedImage image) {
BufferedImage imageIn = image;
BufferedImage imageOut = 
new BufferedImage(imageIn.getWidth(), imageIn.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
int width = imageIn.getWidth();
int height = imageIn.getHeight();
int[] imageInPixels = imageIn.getRGB(0, 0, width, height, null, 0, width);
int[] imageOutPixels = new int[imageInPixels.length];
for (int i = 0; i < imageInPixels.length; i++) {
    int inR = (imageInPixels[i] & 0x00FF0000) >> 16;
    int inG = (imageInPixels[i] & 0x0000FF00) >> 8;
    int inB = (imageInPixels[i] & 0x000000FF) >> 0;

    if (  conditionChecker_inRinGinB  ){
        // modify
    } else {
        // don't modify
    }

}
imageOut.setRGB(0, 0, width, height, imageOutPixels, 0, width);
return imageOut;
}
2012-04-04 22:46
by Java42


0

The easiest way to get a JPEG into a java-readable object is the following:

BufferedImage image = ImageIO.read(new File("MyJPEG.jpg"));

BufferedImage provides methods for getting RGB values at exact pixel locations in the image (X-Y integer coordinates), so it'd be up to you to figure out how you want to store that in a single-dimensional array, but that's the gist of it.

2012-04-04 22:04
by CodeBlind


0

There is a way of taking a buffered image and converting it into an integer array, where each integer in the array represents the rgb value of a pixel in the image.

int[] pixels = ((DataBufferInt)image.getRaster().grtDataBuffer()).getData();

The interesting thing is, when an element in the integer array is edited, the corresponding pixel in the image is as well.

In order to find a pixel in the array from a set of x and y coordinates, you would use this method.

public void setPixel(int x, int y ,int rgb){
    pixels[y * image.getWidth() + x] = rgb;
}

Even with the multiplication and addition of coordinates, it is still faster than using the setRGB() method in the BufferedImage class.

EDIT: Also keep in mind, the image needs type needs to be that of TYPE_INT_RGB, and isn't by default. It can be converted by creating a new image of the same dimensions, and of the type of TYPE_INT_RGB. Then using the graphics object of the new image to draw the original image to the new one.

public BufferedImage toIntRGB(BufferedImage image){
    if(image.getType() == BufferedImage.TYPE_INT_RGB)
         return image;
    BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight, BufferedImage.TYPE_INT_RGB);
    newImage.getGraphics().drawImage(image, 0, 0, null);
    return newImage;
}
2016-12-06 03:17
by Java Noob
Ads