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

时间:2014-07-18 04:33:52

标签: 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 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);
    }
}

任何想法请建议。

1 个答案:

答案 0 :(得分:3)

对于批量转换,请使用此

    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);
    }
    }