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

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

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

BufferedImage.getScaledInstance介绍

暂无

代码示例

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

public static BufferedImage resize(BufferedImage img, int newW, int newH) { 
  Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
  BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);

  Graphics2D g2d = dimg.createGraphics();
  g2d.drawImage(tmp, 0, 0, null);
  g2d.dispose();

  return dimg;
}

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

final float FACTOR  = 4f;
BufferedImage img = ImageIO.read(new File("graphic.png"));
int scaleX = (int) (img.getWidth() * FACTOR);
int scaleY = (int) (img.getHeight() * FACTOR);
Image image = img.getScaledInstance(scaleX, scaleY, Image.SCALE_SMOOTH);
BufferedImage buffered = new BufferedImage(scaleX, scaleY, TYPE);
buffered.getGraphics().drawImage(image, 0, 0 , null);

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

BufferedImage trayIconImage = ImageIO.read(getClass().getResource("/path/to/icon.png"));
int trayIconWidth = new TrayIcon(trayIconImage).getSize().width;
TrayIcon trayIcon = new TrayIcon(trayIconImage.getScaledInstance(trayIconWidth, -1, Image.SCALE_SMOOTH));

代码示例来源:origin: org.springframework.boot/spring-boot

private BufferedImage resizeImage(BufferedImage image, int width, int height) {
  if (width < 1) {
    width = 1;
  }
  if (height <= 0) {
    double aspectRatio = (double) width / image.getWidth() * 0.5;
    height = (int) Math.ceil(image.getHeight() * aspectRatio);
  }
  BufferedImage resized = new BufferedImage(width, height,
      BufferedImage.TYPE_INT_RGB);
  Image scaled = image.getScaledInstance(width, height, Image.SCALE_DEFAULT);
  resized.getGraphics().drawImage(scaled, 0, 0, null);
  return resized;
}

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

/**
 * Re-size a BufferedImage to the given dimensions.
 *
 * @param image the BufferedImage.
 * @param newWidth The width to set the BufferedImage to.
 * @param newHeight The height to set the BufferedImage to.
 * @return The BufferedImage with the specified dimensions
 */
public static BufferedImage resizeImage(final BufferedImage image, final int newWidth, final int newHeight)
{
  final Image resized = image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
  return ImageUtil.bufferedImageFromImage(resized);
}

代码示例来源:origin: JpressProjects/jpress

/**
 * 高保真缩放
 */
private static BufferedImage resize(BufferedImage bi, int toWidth, int toHeight) {
  Graphics g = null;
  try {
    Image scaledImage = bi.getScaledInstance(toWidth, toHeight, Image.SCALE_SMOOTH);
    BufferedImage ret = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
    g = ret.getGraphics();
    g.drawImage(scaledImage, 0, 0, null);
    return ret;
  } catch (Exception e) {
    throw new RuntimeException(e);
  } finally {
    if (g != null) {
      g.dispose();
    }
  }
}

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

private BufferedImage generateAtlas(int mipMapLevel, List<BlockTile> tileImages, Color clearColor) {
    int size = atlasSize / (1 << mipMapLevel);
    int textureSize = tileSize / (1 << mipMapLevel);
    int tilesPerDim = atlasSize / tileSize;

    BufferedImage result = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
    Graphics g = result.getGraphics();

    g.setColor(clearColor);
    for (int index = 0; index < tileImages.size(); ++index) {
      int posX = (index) % tilesPerDim;
      int posY = (index) / tilesPerDim;
      BlockTile tile = tileImages.get(index);
      if (tile != null) {
        g.drawImage(tile.getImage().getScaledInstance(textureSize, textureSize, Image.SCALE_SMOOTH), posX * textureSize, posY * textureSize, null);
      } else {
        g.fillRect(posX * textureSize, posY * textureSize, textureSize, textureSize);
      }
    }

    return result;
  }
}

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

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

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

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

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

public void actionPerformed (ActionEvent event) {
  FileDialog dialog = new FileDialog(editor, "Select Image", FileDialog.LOAD);
  if (lastDir != null) dialog.setDirectory(lastDir);
  dialog.setVisible(true);
  final String file = dialog.getFile();
  final String dir = dialog.getDirectory();
  if (dir == null || file == null || file.trim().length() == 0) return;
  lastDir = dir;
  try {
    final FileHandle absolute = Gdx.files.absolute(dir + file);
    final BufferedImage read = ImageIO.read(absolute.read());
    final Image scaledInstance = read.getScaledInstance(100, -1, Image.SCALE_SMOOTH);
    final ImageIcon image = new ImageIcon(scaledInstance);
    JLabel previewImage = new JLabel(image);
    previewImage.setOpaque(true);
    previewImage.setBackground(Color.MAGENTA);
    buildImagePanel(previewImage, absolute.file());
  } catch (IOException e) {
    e.printStackTrace();
  }
}

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

public void actionPerformed (ActionEvent event) {
  FileDialog dialog = new FileDialog(editor, "Select Image", FileDialog.LOAD);
  if (lastDir != null) dialog.setDirectory(lastDir);
  dialog.setVisible(true);
  final String file = dialog.getFile();
  final String dir = dialog.getDirectory();
  if (dir == null || file == null || file.trim().length() == 0) return;
  lastDir = dir;
  try {
    final FileHandle absolute = Gdx.files.absolute(dir + file);
    final BufferedImage read = ImageIO.read(absolute.read());
    final Image scaledInstance = read.getScaledInstance(100, -1, Image.SCALE_SMOOTH);
    final ImageIcon image = new ImageIcon(scaledInstance);
    JLabel previewImage = new JLabel(image);
    previewImage.setOpaque(true);
    previewImage.setBackground(Color.MAGENTA);
    buildImagePanel(previewImage, absolute.file());
  } catch (IOException e) {
    e.printStackTrace();
  }
}

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

