java.awt.image.BufferedImage.getRGB()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(850)

本文整理了Java中java.awt.image.BufferedImage.getRGB()方法的一些代码示例,展示了BufferedImage.getRGB()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BufferedImage.getRGB()方法的具体详情如下:
包路径:java.awt.image.BufferedImage
类名称:BufferedImage
方法名:getRGB

BufferedImage.getRGB介绍

[英]Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace. Color conversion takes place if this default model does not match the image ColorModel. There are only 8-bits of precision for each color component in the returned data when using this method.
[中]返回默认RGB颜色模型(TYPE_INT_ARGB)和默认sRGB颜色空间中的整数像素。如果此默认模型与图像ColorModel不匹配,则会发生颜色转换。使用此方法时,返回数据中每个颜色分量的精度只有8位。

代码示例

代码示例来源:origin: MovingBlocks/Terasology

public void storeGreyscaleMapIntoAlpha(BufferedImage imageWithoutAlpha, BufferedImage greyscaleImage) {
  int width = imageWithoutAlpha.getWidth();
  int height = imageWithoutAlpha.getHeight();
  int[] imagePixels = imageWithoutAlpha.getRGB(0, 0, width, height, null, 0, width);
  int[] maskPixels = greyscaleImage.getRGB(0, 0, width, height, null, 0, width);
  for (int i = 0; i < imagePixels.length; i++) {
    int color = imagePixels[i] & 0x00ffffff; // Mask preexisting alpha
    int alpha = maskPixels[i] << 24; // Shift blue to alpha
    imagePixels[i] = color | alpha;
  }
  imageWithoutAlpha.setRGB(0, 0, width, height, imagePixels, 0, width);
}

代码示例来源:origin: jphp-group/jphp

@Signature
public Color getPixel(int x, int y) {
  return new Color(image.getRGB(x, y));
}

代码示例来源:origin: redwarp/9-Patch-Resizer

private void verifyBorderImage(BufferedImage border)
   throws Wrong9PatchException {
  int[] rgb = border.getRGB(0, 0, border.getWidth(), border.getHeight(),
               null, 0, border.getWidth());
  for (int i = 0; i < rgb.length; i++) {
   if ((0xff000000 & rgb[i]) != 0) {
    if (rgb[i] != 0xff000000 && rgb[i] != 0xffff0000) {
     throw new Wrong9PatchException();
    }
   }
  }
 }
}

代码示例来源:origin: vert-x3/vertx-examples

int height = term.height();
Image temp = capture.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage scaled = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = scaled.createGraphics();
g2d.drawImage(temp, 0, 0, null);
 sb.append("\033[").append(y + 1).append(";1H");
 for (int x = 0; x < width; x++) {
  Color pixel = new Color(scaled.getRGB(x, y));
  int r = pixel.getRed();
  int g = pixel.getGreen();

代码示例来源:origin: kevin-wayne/algs4

/**
 * Creates a new grayscale picture that is a deep copy of the argument picture.
 *
 * @param  picture the picture to copy
 * @throws IllegalArgumentException if {@code picture} is {@code null}
 */
public GrayscalePicture(GrayscalePicture picture) {
  if (picture == null) throw new IllegalArgumentException("constructor argument is null");
  width  = picture.width();
  height = picture.height();
  image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  filename = picture.filename;
  isOriginUpperLeft = picture.isOriginUpperLeft;
  for (int col = 0; col < width(); col++)
    for (int row = 0; row < height(); row++)
      image.setRGB(col, row, picture.image.getRGB(col, row));
}

代码示例来源:origin: stackoverflow.com

public void applyGrayscaleMaskToAlpha(BufferedImage image, BufferedImage mask)
{
  int width = image.getWidth();
  int height = image.getHeight();

  int[] imagePixels = image.getRGB(0, 0, width, height, null, 0, width);
  int[] maskPixels = mask.getRGB(0, 0, width, height, null, 0, width);

  for (int i = 0; i < imagePixels.length; i++)
  {
    int color = imagePixels[i] & 0x00ffffff; // Mask preexisting alpha
    int alpha = maskPixels[i] << 24; // Shift blue to alpha
    imagePixels[i] = color | alpha;
  }

  image.setRGB(0, 0, width, height, imagePixels, 0, width);
}

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

/**
 * This method returns the height represented by the specified pixel in the
 * given texture. The given texture should be a height-map.
 * 
 * @param image
 *            the height-map texture
 * @param x
 *            pixel's X coordinate
 * @param y
 *            pixel's Y coordinate
 * @return height represented by the given texture in the specified location
 */
private static int getHeight(BufferedImage image, int x, int y) {
  if (x < 0) {
    x = 0;
  } else if (x >= image.getWidth()) {
    x = image.getWidth() - 1;
  }
  if (y < 0) {
    y = 0;
  } else if (y >= image.getHeight()) {
    y = image.getHeight() - 1;
  }
  return image.getRGB(x, y) & 0xff;
}

代码示例来源:origin: MovingBlocks/Terasology

@Override
  public Vector4f calcColor(Biome biome) {
    float humidity = biome.getHumidity();
    float temperature = biome.getTemperature();
    float prod = temperature * humidity;
    int rgbValue = colorLut.getRGB((int) ((1.0 - temperature) * 255.0), (int) ((1.0 - prod) * 255.0));
    Color c = new Color(rgbValue);
    return new Vector4f(c.getRed() / 255f, c.getGreen() / 255f, c.getBlue() / 255f, 1.0f);
  }
},

