java.awt.image.BufferedImage类的使用及代码示例

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

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

BufferedImage介绍

[英]The BufferedImage subclass describes an java.awt.Image with an accessible buffer of image data. All BufferedImage objects have an upper left corner coordinate of (0, 0).
[中]BufferedImage子类描述java。awt。具有可访问的图像数据缓冲区的图像。所有BufferedImage对象的左上角坐标为(0,0)。

代码示例

代码示例来源:origin: nutzam/nutz

/**
 * 在一个RGB画布上重新绘制Image,解决CMYK图像偏色的问题
 */
public static BufferedImage redraw(BufferedImage img, Color bg) {
  BufferedImage rgbImage = new BufferedImage(img.getWidth(),
                        img.getHeight(),
                        BufferedImage.TYPE_3BYTE_BGR);
  Graphics2D g2d = rgbImage.createGraphics();
  g2d.drawImage(img, 0, 0, bg, null);
  g2d.dispose();
  return rgbImage;
}

代码示例来源:origin: linlinjava/litemall

private void drawTextInImgCenter(BufferedImage baseImage, String textToWrite, int y) {
  Graphics2D g2D = (Graphics2D) baseImage.getGraphics();
  g2D.setColor(new Color(167, 136, 69));
  String fontName = "Microsoft YaHei";
  Font f = new Font(fontName, Font.PLAIN, 28);
  g2D.setFont(f);
  g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  // 计算文字长度,计算居中的x点坐标
  FontMetrics fm = g2D.getFontMetrics(f);
  int textWidth = fm.stringWidth(textToWrite);
  int widthX = (baseImage.getWidth() - textWidth) / 2;
  // 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
  g2D.drawString(textToWrite, widthX, y);
  // 释放对象
  g2D.dispose();
}

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

public static void main(String... args) throws Exception {

  BufferedImage image = ImageIO.read(
     new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));

  int w = image.getWidth();
  int h = image.getHeight();

  int[] dataBuffInt = image.getRGB(0, 0, w, h, null, 0, w); 

  Color c = new Color(dataBuffInt[100]);

  System.out.println(c.getRed());   // = (dataBuffInt[100] >> 16) & 0xFF
  System.out.println(c.getGreen()); // = (dataBuffInt[100] >> 8)  & 0xFF
  System.out.println(c.getBlue());  // = (dataBuffInt[100] >> 0)  & 0xFF
  System.out.println(c.getAlpha()); // = (dataBuffInt[100] >> 24) & 0xFF
}

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

@Override
  public int compare (BufferedImage o1, BufferedImage o2) {
    return o2.getWidth() * o2.getHeight() - o1.getWidth() * o1.getHeight();
  }
});

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

static private void plot (BufferedImage dst, int x, int y, int argb) {
  if (0 <= x && x < dst.getWidth() && 0 <= y && y < dst.getHeight()) dst.setRGB(x, y, argb);
}

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

public Rect (BufferedImage source, int left, int top, int newWidth, int newHeight, boolean isPatch) {
  image = new BufferedImage(source.getColorModel(),
    source.getRaster().createWritableChild(left, top, newWidth, newHeight, 0, 0, null),
    source.getColorModel().isAlphaPremultiplied(), null);
  offsetX = left;
  offsetY = top;
  regionWidth = newWidth;
  regionHeight = newHeight;
  originalWidth = source.getWidth();
  originalHeight = source.getHeight();
  width = newWidth;
  height = newHeight;
  this.isPatch = isPatch;
}

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

Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(new Color(0xFFAA00));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(basicStroke);
g2d.drawRoundRect(3, 3, getWidth()-4, getHeight()-4, archH, archH);
setLayout(new GridBagLayout()); 
Graphics2D g2d = (Graphics2D)g.create(); // cloning to work, it is safer aproach
Rectangle2D txRect = new Rectangle2D.Double(0, 0, textureImg.getWidth(), textureImg.getHeight());
TexturePaint txPaint = new TexturePaint(textureImg, txRect);
g2d.setPaint(txPaint);
Graphics2D g2d = (Graphics2D) g.create();
if(gradientImage==null || gradientImage.getHeight() != getHeight())
BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
  Graphics2D g2d = (Graphics2D) image.getGraphics();
  g2d.setPaint(lgrPaint);
