user1900588
user1900588

Reputation: 181

Java byte Image Manipulation

I need to create a simple demo for image manipulation in Java. My code is swing based. I don't have to do anything complex, just show that the image has changed in some way. I have the image read as byte[]. Is there anyway that I can manipulate this byte array without corrupting the bytes to show some very simple manipulation. I don't wish to use paint() etc. Is there anything that I can do directly to the byte[] array to show some change?

edit:
I am reading jpg image as byteArrayInputStream using apache io library. The bytes are read ok and I can confirm it by writing them back as jpeg.

Upvotes: 1

Views: 2901

Answers (1)

Alepac
Alepac

Reputation: 1831

You can try to convert your RGB image to Grayscale. If the image as 3 bytes per pixel rapresented as RedGreenBlue you can use the followinf formula: y=0.299*r+0.587*g+0.114*b.

To be clear iterate over the byte array and replace the colors. Here an example:

    byte[] newImage = new byte[rgbImage.length];

    for (int i = 0; i < rgbImage.length; i += 3) {
        newImage[i] = (byte) (rgbImage[i] * 0.299 + rgbImage[i + 1] * 0.587
                + rgbImage[i + 2] * 0.114);
        newImage[i+1] = newImage[i];
        newImage[i+2] = newImage[i];
    }

UPDATE:

Above code assumes you're using raw RGB image, if you need to process a Jpeg file you can do this:

        try {
            BufferedImage inputImage = ImageIO.read(new File("input.jpg"));

            BufferedImage outputImage = new BufferedImage(
                    inputImage.getWidth(), inputImage.getHeight(),
                    BufferedImage.TYPE_INT_RGB);
            for (int x = 0; x < inputImage.getWidth(); x++) {
                for (int y = 0; y < inputImage.getHeight(); y++) {
                    int rgb = inputImage.getRGB(x, y);
                    int blue = 0x0000ff & rgb;
                    int green = 0x0000ff & (rgb >> 8);
                    int red = 0x0000ff & (rgb >> 16);
                    int lum = (int) (red * 0.299 + green * 0.587 + blue * 0.114);
                    outputImage
                            .setRGB(x, y, lum | (lum << 8) | (lum << 16));
                }
            }
            ImageIO.write(outputImage, "jpg", new File("output.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }

Upvotes: 2

Related Questions