/**
 * 缩放图像(按长宽缩放)<br>
 * 注意:目标长宽与原图不成比例会变形
 * 
 * @param width 目标宽度
 * @param height 目标高度
 * @return this
 */
public Img scale(int width, int height) {
  final BufferedImage srcImg = getValidSrcImg();
  int srcHeight = srcImg.getHeight();
  int srcWidth = srcImg.getWidth();
  int scaleType;
  if (srcHeight == height && srcWidth == width) {
    // 源与目标长宽一致返回原图
    this.targetImage = srcImg;
    return this;
  } else if (srcHeight < height || srcWidth < width) {
    // 放大图片使用平滑模式
    scaleType = Image.SCALE_SMOOTH;
  } else {
    scaleType = Image.SCALE_DEFAULT;
  }
  final Image image = srcImg.getScaledInstance(width, height, scaleType);
  this.targetImage = ImageUtil.toBufferedImage(image);
  return this;
}

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

/**
 * 缩放图像(按长宽缩放)<br>
 * 注意:目标长宽与原图不成比例会变形
 * 
 * @param width 目标宽度
 * @param height 目标高度
 * @return this
 */
public Img scale(int width, int height) {
  final BufferedImage srcImg = getValidSrcImg();
  int srcHeight = srcImg.getHeight();
  int srcWidth = srcImg.getWidth();
  int scaleType;
  if (srcHeight == height && srcWidth == width) {
    // 源与目标长宽一致返回原图
    this.targetImage = srcImg;
    return this;
  } else if (srcHeight < height || srcWidth < width) {
    // 放大图片使用平滑模式
    scaleType = Image.SCALE_SMOOTH;
  } else {
    scaleType = Image.SCALE_DEFAULT;
  }
  final Image image = srcImg.getScaledInstance(width, height, scaleType);
  this.targetImage = ImageUtil.toBufferedImage(image);
  return this;
}

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

int newHeight = (int) (screenshotImage.getHeight() / estimatedPixelRatio);
Image tmp = screenshotImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
BufferedImage scaledImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);

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

g2.drawImage(ret.getScaledInstance(w, h, Image.SCALE_SMOOTH), 0, 0, w, h, null);
g2.dispose();

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

int width = term.width();
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();

代码示例来源:origin: JpressProjects/jpress

/**
 * 等比缩放,居中剪切
 *
 * @param src
 * @param dest
 * @param w
 * @param h
 * @throws IOException
 */
public static void scale(String src, String dest, int w, int h) throws IOException {
  if (notImageExtName(src)) {
    throw new IllegalArgumentException("只支持如下几种图片格式:jpg、jpeg、png、bmp");
  }
  Iterator<ImageReader> iterator = ImageIO.getImageReadersByFormatName(getExtName(src));
  ImageReader reader = (ImageReader) iterator.next();
  InputStream in = new FileInputStream(src);
  ImageInputStream iis = ImageIO.createImageInputStream(in);
  reader.setInput(iis);
  BufferedImage srcBuffered = readBuffereImage(reader, w, h);
  BufferedImage targetBuffered = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  Graphics graphics = targetBuffered.getGraphics();
  graphics.drawImage(srcBuffered.getScaledInstance(w, h, Image.SCALE_DEFAULT), 0, 0, null); // 绘制缩小后的图
  graphics.dispose();
  srcBuffered.flush();
  save(targetBuffered, dest);
  targetBuffered.flush();
}

代码示例来源:origin: apache/nifi

final BufferedImage image = reader.read(0);
final Image scaledImage = image.getScaledInstance(width, height, hints);
final BufferedImage scaledBufferedImg;
if (scaledImage instanceof BufferedImage) {

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

materialTab.setIcon(new ImageIcon(subIcon.getScaledInstance(24, 24, Image.SCALE_SMOOTH)));
};
icon.onChanged(resize);

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

private void loadSprites() throws IOException
{
  Storage storage = store.getStorage();
  Index index = store.getIndex(IndexType.SPRITES);
  final int mapsceneHash = Djb2.hash("mapscene");
  for (Archive a : index.getArchives())
  {
    byte[] contents = a.decompress(storage.loadArchive(a));
    SpriteLoader loader = new SpriteLoader();
    SpriteDefinition[] sprites = loader.load(a.getArchiveId(), contents);
    for (SpriteDefinition sprite : sprites)
    {
      if (sprite.getHeight() <= 0 || sprite.getWidth() <= 0)
      {
        continue;
      }
      if (a.getNameHash() == mapsceneHash)
      {
        BufferedImage spriteImage = new BufferedImage(sprite.getWidth(), sprite.getHeight(), BufferedImage.TYPE_INT_ARGB);
        spriteImage.setRGB(0, 0, sprite.getWidth(), sprite.getHeight(), sprite.getPixels(), 0, sprite.getWidth());
        // scale image down so it fits
        Image scaledImage = spriteImage.getScaledInstance(MAPICON_MAX_WIDTH, MAPICON_MAX_HEIGHT, 0);
        assert scaledMapIcons.containsKey(sprite.getFrame()) == false;
        scaledMapIcons.put(sprite.getFrame(), scaledImage);
      }
    }
  }
}

相关文章