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

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

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

BufferedImage.getSubimage介绍

[英]Returns a subimage defined by a specified rectangular region. The returned BufferedImage shares the same data array as the original image.
[中]返回由指定矩形区域定义的子图像。返回的BufferedImage与原始图像共享相同的数据数组。

代码示例

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

private BufferedImage smaller(BufferedImage im) {
  if (im == null) {
    return null;
  }
  final int nb = 1;
  return im.getSubimage(nb, nb, im.getWidth() - 2 * nb, im.getHeight() - 2 * nb);
}

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

@Override
final protected ImageData exportDiagramNow(OutputStream os, int num, FileFormatOption fileFormatOption, long seed)
    throws IOException {
  final BufferedImage im = createImage();
  final ImageData imageData = new ImageDataSimple(im.getWidth(), (int) maxYpos);
  PngIO.write(im.getSubimage(0, 0, imageData.getWidth(), imageData.getHeight()), os, 96);
  return imageData;
}

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

final BufferedImage source = ImageIO.read(new File("<sourceDir>/1fby-6t-555d.png"));
int idx = 0;
for (int y = 0; y < source.getHeight(); y += 32) {
  ImageIO.write(source.getSubimage(0, y, 32, 32), "png", new File("<sourceDir>/1fby-6t-555d_" + idx++ + ".png"));
}

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

@Override
public void addAugmentedImages(List<BufferedImage> images)
{
  int l = images.size();
  for (int i = 0; i < l; i++)
  {
    BufferedImage im = images.get(i);
    images.add(im.getSubimage(0, 0, cropX, cropY));
    images.add(im.getSubimage(im.getWidth() - cropX, 0, cropX, cropY));
    images.add(im.getSubimage(0, im.getHeight() - cropY, cropX, cropY));
    images.add(im.getSubimage(im.getWidth() - cropX, im.getHeight() - cropY, cropX, cropY));
    images.add(im.getSubimage((im.getWidth() - cropX) / 2, (im.getHeight() - cropY) / 2, cropX, cropY));
  }
  images.subList(0, l).clear();
}

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

@Override
public OcrResult findOcrText(BufferedImage image, Rect rect) throws ValidationErrorException {
  if (rect.getRight() > image.getWidth() && rect.getBottom() > rect.getHeight()) {
    throw new ValidationErrorException("Could not extract element image. Looks like it is located outside of screenshot area");
  }
  try {
    BufferedImage croppedImage = image.getSubimage(rect.getLeft(), rect.getTop(), rect.getWidth(), rect.getHeight());
    GoogleModel model = getGoogleModel(croppedImage);
    if (model.responses != null && !model.responses.isEmpty()) {
      String resultedText = model.responses.get(0).fullTextAnnotation.text;
      if (resultedText == null) {
        resultedText = "";
      }
      return new OcrResult(new String(resultedText.getBytes(Charset.forName("utf-8"))), rect);
    } else {
      throw new NullPointerException("Got empty result");
    }
  } catch (Exception e) {
    throw new ValidationErrorException("Google vision API error: " + e.getMessage(), e);
  }
}

代码示例来源:origin: songgeb/BDIndexSpider

private static ArrayList<BufferedImage> clipBDIndexImage(BufferedImage image) {
  ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
  
  int[] indices = getWidthForNumber(image, 0);
  while (indices != null) {
    int numberWidth = indices[1] - indices[0] + 1;
    if (numberWidth > 10) {//宽度太小的是逗号,不需要保存该数据
      BufferedImage subImage = image.getSubimage(indices[0], 0, numberWidth, image.getHeight());
      images.add(subImage);
    }
    indices = getWidthForNumber(image, indices[1]+1);
  }
  return images;
}

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

