dcsimg
Problem calling convert and mogrify from Java on Linux
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Stephen_Pasco
Posted On:   Tuesday, April 20, 2004 07:35 PM

Hi all, I'm a little stumped. I hope you can help. I've just migrated a Java program that does image conversion using Mogrify and Convert from Windows to Linux (Slackware 9.1) and I've come across a problem I'm not sure how to solve. I'm not that experienced with Linux but I figured someone here probably has a quick fix to this problem or can steer me in the right direction. My Java program, see below, calls the mogrify and convert commands based upon some input. Upon running my program in Linux the image file I target to mogrify or convert does not get mogrified or converted. It appears from the output that mogrify and convert are being called but they don't actually do anything. When I run the same command the J   More>>

Hi all,

I'm a little stumped. I hope you can help.

I've just migrated a Java program that does image conversion using Mogrify and Convert from Windows to Linux (Slackware 9.1) and I've come across a problem I'm not sure how to solve. I'm not that experienced with Linux but I figured someone here probably has a quick fix to this problem or can steer me in the right direction.

My Java program, see below, calls the mogrify and convert commands based upon some input. Upon running my program in Linux the image file I target to mogrify or convert does not get mogrified or converted. It appears from the output that mogrify and convert are being called but they don't actually do anything.

When I run the same command the Java program calls from the Linux command prompt, mogrify and convert work as expected.

What am I doing incorrectly when calling mogrify or convert from Java within Linux?

Here's the system output created upon running the Java program. The "ERROR>" and "OUTPUT>" lines are created by Runtime process error and input streams collected upon calling mogrify and convert.

System out From When Calling Mogrify from Java

spasco@spasco:~/webapps/ROOT/WEB-INF/classes$ java -cp . com.test.ImageMagickTest
resize cmd = mogrify -verbose -debug -type Optimize -resize '350x260>' -quality 75 +profile '*' test.JPG
ERROR>test.JPG=>test.mgk MGK 1024x768 DirectClass 8-bit 101kb 0.1u 0:01
OUTPUT>test.JPG JPEG 1024x768 DirectClass 8-bit 101kb 0.1u 0:01
ExitValue: 0

Java Program
Code:

package com.test;

import com.openforrent.util.FileUtility;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

public class ImageMagickTest {

public static void main(String[] args) {
resize(".","test.JPG","resize");
}

public static void resize(String dir, String fileName, String resizeType) {

Runtime rt = null;
Process proc = null;
StreamGobbler errorGobbler = null;
StreamGobbler outputGobbler = null;
String cmd = null;

try {

int dotIndex = fileName.indexOf('.');
String newFileName = fileName.substring(0, dotIndex);

if(resizeType.equalsIgnoreCase("thumb110")) {
newFileName = newFileName + "_110.jpg";
cmd = "convert -verbose -debug -type Optimize -resize \'110x110>\' -quality 75 +profile \'*\' " + dir + "/" + fileName + " " + dir + "/" + newFileName;
System.out.println("thumb110 cmd = " + cmd);
}
else if(resizeType.equalsIgnoreCase("thumb200")) {
newFileName = newFileName + "_200.jpg";
cmd = "convert -verbose -debug -type Optimize -resize \'200x200>\' -quality 75 +profile \'*\' " + dir + "/" + fileName + " " + dir + "/" + newFileName;
System.out.println("thumb200 cmd = " + cmd);
}
else if(resizeType.equalsIgnoreCase("thumb350")) {
newFileName = newFileName + "_350.jpg";
cmd = "convert -verbose -debug -type Optimize -resize \'350x260>\' -quality 75 +profile \'*\' " + dir + "/" + fileName + " " + dir + "/" + newFileName;
System.out.println("_350 cmd = " + cmd);
}
else if(resizeType.equalsIgnoreCase("resize")) {
cmd = "mogrify -verbose -debug -type Optimize -resize \'350x260>\' -quality 75 +profile \'*\' test.JPG";
System.out.println("resize cmd = " + cmd);
}

rt = Runtime.getRuntime();
proc = rt.exec(cmd);

// any error message?
errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");

// any output?
outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");

// kick them off
errorGobbler.start();
outputGobbler.start();

// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);

}
catch (IOException e) {
e.printStackTrace();
System.out.println("e.getMessage() = " + e.getMessage());
}
catch (InterruptedException e) {
e.printStackTrace();
System.out.println("e.getMessage() = " + e.getMessage());
}
finally {
if(rt != null) {
rt = null;
}
if(proc != null) {
proc = null;
}
if(errorGobbler != null) {
errorGobbler = null;
}
if(outputGobbler != null) {
outputGobbler = null;
}
}
}

public static String createDuplicateImage(String useDir, String fileName) {

FileUtility.copyImageFile(useDir, fileName);

return fileName + "_thumb";

}
}

class StreamGobbler extends Thread {
InputStream is;
String type;

StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}

public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
System.out.println(type + ">" + line);
}
catch (IOException ioe) {
System.out.println("ioe.getMessage() = " + ioe.getMessage());
ioe.printStackTrace();
}
}
}

   <<Less

Re: Problem calling convert and mogrify from Java on Linux

Posted By:   Simon_Ablett  
Posted On:   Wednesday, April 21, 2004 03:29 AM

Try reposting with your code inside
 and 
tags to make it easier to read.

Regards.
About | Sitemap | Contact