Example usage for java.awt.image BufferedImage getRGB

List of usage examples for java.awt.image BufferedImage getRGB

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getRGB.

Prototype

public int getRGB(int x, int y) 

Source Link

Document

Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);

    int rgb = bufferedImage.getRGB(1, 1);

    int w = bufferedImage.getWidth(null);
    int h = bufferedImage.getHeight(null);
    int[] rgbs = new int[w * h];
    bufferedImage.getRGB(0, 0, w, h, rgbs, 0, w);

    rgb = 0xFF00FF00; // green
    bufferedImage.setRGB(1, 1, rgb);//w w w .  j ava 2s. co  m
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    File file = new File("your_file.jpg");
    BufferedImage image = ImageIO.read(file);

    int x = 10;/*from   w w w.ja  v  a  2 s  . co m*/
    int y = 10;

    int clr = image.getRGB(x, y);
    int red = (clr & 0x00ff0000) >> 16;
    int green = (clr & 0x0000ff00) >> 8;
    int blue = clr & 0x000000ff;
    System.out.println("Red Color value = " + red);
    System.out.println("Green Color value = " + green);
    System.out.println("Blue Color value = " + blue);
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    File file = new File("your file path.jpg");
    BufferedImage image = ImageIO.read(file);

    for (int i = 0; i < image.getHeight(); i++) {
        for (int j = 0; j < image.getWidth(); j++) {
            int color = image.getRGB(j, i);
            // convert the color to a readable format
            String clr = Integer.toHexString(color).substring(2);
            System.out.println(clr);
        }/*w  w  w .j  av  a2  s . com*/
    }
}

From source file:Main.java

public static void main(final String args[]) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");
    BufferedImage image = ImageIO.read(url);

    for (int x = 0; x < image.getWidth(); x++) {
        for (int y = 0; y < image.getHeight(); y++) {
            int clr = image.getRGB(x, y);
            int red = (clr & 0x00ff0000) >> 16;
            int green = (clr & 0x0000ff00) >> 8;
            int blue = clr & 0x000000ff;

            System.out.println("Red Color value = " + red);
            System.out.println("Green Color value = " + green);
            System.out.println("Blue Color value = " + blue);
        }/*from w w w  .j a  va 2  s.c o m*/
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage img = ImageIO.read(new File("c:/Java_Dev/a.jpg"));
    int height = img.getHeight();
    int width = img.getWidth();

    System.out.println(height + "  " + width + " " + img.getRGB(30, 30));

    for (int h = 1; h < height; h++) {
        for (int w = 1; w < width; w++) {
            int rgb = img.getRGB(w, h);
            int red = (rgb >> 16) & 0x000000FF;
            int green = (rgb >> 8) & 0x000000FF;
            int blue = (rgb) & 0x000000FF;

            if (red == 0 && green == 0 && blue == 0) {
                System.out.println("Black");
            }//from  ww  w .ja v a 2 s . co  m
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage inputFile = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));

    for (int x = 0; x < inputFile.getWidth(); x++) {
        for (int y = 0; y < inputFile.getHeight(); y++) {
            int rgba = inputFile.getRGB(x, y);
            Color col = new Color(rgba, true);
            col = new Color(255 - col.getRed(), 255 - col.getGreen(), 255 - col.getBlue());
            inputFile.setRGB(x, y, col.getRGB());
        }/*from  w w w .  j  a  v  a  2 s  .c o m*/
    }

    File outputFile = new File("invert.png");
    ImageIO.write(inputFile, "png", outputFile);

}

From source file:Main.java

public static void main(String[] args) throws IOException {
    InputStream stream = Main.class.getResourceAsStream("test.png");
    BufferedImage image = ImageIO.read(stream);
    int iw = image.getWidth();
    int ih = image.getHeight();
    stream.close();//from  w  ww  .jav a2  s .c  o  m

    for (int y = 0; y < ih; y++) {
        for (int x = 0; x < iw; x++) {
            int pixel = image.getRGB(x, y);
            int alpha = (pixel >> 24) & 0xFF;
            int red = (pixel >> 16) & 0xFF;
            int green = (pixel >> 8) & 0xFF;
            int blue = pixel & 0xFF;
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage image = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));
    System.out.println(image == null);
    int width = image.getWidth();
    int height = image.getHeight();
    System.out.println(width + "x" + height);
    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            System.out.printf("%04X ", image.getRGB(col, row));
        }/*  w w  w .  j  a  v  a  2  s  .c  o  m*/
        System.out.println();
    }
}

