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

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

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

BufferedImage.createGraphics介绍

[英]Creates a Graphics2D, which can be used to draw into this BufferedImage.
[中]创建一个Graphics2D,可用于绘制此BufferedImage

代码示例

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

private static BufferedImage createImage (int width, int height, Color color) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D g = image.createGraphics();
    g.setColor(color);
    g.fillRect(0, 0, width, height);
    g.dispose();
    return image;
  }
}

代码示例来源: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: Bukkit/Bukkit

/**
 * Resize an image to 128x128.
 *
 * @param image The image to resize.
 * @return The resized image.
 */
public static BufferedImage resizeImage(Image image) {
  BufferedImage result = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB);
  Graphics2D graphics = result.createGraphics();
  graphics.drawImage(image, 0, 0, 128, 128, null);
  graphics.dispose();
  return result;
}

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

BufferedImage b_img = ...
Graphics2D    graphics = b_img.createGraphics();

graphics.setPaint ( new Color ( r, g, b ) );
graphics.fillRect ( 0, 0, b_img.getWidth(), b_img.getHeight() );

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

BufferedImage dest = new BufferedImage(
  imageWidth, imageHeight,
  BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = dest.createGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();

代码示例来源:origin: kiegroup/optaplanner

private Graphics2D createCanvas(double width, double height) {
  int canvasWidth = (int) Math.ceil(width) + 1;
  int canvasHeight = (int) Math.ceil(height) + 1;
  canvas = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_RGB);
  Graphics2D g = canvas.createGraphics();
  g.setColor(Color.WHITE);
  g.fillRect(0, 0, canvasWidth, canvasHeight);
  return g;
}

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

private byte[] writeImageToBytes(Image image) throws IOException
{
  BufferedImage bi = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  Graphics2D g = bi.createGraphics();
  g.drawImage(image,0,0,width,height,null);
  ImageIO.write(bi,"jpg",baos);
  baos.close();
  bi = null;
  g = null;
  
  return baos.toByteArray();
}

代码示例来源:origin: coobird/thumbnailator

public BufferedImage apply(BufferedImage img)
{
  int width = img.getWidth();
  int height = img.getHeight();
  
  BufferedImage finalImage =
    new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  
  Graphics2D g = finalImage.createGraphics();
  g.setComposite(composite);
  g.drawImage(img, 0, 0, null);
  g.dispose();
  
  return finalImage;
}

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

BufferedImage createResizedCopy(Image originalImage, 
     int scaledWidth, int scaledHeight, 
     boolean preserveAlpha)
 {
   System.out.println("resizing...");
   int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
   BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
   Graphics2D g = scaledBI.createGraphics();
   if (preserveAlpha) {
     g.setComposite(AlphaComposite.Src);
   }
   g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null); 
   g.dispose();
   return scaledBI;
 }

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

BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
g = bufferedImage.createGraphics();
//Color.WHITE estes the background to white. You can use any other color
g.drawImage(image, 0, 0, bufferedImage.getWidth(), bufferedImage.getHeight(), Color.WHITE, null);

代码示例来源:origin: looly/hutool

/**
 * 将图片绘制在背景上
 * 
 * @param backgroundImg 背景图片
 * @param img 要绘制的图片
 * @param rectangle 矩形对象,表示矩形区域的x,y,width,height,x,y从背景图片中心计算
 * @return 绘制后的背景
 */
private static BufferedImage draw(BufferedImage backgroundImg, Image img, Rectangle rectangle, float alpha) {
  final Graphics2D g = backgroundImg.createGraphics();
  g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
  g.drawImage(img, rectangle.x, rectangle.y, rectangle.width, rectangle.height, null); // 绘制切割后的图
  g.dispose();
  return backgroundImg;
}

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

private static BufferedImage createImage (int width, int height, Color color) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D g = image.createGraphics();
    g.setColor(color);
    g.fillRect(0, 0, width, height);
    g.dispose();
    return image;
  }
}

代码示例来源:origin: kiegroup/optaplanner

private Graphics2D createCanvas(double width, double height) {
  int canvasWidth = (int) Math.ceil(width) + 1;
  int canvasHeight = (int) Math.ceil(height) + 1;
  canvas = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_RGB);
  Graphics2D g = canvas.createGraphics();
  g.setColor(Color.WHITE);
  g.fillRect(0, 0, canvasWidth, canvasHeight);
  return g;
}

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

public static final ByteBuffer getImageDataFromImage(BufferedImage bufferedImage) {
  WritableRaster wr;
  DataBuffer db;
  BufferedImage bi = new BufferedImage(128, 64, BufferedImage.TYPE_INT_ARGB);
  Graphics2D g = bi.createGraphics();
  g.drawImage(bufferedImage, null, null);
  bufferedImage = bi;
  wr = bi.getRaster();
  db = wr.getDataBuffer();
  DataBufferInt dbi = (DataBufferInt) db;
  int[] data = dbi.getData();
  ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.length * 4);
  byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
  byteBuffer.asIntBuffer().put(data);
  byteBuffer.flip();
  return byteBuffer;
}

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

private static BufferedImage scaleDown(BufferedImage sourceImage, int targetWidth, int targetHeight) {
  int sourceWidth  = sourceImage.getWidth();
  int sourceHeight = sourceImage.getHeight();
  BufferedImage targetImage = new BufferedImage(targetWidth, targetHeight, sourceImage.getType());
  Graphics2D g = targetImage.createGraphics();
  g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  g.drawImage(sourceImage, 0, 0, targetWidth, targetHeight, 0, 0, sourceWidth, sourceHeight, null);
  g.dispose();
  return targetImage;
}

代码示例来源:origin: ivan-vasilev/neuralnetworks

private BufferedImage resize(BufferedImage input, int targetWidth, int targetHeight)
  {
    BufferedImage result = new BufferedImage(targetWidth, targetHeight, input.getType());

    Graphics2D g = result.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.drawImage(input, 0, 0, targetWidth, targetHeight, null);
    g.dispose();

    return result;
  }
}

代码示例来源:origin: looly/hutool

/**
 * 创建{@link Graphics2D}
 * 
 * @param image {@link BufferedImage}
 * @param color {@link Color}背景颜色以及当前画笔颜色
 * @return {@link Graphics2D}
 * @since 3.2.3
 */
public static Graphics2D createGraphics(BufferedImage image, Color color) {
  final Graphics2D g = image.createGraphics();
  // 填充背景
  g.setColor(color);
  g.fillRect(0, 0, image.getWidth(), image.getHeight());
  return g;
}

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

private Image getScaledImage(Image srcImg, int w, int h){
  BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  Graphics2D g2 = resizedImg.createGraphics();

  g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  g2.drawImage(srcImg, 0, 0, w, h, null);
  g2.dispose();

  return resizedImg;
}

代码示例来源:origin: chewiebug/GCViewer

/**
 * Creates an empty image.
 *
 * @param width width of image
 * @param height height of image
 * @return empty image
 */
public static ImageIcon createEmptyImageIcon(final int width, final int height) {
  final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE);
  final Graphics2D g = image.createGraphics();
  g.dispose();
  return new ImageIcon(image);
}

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

public Font getFontFor(int pixelHeight){
  BufferedImage image = new BufferedImage(1,1, BufferedImage.TYPE_INT_RGB);
  Graphics2D g2 = image.createGraphics();
  return getFontFor(pixelHeight, fakeRenderContext);
}

相关文章