我将根据 Java 中的用户定义函数来扭曲图像。一般来说,图像比较大(JPEG,30-50 MB)。
最初,图像被加载:
BufferedImage img = ImageIO.read("image.jpg");
假设 [X,Y] 为图像的重采样像素坐标,其中 [x,y] 表示其像素坐标。
坐标函数(一个简单的例子)写成如下:
X = y * cos(x);
Y = x;
我的想法是使用逐像素转换:
//Get size of the raster
int width = img.getWidth(), height = img.getHeight();
int proj_width = (int)(width * Math.cos(height*Math.pi/180)),proj_height = height;
//Create output image
BufferedImage img2 = new BufferedImage(proj_width+1, proj_height+1, img.getType());
//Reproject raster
for (int i = 0; i < img.getWidth(); i++) {
for (int j = 0; j < img.getHeight(); j++) {
//Color of the pixel
int col = img.getRGB(i, j);
//Its new coordinates
int X = (int)(i * Math.cos(j*Math.pi/180));
int Y = j;
//Set X,Y,col to the new raster
img2.setRGB(X,Y,col);
}
}
有没有更快的方法来实现这个操作而不需要任何额外的库?
例如使用 Warp 类中的 warpRect() 方法...
谢谢你的帮助。