java - 在java中将图像大小调整为2倍

标签 java image pixel image-resizing

这是我到目前为止所拥有的,但不知道现在该怎么办?它使图片变大,但图片中有很多空间。如何复制像素来填充孔?

public Picture enlarge()
{
 Picture enlarged = new Picture(getWidth() * 2, getHeight() * 2);

for (int x = 0; x < getWidth(); x++)
{
  for (int y = 0; y < getHeight(); y++)
  {
    Pixel orig = getPixel(x,y);
    Pixel enlargedPix = enlarged.getPixel(x*2,y*2);
    Pixel enlargedPix2 = enlarged. getPixel(x,y);
    enlargedPix.setColor(orig.getColor());
    enlargedPix2.setColor(orig.getColor());
  }
}
return enlarged;
}

最佳答案

好吧,如果您将图像放大两倍,并且不使用插值。然后原始图像的像素 (x,y) 应该映射到像素 (2*x,2*y), (2*x, 2*y+1)(2*x+1,2*y)(2*x+1,2*y+1) 。所以算法应该是:

Picture enlarged = new Picture(getWidth() * 2, getHeight() * 2);
for (int x = 0; x < getWidth(); x++) {
    for (int y = 0; y < getHeight(); y++) {
        Pixel orig = getPixel(x,y);
        <b>for(int x2 = 2*x; x2 < 2*x+2; x2++) {</b>
            <b>for(int y2 = 2*y; y2 < 2*y+2; y2++) {</b>
                enlarged.getPixel(<b>x2,y2</b>).setColor(orig.getColor());
            <b>}</b>
        <b>}</b>
    }
}

或者更通用,带有放大参数mag:

Picture enlarged = new Picture(getWidth() * <b>mag</b>, getHeight() * <b>mag</b>);
for (int x = 0; x < getWidth(); x++) {
    for (int y = 0; y < getHeight(); y++) {
        Pixel orig = getPixel(x,y);
        for(int x2 = <b>mag</b>*x; x2 < <b>mag</b>*x+<b>mag</b>; x2++) {
            for(int y2 = <b>mag</b>*y; y2 < <b>mag</b>*y+<b>mag</b>; y2++) {
                enlarged.getPixel(x2,y2).setColor(orig.getColor());
            }
        }
    }
}

关于java - 在java中将图像大小调整为2倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43326214/

相关文章:

java - Android Activity 中没有 OnStart()

java - 导入具有相同IRI的本体

c++ - 读取 16 位 DPX 像素数据

image - 如何使用 apache 或 .htaccess 限制对某种内容的访问?

android - 从相机到 Imageview 的图像分辨率

python - 将像素转换为黑白

java - 交换二维数组元素

java - 无法在我的 Android 应用程序中从 mysql 数据库获取数据到 ListView

php - 是否可以使用 PHP 获取图像鼠标单击位置?

javascript - 节点/圆顶部的 d3 图像不适用于 clipPath