MainContainer.textureImg = MainContainer.textureImg.getSubimage(0, 0, 200, 200);
    JFrame frame = new JFrame("Demo: LogIn Dialogue");

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

+ "The effect we want is a multi-line label.";
BufferedImage image = new BufferedImage(
  400,
  300,
  BufferedImage.TYPE_INT_RGB);
Graphics2D imageGraphics = image.createGraphics();
GradientPaint gp = new GradientPaint(
  20f,
BufferedImage bi = new BufferedImage(
  d.width,
  d.height,
  BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.setColor(new Color(255, 255, 255, 128));
g.fillRoundRect(
  0,
  0,
  bi.getWidth(f),
  bi.getHeight(f),
  15,
Graphics g2 = image.getGraphics();
JLabel imageLabel = new JLabel(ii);
f.setVisible(true);

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

image = ImageIO.read(new URL(
      "http://sstatic.net/stackoverflow/img/logo.png"));
} catch (IOException e) {
return new Dimension(image.getWidth(), image.getHeight());
int w = old.getWidth();
int h = old.getHeight();
BufferedImage img = new BufferedImage(
    w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(old, 0, 0, null);
g2d.setPaint(Color.red);
g2d.setFont(new Font("Serif", Font.BOLD, 20));
String s = "Hello, world!";
FontMetrics fm = g2d.getFontMetrics();
int x = img.getWidth() - fm.stringWidth(s) - 5;
int y = fm.getHeight();
g2d.drawString(s, x, y);
g2d.dispose();
return img;
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new TextOverlay());
f.pack();
f.setVisible(true);

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

BufferedImage canvas = new BufferedImage(width, height, getBufferedImageType(settings.format));
Graphics2D g = (Graphics2D)canvas.getGraphics();
if (!settings.silent) System.out.println("Writing " + canvas.getWidth() + "x" + canvas.getHeight() + ": " + outputFile);
  Rect rect = page.outputRects.get(r);
  BufferedImage image = rect.getImage(imageProcessor);
  int iw = image.getWidth();
  int ih = image.getHeight();
  int rectX = page.x + rect.x, rectY = page.y + page.height - rect.y - (rect.height - settings.paddingY);
  if (settings.duplicatePadding) {
          plot(canvas, rectX - j, rectY + iw - 1 + i, image.getRGB(0, 0));
          plot(canvas, rectX + ih - 1 + j, rectY + iw - 1 + i, image.getRGB(0, ih - 1));
          plot(canvas, rectX - j, rectY - i, image.getRGB(iw - 1, 0));
          plot(canvas, rectX + ih - 1 + j, rectY - i, image.getRGB(iw - 1, ih - 1));
          plot(canvas, rectX - i, rectY + iw - 1 - j, image.getRGB(j, 0));
          plot(canvas, rectX + ih - 1 + i, rectY + iw - 1 - j, image.getRGB(j, ih - 1));
          plot(canvas, rectX + j, rectY - i, image.getRGB(iw - 1, j));
          plot(canvas, rectX + j, rectY + iw - 1 + i, image.getRGB(0, j));
          plot(canvas, rectX - i, rectY - j, image.getRGB(0, 0));
          plot(canvas, rectX - i, rectY + ih - 1 + j, image.getRGB(0, ih - 1));
          plot(canvas, rectX + iw - 1 + i, rectY - j, image.getRGB(iw - 1, 0));
          plot(canvas, rectX + iw - 1 + i, rectY + ih - 1 + j, image.getRGB(iw - 1, ih - 1));
  && !(settings.outputFormat.equalsIgnoreCase("jpg") || settings.outputFormat.equalsIgnoreCase("jpeg"))) {

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

BufferedImage originalImage = ImageIO.read(url);
final BufferedImage textImage = new BufferedImage(
  originalImage.getWidth(),
  originalImage.getHeight(),
  BufferedImage.TYPE_INT_ARGB);
Graphics2D g = textImage.createGraphics();
FontRenderContext frc = g.getFontRenderContext();
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 250);
GlyphVector gv = font.createGlyphVector(frc, "Cat");
int yOff = 80+(int)-box.getY();
Shape shape = gv.getOutline(xOff,yOff);
g.setClip(shape);
g.drawImage(originalImage,0,0,null);
g.setClip(null);
g.setStroke(new BasicStroke(2f));
g.setColor(Color.BLACK);
g.setRenderingHint(
  RenderingHints.KEY_ANTIALIASING,
  RenderingHints.VALUE_ANTIALIAS_ON);
ImageIO.write(textImage,"png",file);
Desktop.getDesktop().open(file);

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

private void init() {
  if (frame != null) frame.setVisible(false);
  frame = new JFrame();
  offscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  onscreenImage  = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  offscreen = offscreenImage.createGraphics();
  onscreen  = onscreenImage.createGraphics();
  setXscale();
  setYscale();
  offscreen.setColor(DEFAULT_CLEAR_COLOR);
  offscreen.fillRect(0, 0, width, height);
  setPenColor();
  setPenRadius();
                       RenderingHints.VALUE_ANTIALIAS_ON);
  hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  offscreen.addRenderingHints(hints);
  draw.addMouseMotionListener(this);
  frame.setContentPane(draw);
  frame.addKeyListener(this);    // JLabel cannot get keyboard focus
  frame.setResizable(false);

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

BufferedImage img = new BufferedImage(PREF_W, PREF_H,
   BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
   RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(BASIC_STROKE);
g2.setColor(Color.blue);
int circleCount = 10;
for (int i = 0; i < circleCount ; i++) {
  int w = PREF_W - 2 * x;
  int h = w;
  g2.drawOval(x, y, w, h);
g2.dispose();
return img;
BufferedImage img = new BufferedImage(PREF_W, PREF_H,
   BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
   RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(BASIC_STROKE);

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

JFrame frame = new JFrame();
    frame.setLayout(new GridLayout(N, N, N, N));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    for (int i = 0; i < N * N; i++) {
      frame.add(new RotatePanel());
    frame.pack();
    frame.setVisible(true);
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
g2d.rotate(theta);
g2d.translate(-image.getWidth(this) / 2, -image.getHeight(this) / 2);
g2d.drawImage(image, 0, 0, null);
BufferedImage bi = new BufferedImage(
  size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.setRenderingHint(
  RenderingHints.KEY_ANTIALIASING,
  RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.getHSBColor(r.nextFloat(), 1, 1));
g2d.setStroke(new BasicStroke(size / 8));
g2d.drawLine(0, size / 2, size, size / 2);
g2d.drawLine(size / 2, 0, size / 2, size);
g2d.dispose();
return bi;

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

Component component) {
BufferedImage image = new BufferedImage(
 component.getWidth(),
 component.getHeight(),
component.paint( image.getGraphics() ); // alternately use .printAll(..)
return image;
Runnable r = new Runnable() {
 public void run() {
  final JFrame f = new JFrame("Test Screenshot");
       new ImageIcon(
        img.getScaledInstance(
         img.getWidth(null)/2,
         img.getHeight(null)/2,
         Image.SCALE_SMOOTH )
        )));
     try {
      ImageIO.write(
       img,
       "png",
  JMenuBar mb = new JMenuBar();
  mb.add(menu);
  f.setJMenuBar(mb);

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

if (scale <= 0) throw new IllegalArgumentException("scale cannot be <= 0: " + scale);
int width = image.getWidth(), height = image.getHeight();
if (image.getType() != BufferedImage.TYPE_4BYTE_ABGR) {
  BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
  newImage.getGraphics().drawImage(image, 0, 0, null);
  image = newImage;
  BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
  newImage.getGraphics().drawImage(image, 0, 0, width, height, 1, 1, width + 1, height + 1, null);
  image = newImage;
  width = Math.max(1, Math.round(width * scale));
  height = Math.max(1, Math.round(height * scale));
  BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
  if (scale < 1) {
    newImage.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING), 0, 0, null);
  } else {
    Graphics2D g = (Graphics2D)newImage.getGraphics();
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, resampling.value);
    g.drawImage(image, 0, 0, width, height, null);

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

this.add(new JPanel() {
  @Override
  protected void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, null);
img = new BufferedImage( 450, 350, IMAGE_TYPE );   // here you should create a compatible BufferedImage
this.setSize(img.getWidth(), img.getHeight());
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
tiles[0] = createOneTile( new Color( 255, 255, 255 ) );
tiles[1] = createOneTile( new Color( 255,   0, 255 ) );
tiles[2] = createOneTile( new Color(   0,   0, 255 ) );
tiles[3] = createOneTile( new Color(   0, 255, 255 ) );  
  for (int j = 0; j < map.length; j++) {
    final BufferedImage tile = tiles[map[j][i]];
    for (int x = 0; x < tile.getWidth(); x++) {
      for (int y = 0; y < tile.getHeight(); y++) {
        img.setRGB( x + i * 50, y + j * 50, tile.getRGB(x,y) );
this.setVisible( true );
final BufferedImage res = new BufferedImage( 50, 50, IMAGE_TYPE );
for (int x = 0; x < res.getWidth(); x++) {
  for (int y = 0; y < res.getHeight(); y++) {
    res.setRGB( x, y, c.getRGB() - r.nextInt(150) );

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

canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
fillCanvas(Color.BLUE);
drawRect(Color.RED, 0, 0, width/2, height/2);
return new Dimension(canvas.getWidth(), canvas.getHeight());
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(canvas, null, null);
int color = c.getRGB();
for (int x = 0; x < canvas.getWidth(); x++) {
  for (int y = 0; y < canvas.getHeight(); y++) {
    canvas.setRGB(x, y, color);
int color = c.getRGB();
    canvas.setRGB(x, y, color);
int width = 640;
int height = 480;
JFrame frame = new JFrame("Direct draw demo");
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

代码示例来源:origin: skylot/jadx

public void decode(InputStream in, OutputStream out) throws JadxException {
  try {
    byte[] data = IOUtils.toByteArray(in);
    BufferedImage im = ImageIO.read(new ByteArrayInputStream(data));
    int w = im.getWidth();
    int h = im.getHeight();
    BufferedImage im2 = new BufferedImage(w + 2, h + 2, BufferedImage.TYPE_INT_ARGB);
    im2.createGraphics().drawImage(im, 1, 1, w, h, null);
    NinePatch np = getNinePatch(data);
    drawHLine(im2, h + 1, np.padLeft + 1, w - np.padRight);
    drawVLine(im2, w + 1, np.padTop + 1, h - np.padBottom);
    int[] xDivs = np.xDivs;
    for (int i = 0; i < xDivs.length; i += 2) {
      drawHLine(im2, 0, xDivs[i] + 1, xDivs[i + 1]);
    }
    int[] yDivs = np.yDivs;
    for (int i = 0; i < yDivs.length; i += 2) {
      drawVLine(im2, 0, yDivs[i] + 1, yDivs[i + 1]);
    }
    ImageIO.write(im2, "png", out);
  } catch (IOException | NullPointerException ex) {
    throw new JadxException(ex.toString());
  }
}

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

private void createShadowPicture(BufferedImage image) {
  int width = image.getWidth();
  int height = image.getHeight();
  int extra = 0;
  if (System.getProperty("os.name").equalsIgnoreCase("Windows XP")) {
    extra = 14; // Only create shadow if Windows XP (avoids double shadow in Mac OS; not tested for other OSes)
  }
  setSize(new Dimension(width + extra, height + extra));
  setLocationRelativeTo(null);
  Rectangle windowRect = getBounds();
  splash = new BufferedImage(width + extra, height + extra, BufferedImage.TYPE_INT_ARGB);
  Graphics2D g2 = (Graphics2D) splash.getGraphics();
  try {
    Robot robot = new Robot(getGraphicsConfiguration().getDevice());
    BufferedImage capture = robot.createScreenCapture(new Rectangle(windowRect.x, windowRect.y, windowRect.width + extra,
        windowRect.height + extra));
    g2.drawImage(capture, null, 0, 0);
  } catch (AWTException e) {
  }
  BufferedImage shadow = new BufferedImage(width + extra, height + extra, BufferedImage.TYPE_INT_ARGB);
  Graphics g = shadow.getGraphics();
  g.setColor(new Color(0.0f, 0.0f, 0.0f, 0.3f));
  g.fillRoundRect(6, 6, width, height, 12, 12);
  g2.drawImage(shadow, getBlurOp(7), 0, 0);
  g2.drawImage(image, 0, 0, this);
}

相关文章