在java中按2倍调整图像大小

在java中按2倍调整图像大小,java,image,pixel,image-resizing,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

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

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;
}
公共图片放大()
{
图片放大=新图片(getWidth()*2,getHeight()*2);
对于(int x=0;x
新图片中存在间隙的原因是,您仅为每个原始像素设置一次像素,而您必须为原始图像的每个像素设置4个像素(即2x2),因为它是原始图像的两倍大。

如果将图像放大两倍,并且不使用插值。然后,原始图像的像素
(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);
        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());
            }
        }
    }
}

为什么要使用
放大像素2
?我试图制作第二组像素来填充孔
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());
            }
        }
    }
}