From source file:projects.nemetode.StackVideos.java

/**
 * Main application entry point./*from   ww w  . j  a v  a  2s  .  c  o  m*/
 * @param args
 * @throws IOException 
 * @throws InterruptedException 
 */
public static void main(String[] args) throws IOException, InterruptedException {

    // Directory containing the video folders
    File videoDir = new File("/home/nrowell/Temp/videos");

    // Get all subdirectories (M20160812_225434_Gargunnock etc)
    String[] dirs = videoDir.list(new FilenameFilter() {
        @Override
        public boolean accept(File current, String name) {
            return new File(current, name).isDirectory();
        }
    });

    // Process each video: break it into single frames using avconv, then map the individual frames by frame number
    Map<Integer, List<BufferedImage>> framesMap = new HashMap<>();

    for (String dir : dirs) {

        File videoSubDir = new File(videoDir, dir);

        logger.info("Processing " + videoSubDir.getName());

        File avi = new File(videoSubDir, videoSubDir.getName() + "_NR.avi");

        String[] command = new String[] { "avconv", "-i", avi.getAbsolutePath(), "-f", "image2",
                videoSubDir.getAbsolutePath() + "/frame%03d.png" };

        logger.info(String.format("Command: %s %s %s %s %s %s ", command));

        final Process proc = Runtime.getRuntime().exec(command);
        StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "AVCONV");
        errorGobbler.start();
        int exitCode = proc.waitFor();
        errorGobbler.join();

        // Now load all the images to the map
        for (File imFile : (Collection<File>) FileUtils.listFiles(videoSubDir, new String[] { "png" }, false)) {
            // Read the frame number
            String filename = imFile.getName(); // frame_XXX.png
            int fNum = Integer.parseInt(filename.substring(5, 8));
            BufferedImage frame = ImageIO.read(imFile);
            if (!framesMap.containsKey(fNum)) {
                framesMap.put(fNum, new LinkedList<BufferedImage>());
            }
            framesMap.get(fNum).add(frame);
            imFile.delete();
        }

    }

    // Loop over the frame number
    for (Entry<Integer, List<BufferedImage>> entry : framesMap.entrySet()) {

        int fNum = entry.getKey();
        List<BufferedImage> frames = entry.getValue();

        // Generate single combined output image from all clips
        FloatList[] pixels = new FloatList[640 * 480];
        for (int p = 0; p < pixels.length; p++) {
            pixels[p] = new FloatList();
        }

        // Loop over all the clips and produce a merged image of all of the individual frames
        for (BufferedImage frame : frames) {

            // Read all the pixels into the list
            for (int i = 0; i < 640; i++) {
                for (int j = 0; j < 480; j++) {

                    // Index into flattened pixels array
                    int idx = i * 480 + j;

                    // Extract the pixel value from the frame
                    int pixel = frame.getRGB(i, j);
                    //                  int a = (pixel >> 24) & 0xFF;
                    // The RGB all contain the same value
                    int r = (pixel >> 16) & 0xFF;
                    //                  int g = (pixel >> 8) & 0xFF;
                    //                  int b = pixel & 0xFF;

                    pixels[idx].add((float) r);
                }
            }
        }

        BufferedImage composite = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);

        // Draw integer array of pixel values into image as graylevels
        for (int x = 0; x < 640; x++) {
            for (int y = 0; y < 480; y++) {

                // Index into flattened pixels array
                int idx = x * 480 + y;

                int gray = (int) pixels[idx].getMinMax()[1];
                int pixel = 0xFF000000 + (gray << 16) + (gray << 8) + gray;

                composite.setRGB(x, y, pixel);
            }
        }

        ImageIO.write(composite, "png", new File(videoDir, String.format("median_%03d.png", fNum)));
    }

    // AVCONV command to encode video:
    //  avconv -r 30 -i median_%03d.png -vcodec libx264 -crf 20 -vf transpose=1 movie.mp4

}

From source file:Main.java

/**
 * This method cleans input image by replacing
 * all non black pixels with white pixels
 * @param image - input image that will be cleaned
 * @return - cleaned input image as BufferedImage
 *///ww  w  . j  a va 2 s .c o m
public static BufferedImage blackAndWhiteCleaning(BufferedImage image) {
    for (int j = 0; j < image.getHeight(); j++) {
        for (int i = 0; i < image.getWidth(); i++) {
            if (image.getRGB(i, j) != -16777216) {
                image.setRGB(i, j, -1);
            }
        }
    }
    return image;
}