【发布时间】:2015-08-10 04:02:36
【问题描述】:
我一直在编写一个 Java 程序,它读取图像,将其细分为可定义数量的矩形图块,然后将每个图块的像素与另一个图块的像素交换,然后将它们重新组合在一起并渲染图像.
想法的解释:http://i.imgur.com/OPefpjf.png
我一直在使用BufferedImage 类,所以我的想法是首先从其数据缓冲区中读取所有width * height 像素并将它们保存到一个数组中。
然后,根据瓦片的高度和宽度,将每个瓦片的整个像素信息复制到小数组中,打乱它们,然后将这些数组中包含的数据写回它们在数据缓冲区中的位置。然后用原始颜色和样本模型以及更新的数据缓冲区创建一个新的BufferedImage 就足够了。
但是,当我从更新的数据缓冲区创建一个新的WriteableRaster 时,我遇到了不祥的错误,并且像素数不匹配(我突然得到了 24,而不是原来的 8,等等),所以我认为我处理像素信息的方式有问题。
(BufferedImage 和 WriteableRaster 的参考页面)
我使用以下循环遍历一维数据缓冲区:
// maximum iteration values
int numRows = height/tileHeight;
int numCols = width/tileWidth;
// cut picture into tiles
// for each column of the image matrix
// addressing columns (1D)
for ( int column = 0; column < numCols; column++ )
{
// for each row of the matrix
// addressing cells (2D)
for ( int row = 0; row < numRows; row++ )
{
byte[] pixels = new byte[(tileWidth+1) * (tileHeight+1)];
int celloffset = (column + (width * row)); // find cell base address
// for each row inside the cell
// adressing column inside a tile (3D)
for ( int colpixel = 0; colpixel < tileWidth; colpixel++ )
{
// for each column inside the tile -> each pixel of the cell
for ( int rowpixel = 0; rowpixel < tileHeight; rowpixel++ )
{
// address of pixel in original image buffer array allPixels[]
int origpos = celloffset + ((rowpixel * tileWidth) + colpixel);
// translated address of pixel in local pixels[] array of current tile
int transpos = colpixel + (rowpixel * tileWidth);
// source, start, dest, offset, length
pixels[transpos] = allPixels[origpos];
}
}
}
}
这段代码有问题吗?或者是否有一种我还没有想到的更简单的方法来做到这一点?
【问题讨论】:
-
我建议发布一个完整的程序,人们可以尝试运行,我相信它会增加您获得有用帮助的可能性。
-
创建子图像并绘制到新图像不是您的选择吗?
标签: java arrays image-processing bufferedimage