Want to invoke and then loop over a linux shell command from Java
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 p开发者_如何学Goost hear I hope I've been able to explain my problem clearly enough...feel free to leave feedback on "how to formulate questions correctly"!
This is actually a job for bash:
#!/bin/bash
for x in img????.rgb; do
convert -size 1024x1024 "$x" "${x%.rgb}.jpg"
done
精彩评论