BufferedImage srcImage = page.getSubimage(region.left, region.top, region.height, region.width);
  splitImage = new BufferedImage(region.width, region.height, page.getType());
  op.filter(srcImage, splitImage);
} else {
  splitImage = page.getSubimage(region.left, region.top, region.width, region.height);
  BufferedImage paddedImage = new BufferedImage(splitImage.getWidth() + padding * 2, splitImage.getHeight() + padding * 2,
    page.getType());
  Graphics2D g2 = paddedImage.createGraphics();

代码示例来源:origin: MrCrayfish/ModelCreator

private static ImageIcon createIcon(BufferedImage source)
{
  source = source.getSubimage(0, 0, source.getWidth(), source.getWidth());
  Image scaledImage = source.getScaledInstance(64, 64, java.awt.Image.SCALE_FAST);
  return new ImageIcon(scaledImage);
}

代码示例来源:origin: oskopek/javaanpr

private BufferedImage cutLeftRight(BufferedImage origin, PlateHorizontalGraph graph) {
  graph.applyProbabilityDistributor(new Graph.ProbabilityDistributor(0f, 0f, 2, 2));
  List<Peak> peaks = graph.findPeak();
  if (peaks.size() != 0) {
    Peak p = peaks.get(0);
    return origin.getSubimage(p.getLeft(), 0, p.getDiff(), getImage().getHeight());
  }
  return origin;
}

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

BufferedImage srcImage = page.getSubimage(region.left, region.top, region.height, region.width);
  splitImage = new BufferedImage(region.width, region.height, page.getType());
  op.filter(srcImage, splitImage);
} else {
  splitImage = page.getSubimage(region.left, region.top, region.width, region.height);
  BufferedImage paddedImage = new BufferedImage(splitImage.getWidth() + padding * 2, splitImage.getHeight() + padding * 2,
    page.getType());
  Graphics2D g2 = paddedImage.createGraphics();

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

public static void takeScreenshot(String screenshotPathAndName, WebDriver driver) {
  File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  try {

    int height = driver.manage().window().getSize().getHeight();
    BufferedImage img = ImageIO.read(scrFile);
    BufferedImage dest = img.getSubimage(0, 0, img.getWidth(), height);
    ImageIO.write(dest, "png", scrFile);

    FileUtils.copyFile(scrFile, new File(screenshotPathAndName));
  } catch(Exception e) {
    e.printStackTrace();
  }
}

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

@Override
public void addAugmentedImages(List<BufferedImage> images)
{
  int l = images.size();
  for (int i = 0; i < l; i++)
  {
    BufferedImage im = images.get(i);
    for (int j = 0; j < crops; j++)
    {
      images.add(im.getSubimage(random.nextInt(im.getWidth() - cropX), random.nextInt(im.getHeight() - cropY), cropX, cropY));
    }
  }
  images.subList(0, l).clear();
}

代码示例来源:origin: oskopek/javaanpr

private BufferedImage cutTopBottom(BufferedImage origin, PlateVerticalGraph graph) {
  graph.applyProbabilityDistributor(new Graph.ProbabilityDistributor(0f, 0f, 2, 2));
  Peak p = graph.findPeak(3).get(0);
  return origin.getSubimage(0, p.getLeft(), getImage().getWidth(), p.getDiff());
}

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

@Override
public void addAugmentedImages(List<BufferedImage> images)
{
  int l = images.size();
  for (int i = 0; i < l; i++)
  {
    BufferedImage source = images.get(i);
    for (int x = 0; x <= source.getWidth() - width; x += strideX)
    {
      for (int y = 0; y <= source.getHeight() - height; y += strideY)
      {
        images.add(source.getSubimage(x, y, width, height));
      }
    }
  }
  images.subList(0, l).clear();
}

代码示例来源:origin: net.sourceforge.plantuml/plantuml

@Override
final protected ImageData exportDiagramNow(OutputStream os, int num, FileFormatOption fileFormatOption)
    throws IOException {
  final BufferedImage im = createImage();
  final ImageData imageData = new ImageDataSimple(im.getWidth(), (int) maxYpos);
  PngIO.write(im.getSubimage(0, 0, imageData.getWidth(), imageData.getHeight()), os, 96);
  return imageData;
}

代码示例来源:origin: shopizer-ecommerce/shopizer

public BufferedImage getCroppedImage() throws IOException {
  
    //out if croppedArea == 0 or file is null
  
  
    Rectangle goal = new Rectangle( (int)this.getCropAreaWidth(), (int) this.getCropAreaHeight()); 
    
    //Then intersect it with the dimensions of your image:
    Rectangle clip = goal.intersection(new Rectangle(originalFile.getWidth(), originalFile.getHeight())); 
    
    //Now, clip corresponds to the portion of bi that will fit within your goal. In this case 100 x50.
    //Now get the subImage using the value of clip.
    BufferedImage clippedImg = originalFile.getSubimage(clip.x, clip.y, clip.width, clip.height); 
    
    return clippedImg;
  
  
  
}

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

@Override
public List<BufferedImage> getNextRawImages()
{
  if (images == null)
  {
    images = new BufferedImage[properties.getImagesBulkSize()];
  }
  IntStream stream = IntStream.range(0, properties.getImagesBulkSize());
  stream.forEach(i -> {
    if (sourceImages.get(currentImage).getWidth() - width > currentX)
    {
      currentX++;
    } else if (sourceImages.get(currentImage).getHeight() - height > currentY)
    {
      currentY++;
      currentX = 0;
    } else
    {
      currentImage = (currentImage + 1) % sourceImages.size();
      currentY = currentX = 0;
    }
    images[i] = sourceImages.get(currentImage).getSubimage(currentX, currentY, width, height);
  });
  return Arrays.asList(images);
}

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

switch (resizeType) {
case SMALLEST_DIMENSION_RECT:
  if (input.getWidth() < input.getHeight())
    targetHeight = (int) (input.getHeight() / ((float) input.getWidth()) * smallestDimension);
  } else
    targetWidth = (int) (input.getWidth() / ((float) input.getHeight()) * smallestDimension);
    targetHeight = smallestDimension;
  if (input.getWidth() < input.getHeight())
    input = input.getSubimage(0, (input.getHeight() - input.getWidth()) / 2, input.getWidth(), input.getWidth());
  } else
    input = input.getSubimage((input.getWidth() - input.getHeight()) / 2, 0, input.getHeight(), input.getHeight());

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

