Here is a simple javaclass that can be used to "talk" with ImageMagick
package com.cluster9.pdfgenerator;
import com.cluster9.foundation.*;
import java.io.*;
public class ImageMagickWrapper {
private ImageMagickWrapper() {
super();
}
public static String imageType(byte[] image) throws IOException {
//an PDF file must start with: "%PDF"
InputStream fis = null;
String imageType = null;
String fileSignature = null;
byte b[] = new byte[4];
fis = new ByteArrayInputStream(image);
int length = fis.available();
if (length >= 4) {
fis.read(b, 0, 4);
fileSignature = new String( b );
if (fileSignature.toLowerCase().indexOf("pdf")!=-1) {
return "pdf";
}
}
fis.close();
fis = null;
String result = null;
String command = TYPE_PATH_TO_IMAGEMAGICK_IDENTIFY+" -";
result = new String(process(new ByteArrayInputStream(image), command));
if (result.toLowerCase().indexOf("gif")!=-1) {
//its an gif
imageType = "gif";
} else if (result.toLowerCase().indexOf("jpg")!= -1 ||
result.toLowerCase().indexOf("jpeg") != -1) {
imageType = "jpeg";
} else if (result.toLowerCase().indexOf("png")!= -1 ) {
imageType = "png";
} /*else if (result.toLowerCase().indexOf("tif")!= -1) {
imageType = "tiff";
} */else {
imageType = "unsupported image";
}
return imageType;
}
public static byte[] convertImage(byte[] ori, String filetype) throws IOException
{
String command = TYPE_PATH_TO_IMAGEMAGICK_CONVERT + " - " +
filetype.toUpperCase() + ":-";
byte[] result = process(new ByteArrayInputStream(ori), command);
return result;
}
private static byte[] process(InputStream in, String command) throws IOException {
Process p = Runtime.getRuntime().exec(command);
InputStream pin = p.getInputStream();
InputStream perr = p.getErrorStream();
OutputStream pout = p.getOutputStream();
ProcessReader processReader = new ProcessReader(pin);
processReader.start();
ProcessReader errorReader = new ProcessReader(perr);
errorReader.start();
byte[] buf = new byte[0x10000];
int count = 0;
while((count = in.read(buf)) != -1) {
pout.write(buf, 0, count);
}
pout.close();
String errorMessage = new String(errorReader.getResult());
if (errorMessage != null && errorMessage.length() > 0) {
throw new IOException(errorMessage);
}
return processReader.getResult();
}
static class ProcessReader extends Thread {
private InputStream in;
private byte[] result;
ProcessReader(InputStream in) {
this.in = in;
}
synchronized byte[] getResult() {
while(isAlive()) {
try {
wait();
} catch(InterruptedException ex) {
System.out.println("Exception: " + ex);
}
}
return result;
}
public void run() {
int avail = 0;
byte[] buf = new byte[0x10000];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while(true) {
try {
int count = in.read(buf);
if(count == -1) {
baos.close();
result = baos.toByteArray();
baos = null;
return;
}
baos.write(buf, 0, count);
} catch(IOException ex) {
System.out.println("Exception: " + ex);
}
}
}
}
}
On Friday, June 14, 2002, at 12:15 AM, morpheus Smith wrote:
> That's what exactly i'm looking for.(actullay was
> planning to write).
> If you could send that , that would be a great help
> for me.
> thanks a lot.
> i appreciate your prompt reply.
>
> thanks
> morpheus
>
>
> --- David Teran <[EMAIL PROTECTED]> wrote:
>> yes, try imagemagick. If someone is interested, we
>> have written a wrapper that makes runtime calls and
>> converts images on the fly.
>>
>> regards david
>>
>> On Thursday, June 13, 2002, at 11:41 PM, morpheus
>> Smith wrote:
>>
>>> Gif images created from photoshop, gimp and
>>> photoeditor are not directly supported by iText
>> b'cos
>>> of gif standards. is there free tool available by
>>> which i can create gifs supported by iText.
>>>
>>> Thanks in advance.
>>> -- morphues
>>>
>>> __________________________________________________
>>> Do You Yahoo!?
>>> Yahoo! - Official partner of 2002 FIFA World Cup
>>> http://fifaworldcup.yahoo.com
>>>
>>>
>>
> _______________________________________________________________
>>>
>>> Don't miss the 2002 Sprint PCS Application
>> Developer's Conference
>>> August 25-28 in Las Vegas -
>>
> http://devcon.sprintpcs.com/adp/index.cfm?source=osdntextlink
>>>
>>> _______________________________________________
>>> iText-questions mailing list
>>> [EMAIL PROTECTED]
>>>
>>
> https://lists.sourceforge.net/lists/listinfo/itext-questions
>>>
>>
>>
>>
> _______________________________________________________________
>>
>> Don't miss the 2002 Sprint PCS Application
>> Developer's Conference
>> August 25-28 in Las Vegas -
>>
> http://devcon.sprintpcs.com/adp/index.cfm?source=osdntextlink
>>
>> _______________________________________________
>> iText-questions mailing list
>> [EMAIL PROTECTED]
>>
> https://lists.sourceforge.net/lists/listinfo/itext-questions
>
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! - Official partner of 2002 FIFA World Cup
> http://fifaworldcup.yahoo.com
>
_______________________________________________________________
Don't miss the 2002 Sprint PCS Application Developer's Conference
August 25-28 in Las Vegas -
http://devcon.sprintpcs.com/adp/index.cfm?source=osdntextlink
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions