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

/**
 * This method cleans input image by replacing all pixels with RGB values
 * from RGBcolor input (the input color) to -1 (white) with white pixels and
 * from RGBcolor input (the input color) to -16777216 (black) with black pixels
 * @param image - input image that will be cleaned
 * @param RGBcolor - input RGB value of wanted color as reference for celaning
 * @return - cleaned input image as BufferedImage
 *//*from  w  w w  . j  av a 2s .c  o  m*/
public static BufferedImage colorCleaning(BufferedImage image, int RGBcolor) {
    for (int j = 0; j < image.getHeight(); j++) {
        for (int i = 0; i < image.getWidth(); i++) {
            if (image.getRGB(i, j) == RGBcolor) {
                image.setRGB(i, j, -16777216);
            } else {
                image.setRGB(i, j, -1);
            }
        }
    }
    return image;
}

From source file:Com.Operaciones.java

public static InputStream generateBarras(String text, int h, int w) throws Exception {

    Charset charset = Charset.forName("ISO-8859-1");
    CharsetEncoder encoder = charset.newEncoder();
    byte[] b = null;
    ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(text));
    b = bbuf.array();/*w  ww . j  ava2s  .  co  m*/
    String data = new String(b, "ISO-8859-1");
    // get a byte matrix for the data

    BitMatrix matrix = null;

    matrix = new Code128Writer().encode(data, BarcodeFormat.CODE_128, w, h, null);

    int height = matrix.getHeight();
    int width = matrix.getWidth();
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
        }
    }

    byte[] imgByte = ChartUtilities.encodeAsPNG(image);

    InputStream myInputStream = new ByteArrayInputStream(imgByte);

    return myInputStream;

}

From source file:UploadImage.java

public static BufferedImage shrink(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

public static BufferedImage getTransparentImage(BufferedImage image, Color transparent) {
    BufferedImage img = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = img.createGraphics();
    for (int x = 0; x < img.getWidth(); x++) {
        for (int y = 0; y < img.getHeight(); y++) {
            if (image.getRGB(x, y) != transparent.getRGB()) {
                img.setRGB(x, y, image.getRGB(x, y));
            }//from  w w w .  ja v  a 2  s . co m
        }
    }
    g.dispose();
    return img;
}

From source file:app.utils.ImageUtilities.java

public static BufferedImage convertImageToBufferedImage(Image image, int IMAGE_TYPE) {
    final int width = image.getWidth();
    final int height = image.getHeight();
    BufferedImage result = new BufferedImage(width, height, IMAGE_TYPE);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            int rgbValue = getRgbValue(image.getPixels()[x][y]);
            result.setRGB(x, y, rgbValue);
        }/*from   w w  w .  j a v  a  2s.  c  o m*/
    }
    return result;
}

From source file:Main.java

public static BufferedImage int2image(int[][] scene) {
    int maxValue = -1;
    int minValue = Integer.MAX_VALUE;
    for (int y = 0; y < scene.length; y++) {
        for (int x = 0; x < scene[y].length; x++) {
            maxValue = Math.max(maxValue, scene[y][x]);
            minValue = Math.min(minValue, scene[y][x]);
        }/*from  ww w .j  av a2 s  .c  o m*/
    }

    if (maxValue == minValue)
        maxValue = minValue + 1;
    final double scale = 255.0 / (maxValue - minValue);

    BufferedImage image = new BufferedImage(scene[0].length, scene.length, BufferedImage.TYPE_INT_RGB);
    for (int y = 0; y < scene.length; y++) {
        for (int x = 0; x < scene[y].length; x++) {
            final int c = (int) (scale * (scene[y][x] - minValue));
            image.setRGB(x, y, c << 16 | c << 8 | c);
        }
    }

    return image;
}

From source file:Main.java

public static BufferedImage replaceColor(BufferedImage src, int sourceRGB, int replaceRGB) {

    for (int y = 0; y < src.getHeight(); y++) {
        for (int x = 0; x < src.getWidth(); x++) {

            int rawRGB = src.getRGB(x, y);
            int rgb = rawRGB & 0xffffff;
            int alpha = rawRGB & 0xff000000;

            if (rgb == sourceRGB) {
                src.setRGB(x, y, alpha | replaceRGB);
            }/*w w w.j a  v a  2 s.com*/
        }
    }

    return src;
}

From source file:Main.java

public static BufferedImage generateOutline(BufferedImage source, Color color1, Color color2, boolean alpha) {
    int w = source.getWidth();
    int h = source.getHeight();
    BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            if (!testNeighbour(source, x, y, alpha)) {
                if (testNeighbours(source, x, y, alpha)) {
                    boolean a = ((x + y) % 10) <= 5;
                    result.setRGB(x, y, a ? color1.getRGB() : color2.getRGB());
                }/*  www . jav  a 2s  .c o m*/
            }
        }
    }
    return result;
}

From source file:ImageProc.java

public static void imwrite(File file, int[][] classificationMat, int imgDim1, int imgDim2) {
    BufferedImage image = new BufferedImage(imgDim2, imgDim1, BufferedImage.TYPE_INT_RGB);

    for (int i = 0; i < imgDim1; i++) {
        for (int j = 0; j < imgDim2; j++) {

            switch (classificationMat[i][j]) {
            case 1:
                image.setRGB(j, i, Color.BLUE.getRGB());
                break;
            case 2:
                image.setRGB(j, i, Color.CYAN.getRGB());
                break;
            case 3:
                image.setRGB(j, i, Color.GREEN.getRGB());
                break;
            case 4:
                image.setRGB(j, i, Color.ORANGE.getRGB());
                break;
            case 5:
                image.setRGB(j, i, Color.RED.getRGB());
                break;
            }/*from  w w  w .  ja  v a 2  s  .c o m*/
        }
    }
    try {
        ImageIO.write(image, "png", file);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:contactangle.ImageControl.java

public static BufferedImage copyByPixel(BufferedImage ii) {
    BufferedImage oi = new BufferedImage(ii.getWidth(), ii.getHeight(), BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < ii.getWidth(); x++) {
        for (int y = 0; y < ii.getHeight(); y++) {
            int pixelData = ii.getRGB(x, y);
            oi.setRGB(x, y, pixelData);
        }/*  ww  w  . j a  v a  2 s .c  o m*/
    }
    return oi;
}