/**
 * 剪切
 *
 * @param srcImageFile  原图
 * @param destImageFile 存放的目标位置
 * @param left          起始点:左
 * @param top           起始点:上
 * @param width         宽
 * @param height        高
 */
public static void crop(String srcImageFile, String destImageFile, int left, int top, int width, int height) {
  if (notImageExtName(srcImageFile)) {
    throw new IllegalArgumentException("只支持如下几种图片格式:jpg、jpeg、png、bmp");
  }
  try {
    BufferedImage bi = ImageIO.read(new File(srcImageFile));
    width = Math.min(width, bi.getWidth());
    height = Math.min(height, bi.getHeight());
    if (width <= 0) width = bi.getWidth();
    if (height <= 0) height = bi.getHeight();
    left = Math.min(Math.max(0, left), bi.getWidth() - width);
    top = Math.min(Math.max(0, top), bi.getHeight() - height);
    BufferedImage subimage = bi.getSubimage(left, top, width, height);
    BufferedImage resizeImage = resize(subimage, 200, 200);
    save(resizeImage, destImageFile);
  } catch (Exception e) {
    log.error(e.toString(), e);
  }
}

代码示例来源:origin: igniterealtime/Openfire

if ( avatar.getWidth() <= targetDimension && avatar.getHeight() <= targetDimension )
    Log.debug( "Original image dimension ({}x{}) is within acceptable bounds ({}x{}). No need to resize.", new Object[] { avatar.getWidth(), avatar.getHeight(), targetDimension, targetDimension });
    return null;
Log.debug( "Original image is " + avatar.getWidth() + "x" + avatar.getHeight() + " pixels" );
  avatar = avatar.getSubimage( x, y, targetWidth, targetHeight );

相关文章