代码示例来源:origin: h2oai/h2o-3

BufferedImage scaledImg = new BufferedImage(w, h, img.getType());
Graphics2D g2d = scaledImg.createGraphics();
g2d.drawImage(img, 0, 0, w, h, null);
for (int i = 0; i < h; i++) {
 for (int j = 0; j < w; j++) {
  Color mycolor = new Color(scaledImg.getRGB(j, i));
  int red = mycolor.getRed();
  int green = mycolor.getGreen();

代码示例来源:origin: kevin-wayne/algs4

/**
 * Creates a new picture that is a deep copy of the argument picture.
 *
 * @param  picture the picture to copy
 * @throws IllegalArgumentException if {@code picture} is {@code null}
 */
public Picture(Picture picture) {
  if (picture == null) throw new IllegalArgumentException("constructor argument is null");
  width  = picture.width();
  height = picture.height();
  image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  filename = picture.filename;
  isOriginUpperLeft = picture.isOriginUpperLeft;
  for (int col = 0; col < width(); col++)
    for (int row = 0; row < height(); row++)
      image.setRGB(col, row, picture.image.getRGB(col, row));
}

代码示例来源:origin: libgdx/libgdx

public BufferedImage processImage (BufferedImage image, int maxIterations) {
  int width = image.getWidth();
  int height = image.getHeight();
  BufferedImage processedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  int[] rgb = image.getRGB(0, 0, width, height, null, 0, width);
  Mask mask = new Mask(rgb);
  int iterations = 0;
  int lastPending = -1;
  while (mask.pendingSize > 0 && mask.pendingSize != lastPending && iterations < maxIterations) {
    lastPending = mask.pendingSize;
    executeIteration(rgb, mask, width, height);
    iterations++;
  }
  processedImage.setRGB(0, 0, width, height, rgb, 0, width);
  return processedImage;
}

代码示例来源:origin: galenframework/galen

private static ByteBuffer readRgbModelFrom(BufferedImage image) {
  int w = image.getWidth();
  int h = image.getHeight();
  int[] pixels = new int[w * h];
  image.getRGB(0, 0, w, h, pixels, 0, w);
  // use direct byte buffer to resolve heap errors here
  ByteBuffer rgbBytes = ByteBuffer.allocateDirect(w * h * BLOCK_SIZE);
  for (int r = 0; r < h; r++) {
    for (int c = 0; c < w; c++) {
      int index = r * w + c;
      int indexRgb = r * w * BLOCK_SIZE + c * BLOCK_SIZE;
      rgbBytes.put(indexRgb, (byte) ((pixels[index] >> 16) & 0xff));
      rgbBytes.put(indexRgb + 1, (byte) ((pixels[index] >> 8) & 0xff));
      rgbBytes.put(indexRgb + 2, (byte) (pixels[index] & 0xff));
      rgbBytes.put(indexRgb + 3, (byte) ((pixels[index] >> 24) & 0xff));
    }
  }
  return rgbBytes;
}

代码示例来源:origin: MovingBlocks/Terasology

@Override
  public Vector4f calcColor(Biome biome) {
    float humidity = biome.getHumidity();
    float temperature = biome.getTemperature();
    float prod = humidity * temperature;
    int rgbValue = foliageLut.getRGB((int) ((1.0 - temperature) * 255.0), (int) ((1.0 - prod) * 255.0));
    Color c = new Color(rgbValue);
    return new Vector4f(c.getRed() / 255f, c.getGreen() / 255f, c.getBlue() / 255f, 1.0f);
  }
};

代码示例来源:origin: loklak/loklak_server

final int height2 = ruy - loy + 1;
boolean border = false;
final BufferedImage image2 = new BufferedImage(width2, height2, BufferedImage.TYPE_INT_RGB);
        rgb = this.image.getRGB(i - 1, j);
        border = (rgb == bgcolor);
        rgbR += rgb >> 16 & 0xff;
        rgb = this.image.getRGB(i, j - 1);
        border = border || (rgb == bgcolor);
        rgbR += rgb >> 16 & 0xff;
        rgb = this.image.getRGB(i + 1, j);
        border = border || (rgb == bgcolor);
        rgbR += rgb >> 16 & 0xff;
        rgb = this.image.getRGB(i, j + 1);
        border = border || (rgb == bgcolor);
        rgbR += rgb >> 16 & 0xff;
    rgb = this.image.getRGB(i, j);
    image2.setRGB(i-lox, j-loy, rgb);

代码示例来源:origin: libgdx/libgdx

public BufferedImage processImage (BufferedImage image, int maxIterations) {
  int width = image.getWidth();
  int height = image.getHeight();
  BufferedImage processedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  int[] rgb = image.getRGB(0, 0, width, height, null, 0, width);
  Mask mask = new Mask(rgb);
  int iterations = 0;
  int lastPending = -1;
  while (mask.pendingSize > 0 && mask.pendingSize != lastPending && iterations < maxIterations) {
    lastPending = mask.pendingSize;
    executeIteration(rgb, mask, width, height);
    iterations++;
  }
  processedImage.setRGB(0, 0, width, height, rgb, 0, width);
  return processedImage;
}

代码示例来源:origin: plantuml/plantuml

public void drawImage(BufferedImage image, double x, double y) {
  final int width = image.getWidth();
  final int height = image.getHeight();
  append("gsave", true);
  append(format(x) + " " + format(y) + " translate", true);
  append(format(width) + " " + format(height) + " scale", true);
  append("" + width + " " + height + " 8 [" + width + " 0 0 -" + height + " 0 " + height + "]", true);
  // append("" + width + " " + height + " 8 [0 0 0 0 0 0]");
  append("{<", true);
  final StringBuilder sb = new StringBuilder();
  for (int j = height - 1; j >= 0; j--) {
    for (int i = 0; i < width; i++) {
      final String hexString = getRgb(image.getRGB(i, j));
      assert hexString.length() == 6;
      sb.append(hexString);
    }
  }
  append(sb.toString(), true);
  // append(">} image");
  append(">} false 3 colorimage", true);
  ensureVisible(x + width, y + height);
  append("grestore", true);
}

代码示例来源:origin: runelite/runelite

private Color colorAt(int x, int y)
  {
    x = Ints.constrainToRange(x, 0, size - 1);
    y = Ints.constrainToRange(y, 0, size - 1);
    return new Color(image.getRGB(x, y));
  }
}

