简体   繁体   English

在Java中将图像大小调整2倍

[英]Resizing an image in java by factor of 2

This is what I have so far, but don't know what to do now? 到目前为止,这就是我所拥有的,但是现在不知道该怎么办? It makes the picture bigger but there is a lot of spaces in the picture. 它使图片更大,但图片中有很多空间。 How do you copy pixels to fill in the holes? 如何复制像素以填充孔?

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;
}

新图片中存在间隙的原因是,您只需要为每个原始像素设置一次像素,而您必须为原始图像的每个像素设置4个像素(即2x2),因为它是原始图像的两倍。

Well if you enlarge an image times two , and you don't use interpolation . 好吧,如果您将图像放大 两倍 ,并且不使用插值 Then a pixel (x,y) of the original image, should be mapped to pixels (2*x,2*y) , (2*x,2*y+1) , (2*x+1,2*y) and (2*x+1,2*y+1) . 然后应将原始图像的像素(x,y)映射到像素(2*x,2*y)(2*x,2*y+1)(2*x+1,2*y)(2*x+1,2*y+1) So the algorithm should be: 因此,算法应为:

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);
        for(int x2 = 2*x; x2 < 2*x+2; x2++) {
            for(int y2 = 2*y; y2 < 2*y+2; y2++) {
                enlarged.getPixel(x2,y2).setColor(orig.getColor());
            }
        }
    }
}

Or more generic, with a magnification parameter mag : 或更普通的,具有mag的放大参数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM