Example usage for java.awt.image BufferedImage setRGB

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

Introduction

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

Prototype

public void setRGB(int x, int y, int rgb) 

Source Link

Document

Sets a pixel in this BufferedImage to the specified RGB value.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);

    Graphics g = img.getGraphics();
    g.setColor(Color.red);//  w  w w  . jav a  2  s. c  o m
    g.setFont(new Font("Arial", Font.BOLD, 14));
    g.drawString("Reference", 10, 80);

    int w = 100;
    int h = 100;
    int x = 1;
    int y = 1;

    int[] pixels = new int[w * h];
    PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);

    pg.grabPixels();
    BufferedImage bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

    for (int j = 0; j < h; j++) {
        for (int i = 0; i < w; i++) {
            bimg.setRGB(x + i, y + j, pixels[j * w + i]);
        }
    }

    FileOutputStream fout = new FileOutputStream("jpg.jpg");

    JPEGImageEncoder jencoder = JPEGCodec.createJPEGEncoder(fout);
    JPEGEncodeParam enParam = jencoder.getDefaultJPEGEncodeParam(bimg);

    enParam.setQuality(1.0F, true);
    jencoder.setJPEGEncodeParam(enParam);
    jencoder.encode(bimg);

    fout.close();

}

From source file:Main.java

public static void main(String[] args) throws IOException {

    int width = 100;// width of your image
    int height = 100; // height of your image

    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < width; ++x) {
        for (int y = 0; y < height; ++y) {
            int grayscale = 123;
            int colorValue = grayscale | grayscale << 8 | grayscale << 16;
            img.setRGB(x, y, colorValue);
        }/*from w w  w  .  j  a v  a2  s  .c  o m*/
    }
    ImageIO.write(img, "png", new File("c:/Java_Dev/output.png"));
}

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);
}

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());
        }/*  w w w  .jav  a 2  s . c  o m*/
    }

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

}

From source file:projects.nemetode.StackVideos.java

/**
 * Main application entry point.//from   ww  w. jav  a  2  s.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

private static BufferedImage map(int sizeX, int sizeY) {
    final BufferedImage res = new BufferedImage(sizeX, sizeY, BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < sizeX; x++) {
        for (int y = 0; y < sizeY; y++) {
            res.setRGB(x, y, Color.WHITE.getRGB());
        }/*from   www .j a v  a 2  s .  c  o m*/
    }
    return res;
}

From source file:Main.java

static BufferedImage enlarge(BufferedImage image, int n) {
    int w = image.getWidth() / n;
    int h = image.getHeight() / n;

    BufferedImage shrunkImage = new BufferedImage(w, h, image.getType());

    for (int y = 0; y < h; ++y)
        for (int x = 0; x < w; ++x)
            shrunkImage.setRGB(x, y, image.getRGB(x * n, y * n));

    return shrunkImage;
}

From source file:Main.java

/**
 * Creates a static rectangular image with given color, width and height.
 *//* ww  w  .jav a  2 s. c o  m*/
public static Icon buildStaticImage(Color color, int width, int height) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            image.setRGB(x, y, color.getRGB());
        }
    }
    return new ImageIcon(image);
}

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
 *///from w w w.jav a 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;
}

From source file:Main.java

/**
 * This method cleans input image by replacing all pixels with RGB values
 * from -3092272 (light gray) to -1 (white) with white pixels and
 * from -3092272 (light gray) to -16777216 (black) with black pixels
 * @param image - input image that will be cleaned
 * @return - cleaned input image as BufferedImage
 *///from ww  w.j  a  v a  2 s.  c om
public static BufferedImage blackAndLightGrayCleaning(BufferedImage image) {
    for (int j = 0; j < image.getHeight(); j++) {
        for (int i = 0; i < image.getWidth(); i++) {
            if (image.getRGB(i, j) > -3092272) {
                image.setRGB(i, j, -1);
            } else {
                image.setRGB(i, j, -16777216);
            }
        }
    }
    return image;
}