想要从 Java 调用然后循环 Linux shell 命令

发布于 2024-11-29 10:32:52 字数 1239 浏览 1 评论 0原文

任务:我有一堆名为 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我乃一代侩神 2024-12-06 10:32:52

这实际上是 bash 的工作:

#!/bin/bash
for x in img????.rgb; do
    convert -size 1024x1024 "$x" "${x%.rgb}.jpg"
done

This is actually a job for bash:

#!/bin/bash
for x in img????.rgb; do
    convert -size 1024x1024 "$x" "${x%.rgb}.jpg"
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文