1

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.

user1900588
  • 181
  • 1
  • 3
  • 15
  • 1
    it seems that you are at very beginning in Java , i suggest you to refer Java Complete Reference from herehttp://www.herbschildt.com/whatsnew.htm and specifically read Image Processing Part , you will find there what you want – Mihir Apr 15 '13 at 10:51
  • What manipulations you can do on the `byte[]` array depend on what format it is being stored in. RGB bitmap? ARGB bitmap? jpg? – Patashu Apr 16 '13 at 05:56
  • @Patashu My images are downloaded from server as base64 string and then I convert them to imageBytes using the Base64.decodeBase64() function from apache.commons library. I then write them using fileoutputstream and then read again in another function using byteArrayInputStream. These are the actual bytes that need to be changed. – user1900588 Apr 16 '13 at 06:16
  • @Patashu The image is JPG. – user1900588 Apr 16 '13 at 06:40
  • for clarifying: you want to show that an image has been changed (this last acceess) or you want to show the differences between state #1 and #2 of an image (like background-color changed from green to yellow) – Sascha Apr 15 '13 at 10:54
  • I wish to change the image pixels, color, anything that can be changed in say, if I am not asking for too much, in 3 or four operation. I could write the whole java paint thing. But I just want a 2 to 3 lines solution to show any basic operation on image. – user1900588 Apr 15 '13 at 11:01
  • fastest image manipulation should be used with http://stackoverflow.com/questions/6524196/java-get-pixel-array-from-image – ddaaggeett Aug 26 '16 at 15:36

1 Answers1

2

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();
        }
Alepac
  • 1,833
  • 13
  • 24
  • Thanks. I tried the above code. There are two things though: one an arrayIndexoutofboundsexception and the other is the native jpeg format corruption. The index is probably because of the conditions i – user1900588 Apr 16 '13 at 05:52