代码示例来源:origin: plantuml/plantuml

private static BufferedImage addTransparent(BufferedImage ico) {
  if (ico == null) {
    return null;
  }
  final BufferedImage transparentIcon = new BufferedImage(ico.getWidth(), ico.getHeight(),
      BufferedImage.TYPE_INT_ARGB_PRE);
  for (int i = 0; i < ico.getWidth(); i++) {
    for (int j = 0; j < ico.getHeight(); j++) {
      final int col = ico.getRGB(i, j);
      if (col != ico.getRGB(0, 0)) {
        transparentIcon.setRGB(i, j, col);
      }
    }
  }
  return transparentIcon;
}

代码示例来源:origin: pentaho/pentaho-kettle

/**
  * Converts BufferedImage to SWT/Image with alpha channel.
  */
 protected Image swing2swt( Device device, BufferedImage img ) {
  PaletteData palette = new PaletteData( 0xFF0000, 0xFF00, 0xFF );
  ImageData data = new ImageData( img.getWidth(), img.getHeight(), 32, palette );
  for ( int y = 0; y < data.height; y++ ) {
   for ( int x = 0; x < data.width; x++ ) {
    int rgba = img.getRGB( x, y );
    int rgb = palette.getPixel( new RGB( ( rgba >> 16 ) & 0xFF, ( rgba >> 8 ) & 0xFF, rgba & 0xFF ) );
    int a = ( rgba >> 24 ) & 0xFF;
    data.setPixel( x, y, rgb );
    data.setAlpha( x, y, a );
   }
  }
  return new Image( device, data );
 }
}

代码示例来源:origin: kevin-wayne/algs4

/**
 * Returns the grayscale value of pixel ({@code col}, {@code row}) as a {@link java.awt.Color}.
 *
 * @param col the column index
 * @param row the row index
 * @return the grayscale value of pixel ({@code col}, {@code row})
 * @throws IllegalArgumentException unless both {@code 0 <= col < width} and {@code 0 <= row < height}
 */
public Color get(int col, int row) {
  validateColumnIndex(col);
  validateRowIndex(row);
  Color color = new Color(image.getRGB(col, row));
  return toGray(color);
}

相关文章