如何在Java中给图像添加边框?

huangapple go评论76阅读模式
英文:

How can I add a border to an image in Java?

问题

下面是你提供的代码翻译后的部分:

/**
 * TODO 待完成的方法。包含需要更改的一些代码
 *
 * @param enlargeFactorPercentage 边框百分比的放大因子
 * @param dimAvg                  边框像素的平均颜色半径
 *
 * @return 带有扩展边框的新图像
 */
public static BufferedImage addBorders(BufferedImage image, int enlargeFactorPercentage, int dimAvg) {

    // TODO 待完成的方法

    int height = image.getHeight();
    int width = image.getWidth();

    System.out.println("图像高度 = " + height);
    System.out.println("图像宽度 = " + width);

    // 创建新图像
    BufferedImage bi = new BufferedImage(width, height, image.getType());

    // 复制图像
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int pixelRGB = image.getRGB(x, y);
            bi.setRGB(x, y, pixelRGB);
        }
    }

    // 绘制顶部和底部边框

    // 绘制左侧和右侧边框

    // 绘制角落

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int pixelRGB = image.getRGB(x, y);
            for (enlargeFactorPercentage = 0; enlargeFactorPercentage < 10; enlargeFactorPercentage++) {
                bi.setRGB(width, enlargeFactorPercentage, pixelRGB * dimAvg);
                bi.setRGB(enlargeFactorPercentage, height, pixelRGB * dimAvg);
            }
        }
    }

    return bi;
}

请注意,由于你要求只返回翻译后的代码部分,我已经过滤掉了你原始代码中的注释、空白行以及其他非代码相关的内容。

英文:

The border needs to be made out of the closest pixel of the given image, I saw some code online and came up with the following. What am I doing wrong? I'm new to java, and I am not allowed to use any methods.

/**
* TODO Method to be done. It contains some code that has to be changed
* 
* @param enlargeFactorPercentage the border in percentage
* @param dimAvg                  the radius in pixels to get the average colour
*                                of each pixel for the border
* 
* @return a new image extended with borders
*/
public static BufferedImage addBorders(BufferedImage image, int enlargeFactorPercentage, int dimAvg) {
// TODO method to be done
int height = image.getHeight();
int width = image.getWidth();
System.out.println(&quot;Image height = &quot; + height);
System.out.println(&quot;Image width = &quot; + width);
// create new image
BufferedImage bi = new BufferedImage(width, height, image.getType());
// copy image
for (int y = 0; y &lt; height; y++) {
for (int x = 0; x &lt; width; x++) {
int pixelRGB = image.getRGB(x, y);
bi.setRGB(x, y, pixelRGB);
}
}
// draw top and bottom borders
// draw left and right borders
// draw corners
for (int y = 0; y &lt; height; y++) {
for (int x = 0; x &lt; width; x++) {
int pixelRGB = image.getRGB(x, y);
for (enlargeFactorPercentage = 0; enlargeFactorPercentage &lt; 10; enlargeFactorPercentage++){
bi.setRGB(width, enlargeFactorPercentage, pixelRGB * dimAvg);
bi.setRGB(enlargeFactorPercentage, height, pixelRGB * dimAvg);
}
}
}
return bi;

答案1

得分: 1

我不被允许使用任何方法。

那是什么意思?如果不能使用API中的方法,你怎么编写代码呢?

int enlargeFactorPercentage

这是做什么的?对我来说,enlarge 意味着使变大。所以如果你有一个因子是 10,图像尺寸是 (100, 100),那么新图像尺寸将会是 (110, 110),这意味着边框会有 5 像素?

你的代码创建的 BufferedImage 大小与原始图像相同。这是不是意味着你会让边框变为 5 像素,并从原始图像上裁剪掉 5 像素?

没有明确的需求,我们无法提供帮助。

@return 带有扩展边框的新图像

由于你还有一个注释说“扩展”,我会假设你的要求是返回较大的图像。

所以我会采取以下解决方案:

  1. 在增加的尺寸上创建 BufferedImage。
  2. 从 BufferedImage 获取 Graphics2D 对象。
  3. 使用 Graphics2D.fillRect(….) 方法,用你想要的边框颜色填充整个 BufferedImage。
  4. 使用 Graphics2D.drawImage(…) 方法将原始图像绘制到放大的 BufferedImage 上。
英文:

> I am not allowed to use any methods.

What does that mean? How can you write code if you can't use methods from the API?

int enlargeFactorPercentage

What is that for? To me, enlarge means to make bigger. So if you have a factor of 10 and your image is (100, 100), then the new image would be (110, 110), which means the border would be 5 pixels?

Your code is creating the BufferedImage the same size as the original image. So does that mean you make the border 5 pixels and chop off 5 pixels from the original image?

Without proper requirements we can't help.

> @return a new image extended with borders

Since you also have a comment that says "extended", I'm going to assume your requirement is to return the larger image.

So the solution I would use is to:

  1. create the BufferedImage at the increased size
  2. get the Graphics2D object from the BufferImage
  3. fill the entire BufferedImage with the color you want for the border using the Graphics2D.fillRect(….) method
  4. paint the original image onto the enlarged BufferedImage using the Graphics2D.drawImage(…) method.

答案2

得分: 0

欢迎来到stackoverflow!

关于“不允许使用方法”,我不太明白你的意思。如果没有方法,甚至无法运行程序,因为具有`public static void main(String[] args)`的东西是一个方法(主方法),你需要它,因为它是程序的起始点...

但是为了回答你的问题:
你需要加载图像。一种可能性是使用ImageIO。然后创建一个2D图形对象,然后可以使用drawRectangle()创建一个边框矩形:

    BufferedImage bi = //加载图像
    Graphics2D g = bi.getGraphics();
    g.drawRectangle(0, 0, bi.getHeight(), bi.getWidth());

这段简短的代码只是一个提示。尝试一下,并阅读[此处的BufferedImage文档][1]和[Graphics2D文档][2]。

编辑:请注意,这并不完全正确。使用上面的代码会覆盖图像的外部像素线。如果你不想剪切任何像素,那么你需要将其放大并使用`bi.getHeight()+2`和`bi.getWidth()+2`进行绘制。+2是因为你需要在图像的每一侧多一个像素。

  [1]: https://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html
  [2]: https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html
英文:

Hello and welcome to stackoverflow!

Not sure what you mean with "not allowed using methods". Without methods you can not even run a program because the "thing" with public static void main(String[] args) is a method (the main method) and you need it, because it is the program starting point...

But to answer your question:
You have to load your image. A possibility would be to use ImageIO. Then you create a 2D graphics object and then you can to drawRectangle() to create a border rectangle:

BufferedImage bi = //load image
Graphics2D g = bi.getGraphics();
g.drawRectangle(0, 0, bi.getHeight(), bi.getWidth());

This short code is just a hint. Try it out and read the documentation from Bufferedimage see here and from Graphics2D

Edit: Please notice that this is not quite correct. With the code above you overdraw the outer pixel-line from the image. If you don't want to cut any pixel of, then you have to scale it up and draw with bi.getHeight()+2 and bi.getWidth()+2. +2 because you need one pixel more at each side of the image.

huangapple
  • 本文由 发表于 2020年4月5日 03:39:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/61033769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定