如何使用Java为一批文件使用ImageMagick实现.psd到.jpg的转换

如何使用Java为一批文件使用ImageMagick实现.psd到.jpg的转换,java,swing,imagemagick,image-conversion,Java,Swing,Imagemagick,Image Conversion,我需要在swing中查看.psd图像,所以我决定将其转换为.jpg并尝试显示它 My terminal command for single .psd to .jpg convert image.psd image.jpg (this is creating three images image-0.jpg,image-1.jpg,image-2.jpg) 但是如果我用 convert image.psd[0] image.jpg (this gives me correct outp

我需要在swing中查看.psd图像,所以我决定将其转换为.jpg并尝试显示它

 My terminal command for single .psd to .jpg

 convert image.psd image.jpg (this is creating three images image-0.jpg,image-1.jpg,image-2.jpg)
但是如果我用

 convert image.psd[0] image.jpg (this gives me correct output)
但是我需要使用java转换.psd图像。我试过了,但不起作用

if (new File(_imageicon[i].toString()).getName().endsWith(".psd") || 
        new File(_imageicon[i].toString()).getName().endsWith(".PSD")) {
    File psdFile = new File(_imageicon[i].toString());
    if(psdFile.exists()) {
        System.out.println("_imageicon[i].toString()"+_imageicon[i].toString());               
        System.out.println("_imageicon[i].toString().replace"+_imageicon[i].toString().replace(".psd", ".jpg").replace(".PSD", ".jpg"));
        ProcessBuilder pb = new ProcessBuilder("convert", _imageicon[i].toString() , _imageicon[i].toString().replace(".psd", ".jpg").replace(".PSD", ".jpg"));
        pb.redirectErrorStream(true);
        try {
            Process p = pb.start();
        } catch (IOException ex) {
            Logger.getLogger(GridEditor.class.getName()).log(Level.SEVERE, null, ex);
        }

        ImageIcon ii = new ImageIcon(_imageicon[i].toString().replace(".psd", ".jpg").replace(".PSD", ".jpg"));
        Image imagepdf1 = ii.getImage();
        Image newimg = imagepdf1.getScaledInstance(100, 60, java.awt.Image.SCALE_SMOOTH);
        ii = new ImageIcon(newimg);
        _lbl[i].setIcon(ii);
    }
}

任何想法请提出。

对于批量转换,请使用此选项

    ProcessBuilder pb = new ProcessBuilder("mogrify", "-format ", "jpg ", psdafterconvpath, psdpath + "\\" + "*.psd");
    pb.redirectErrorStream(true);
    try {
        Process p = pb.start();
    } catch (IOException ex) {
        Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

对于批处理转换,请使用以下命令

    ProcessBuilder pb = new ProcessBuilder("mogrify", "-format ", "jpg ", psdafterconvpath, psdpath + "\\" + "*.psd");
    pb.redirectErrorStream(true);
    try {
        Process p = pb.start();
    } catch (IOException ex) {
        Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

format和jpg后面有一个空格。还要添加[0]以获取单个图像

 ProcessBuilder pb = new ProcessBuilder("mogrify", "-format", "jpg", psdafterconvpath, psdpath + "\\" + "*.psd[0]");
pb.redirectErrorStream(true);
try {
    Process p = pb.start();
} catch (IOException ex) {
    Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
}
}

这对我有用。检查并确认。

格式和jpg后面有空格。还要添加[0]以获取单个图像

 ProcessBuilder pb = new ProcessBuilder("mogrify", "-format", "jpg", psdafterconvpath, psdpath + "\\" + "*.psd[0]");
pb.redirectErrorStream(true);
try {
    Process p = pb.start();
} catch (IOException ex) {
    Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
}
}

这对我有用。检查并确认。

代码在哪里使用错误和输出流?另请参见。代码在哪里使用错误和输出流?另请参见。此代码不会引发错误,但不会转换,但可以从命令行运行,如“mogrify-format jpg psdafterconvpath psdpath/*psd”。此代码不会引发错误,但不会转换,但可以从命令行运行,如“mogrify-format jpg psdafterconvpath psdpath/*psd”