Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java BuffereImage:如何通过访问像素阵列正确复制图像n次?_Java_Arrays_Pixel_Bufferedimage - Fatal编程技术网

Java BuffereImage:如何通过访问像素阵列正确复制图像n次?

Java BuffereImage:如何通过访问像素阵列正确复制图像n次?,java,arrays,pixel,bufferedimage,Java,Arrays,Pixel,Bufferedimage,我正在处理一个问题,我正在处理一个项目的哪种“分叉”。对于项目本身来说,这并不是必须解决的问题,但我要提到起源,因为这是一项“奇怪的特定”任务 我正在尝试从文件(8x8像素)读取一个小的缓冲区图像。 该图像的像素被写入一个整数数组,长度为64(显然) 然后,创建一个长度为64*64(=4096)的新数组。小数组的像素被复制到大数组中64次,每次到达末尾时,只需将较小的索引重置为0 最后,我创建了一个宽度为64、高度为64的新BuffereImage。然后将大数组设置为所述BuffereImage

我正在处理一个问题,我正在处理一个项目的哪种“分叉”。对于项目本身来说,这并不是必须解决的问题,但我要提到起源,因为这是一项“奇怪的特定”任务

我正在尝试从文件(8x8像素)读取一个小的缓冲区图像。 该图像的像素被写入一个整数数组,长度为64(显然)

然后,创建一个长度为64*64(=4096)的新数组。小数组的像素被复制到大数组中64次,每次到达末尾时,只需将较小的索引重置为0

最后,我创建了一个宽度为64、高度为64的新BuffereImage。然后将大数组设置为所述BuffereImage的rgbArray。代码如下:

public static void main(String[] args)throws IOException {
    
    BufferedImage toCopy = ImageIO.read(new File("smallStripes.png"));
    BufferedImage copiedNTimes = new BufferedImage(64, 64, BufferedImage.TYPE_BYTE_BINARY); 
    //copiedNTimes is to be the resulting image

    Graphics2D g2d = (Graphics2D) copiedNTimes.getGraphics();
    g2d.setColor(Color.WHITE);
    g2d.fillRect(0, 0, 64, 64);

    int[] smallPixels = new int[64];

    toCopy.getRGB(0, 0, 8, 8, smallPixels, 0, 8);
    //copy the rgb array of read image into the 64 - array

    int[] copied = copyNTimes(smallPixels, new int[64*64]);

    copiedNTimes.setRGB(0, 0, 64, 64, copied, 0, 8);
    //setting the rgb array of result image to the copied one

    FileOutputStream fos = new FileOutputStream(new File("result.png"));
    ImageIO.write(copiedNTimes, "png", fos);
}

static int[] copyNTimes(int[] small, int[] big){

    //this method copies the small array into the larger one
    //until the larger one is 'filled up'

    int index = 0;

    for(int x = 0 ; x < big.length; x++){
        big[x] = small[index];
        index++;
        if(index == small.length)
            index = 0;
    }

    return big;
}
publicstaticvoidmain(字符串[]args)引发IOException{
buffereImage toCopy=ImageIO.read(新文件(“smallstrips.png”);
BuffereImage copiedNTimes=新的BuffereImage(64,64,BuffereImage.TYPE_BYTE_BINARY);
//复制时间将作为结果图像
Graphics2D g2d=(Graphics2D)copiedEntimes.getGraphics();
g2d.setColor(Color.WHITE);
g2d.fillRect(0,0,64,64);
int[]smallPixels=新的int[64];
getRGB(0,0,8,8,小像素,0,8);
//将读取图像的rgb数组复制到64数组中
int[]复制=复制次数(小像素,新int[64*64]);
setRGB(0,0,64,64,复制的0,8);
//将结果图像的rgb阵列设置为复制的阵列
FileOutputStream fos=新的FileOutputStream(新文件(“result.png”));
ImageIO.write(复制时间,“png”,fos);
}
静态int[]复制时间(int[]小,int[]大){
//此方法将小数组复制到大数组中
//直到较大的一个被“填满”
int指数=0;
对于(int x=0;x
它或多或少像我预期的那样工作,但图像被写为“移位”:

smallstrips.png:

result.png:

我的问题是:

我怎样才能做到条纹彼此“对齐”?现在它是,从左到右,8px黑色,8px白色,8px黑色。。。等等 为什么不是64像素黑色(新线)64像素白色(新线)等


如前所述,它异常具体且过于简单,因此我可以更好地描述它。

您的代码使用scanline=8作为设置RGB的最后一个参数,并且在复制时间中使用错误的逻辑,从而导致剥离效果。如果要将8x8像素图像作为8x8块重复到64x64像素图像中,请使用以下命令替换setRGB调用以将小图像重复到大图像中:

for (int x = 0 ; x < 64 ; x += 8)
    for (int y = 0 ; y < 64 ; y += 8)
        copiedNTimes.setRGB(x, y, 8, 8, smallPixels, 0, 8);
for(int x=0;x<64;x+=8)
对于(int y=0;y<64;y+=8)
setRGB(x,y,8,8,小像素,0,8);
或者将setRGB调用替换为以下内容,以首先构建更大的int[]并在一个步骤中应用它:

copiedNTimes.setRGB(0, 0, 64, 64, copied, 0, 64);

static int[] copyNTimes(int[] small, int[] big){
    for(int x = 0 ; x < big.length; x++){
        big[x] = small[8 * ((x / 64) % 8) + (x % 8)];
    }
    return big;
}
copiedNTimes.setRGB(0,0,64,64,copied,0,64);
静态int[]复制时间(int[]小,int[]大){
对于(int x=0;x
您是要复制图像还是放大图像?换言之,是要将每个像素复制64次,还是要将64个像素复制64次?我的目标是将小8x8图像相邻放置,每行8列8个图像。这是因为它最初不是照片图像,而是可扫描的代码光栅。发布的解决方案对我来说效果很好,谢谢你的评论!成功了!结果我不明白阵列是如何构建的。扫描线的大小最初是64,这当然无助于纠正我错误的复制方法。非常感谢,这比我想承认的更让我烦恼。