Java Image Processing : Negative of Input Image - source code

The code below is for getting negative of an input image in JAVA:


public class TestImagesss {
    public static void main(String[] args) {
        BufferedImage org = getImage("test.jpg");
        BufferedImage negative = getNegativeImage(org);
        new ImageFrame(org, "Original");
        new ImageFrame(negative, "After Negative");
    }

Code and Outputs :
Original Image
Negative operation in Original Image
The working full source code for image negative in JAVA
package gt.imageProcessing.algorithms;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class TestImagesss {
    public static void main(String[] args) {
        BufferedImage org = getImage("test.jpg");
        BufferedImage negative = getNegativeImage(org);
        new ImageFrame(org, "Original");
        new ImageFrame(negative, "After Negative");
    }

    public static BufferedImage getImage(String imageName) {
        try {
            File input = new File(imageName);
            BufferedImage image = ImageIO.read(input);
            return image;
        } catch (IOException ie) {
            System.out.println("Error:" + ie.getMessage());
        }
        return null;
    }

    public static BufferedImage getNegativeImage(BufferedImage img) {
        int w1 = img.getWidth();
        int h1 = img.getHeight();
        // int value[][] = new int[w1][h1];
        BufferedImage gray = new BufferedImage(w1, h1, 1);
        int value, alpha, r, g, b;
        for (int i = 0; i < w1; i++) {
            for (int j = 0; j < h1; j++) {
                value = img.getRGB(i, j); // store value
                alpha = getAlpha(value);
                r = 255 - getRed(value);
                g = 255 - getGreen(value);
                b = 255 - getBlue(value);

                value = createRGB(alpha, r, g, b);
                gray.setRGB(i, j, value);
            }
        }
        return gray;
    }

    public static int createRGB(int alpha, int r, int g, int b) {
        int rgb = (alpha << 24) + (r << 16) + (g << 8) + b;
        return rgb;
    }

    public static int getAlpha(int rgb) {
        return (rgb >> 24) & 0xFF;
    }

    public static int getRed(int rgb) {
        return (rgb >> 16) & 0xFF;
    }

    public static int getGreen(int rgb) {
        return (rgb >> 8) & 0xFF;
    }

    public static int getBlue(int rgb) {
        return rgb & 0xFF;
    }
}

3 comments :

  1. what do you have coding steganografi with java?

    email to: bayuwinarta@yahoo.com

    ReplyDelete
  2. can you tell how to extract digits from image??

    ReplyDelete
  3. thank you very much for source code

    ReplyDelete

Your Comment and Question will help to make this blog better...