想要从 Java 调用然后循环 Linux shell 命令
任务:我有一堆名为 .rgb 格式的图像,我想将它们转换为 .jpeg,
而不是手动转换它们(“convert -size img0001.rgb img0001.jpeg”)使用 imagemagick,我正在尝试编写一个为我做这件事的 java 程序。
我正在一台linux机器上工作。
问题:我的程序仅将第一个 .rgb 图像转换为 .jpeg,但完全忽略循环命令 p = Runtime.getRuntime().exec(cmd1);
。
public class RbgConvertor {
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(args [0]); // Number of pictures
String size = "1024x1024"; // Size image
String Path = "/home/nox/grbcode0000/"; // Image Directory
Process p; // buidiling a proces for "Runtime"
//I use a for loop as I have about an hundred of .rbg,
for (int i = 0; i <= n; i++) {
String[] cmd1 = {"bash",
"convert",
"-size", size,
Path,args[1],"0",""+(i+1),".rbg", //The "0" and (i+1) are to cope with the images being named img0001 etc.
Path,args[1],"0",""+(i+1),".jpeg"} ;
p = Runtime.getRuntime().exec(cmd1);
}// End If
}//End For
}//End Main
由于这是我听到的第一篇文章,我希望我能够足够清楚地解释我的问题...请随时留下有关“如何正确提出问题”的反馈!
Task: I have a bunch of images in a format called .rgb, and I want to convert them in .jpeg
Instead of converting them by hand (" convert -size img0001.rgb img0001.jpeg")using imagemagick, I am trying to write a java program that does it for me.
And I am working on a linux machine.
Problem: my program converts only the first .rgb image into a .jpeg, but completely ignores to loop the command p = Runtime.getRuntime().exec(cmd1);
.
public class RbgConvertor {
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(args [0]); // Number of pictures
String size = "1024x1024"; // Size image
String Path = "/home/nox/grbcode0000/"; // Image Directory
Process p; // buidiling a proces for "Runtime"
//I use a for loop as I have about an hundred of .rbg,
for (int i = 0; i <= n; i++) {
String[] cmd1 = {"bash",
"convert",
"-size", size,
Path,args[1],"0",""+(i+1),".rbg", //The "0" and (i+1) are to cope with the images being named img0001 etc.
Path,args[1],"0",""+(i+1),".jpeg"} ;
p = Runtime.getRuntime().exec(cmd1);
}// End If
}//End For
}//End Main
As this is my first post hear I hope I've been able to explain my problem clearly enough...feel free to leave feedback on "how to formulate questions correctly"!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这实际上是 bash 的工作:
This is actually a job for bash: