我想从文件夹中读取图像并将其旋转写回。 这是将图像旋转 90 度或 90 度的倍数(90, 180, 270) 的更快方式(而不是更简单的方式),并且只是在 Java 8 中?
我在互联网上搜索了很多时间来找到这个问题的答案......但一无所获。所以我想到了你们,也许你们中的一个人可以帮助我解决这个问题。我真的会认出那个人。
非常感谢。
最佳答案
正如 @sascha 所提到的:“您刚刚获得了受这些良好旋转候选者限制的像素索引的不同访问行为”。
因此,您可以使用图像处理框架来访问像素并以这种方式转换图像。
就我而言,我使用 Marvin Framework 。在我的笔记本上,500x298 图像的旋转过程需要 12 毫秒。
源代码:
public class RotateImages {
public RotateImages(){
MarvinImage image = MarvinImageIO.loadImage("./res/auto.jpg");
MarvinImageIO.saveImage(rotate90(image), "./res/auto_90.jpg");
MarvinImageIO.saveImage(rotate180(image), "./res/auto_180.jpg");
MarvinImageIO.saveImage(rotate270(image), "./res/auto_270.jpg");
}
private MarvinImage rotate90(MarvinImage image){
MarvinImage imageOut = new MarvinImage(image.getHeight(), image.getWidth());
for(int y=0; y<image.getHeight(); y++){
for(int x=0; x<image.getWidth(); x++){
int newX = y;
int newY = (image.getWidth()-1)-x;
imageOut.setIntColor(newX, newY, image.getIntColor(x, y));
}
}
return imageOut;
}
private MarvinImage rotate180(MarvinImage image){
MarvinImage imageOut = new MarvinImage(image.getWidth(), image.getHeight());
for(int y=0; y<image.getHeight(); y++){
for(int x=0; x<image.getWidth(); x++){
int newX = (image.getWidth()-1)-x;
int newY = (image.getHeight()-1)-y;
imageOut.setIntColor(newX, newY, image.getIntColor(x, y));
}
}
return imageOut;
}
private MarvinImage rotate270(MarvinImage image){
MarvinImage imageOut = new MarvinImage(image.getHeight(), image.getWidth());
for(int y=0; y<image.getHeight(); y++){
for(int x=0; x<image.getWidth(); x++){
int newX = (image.getHeight()-1)-y;
int newY = x;
imageOut.setIntColor(newX, newY, image.getIntColor(x, y));
}
}
return imageOut;
}
public static void main(String[] args) { new RotateImages(); }
}
关于java - 将图像旋转 90 度或倍数的更快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40579035/