Hi,
I am finding the best and fastest way of converting a GIF Image to JPEG, with the restriction that I am using JDK1.3
I have tried using JAI but at times the images returned are broken. And Black and White images cannot be converted at all. Can someone please please help fix this problem.
Here is the code that doesnt function well.
Also, when this code is loaded on a HP-UX box, it throws a register error stating something is already registered.
****************
import java.io.ByteArrayOutputStr
eam;
import java.io.IOException;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp
;
import org.apache.log4j.Logger;
import com.sun.media.jai.codec.By
teArraySee
kableStrea
m;
import com.sun.media.jai.codec.Im
ageCodec;
import com.sun.media.jai.codec.Im
ageEncoder
;
import com.sun.media.jai.codec.JP
EGEncodePa
ram;
import com.tmobile.wfe.idc.web.ca
che.CacheI
tem;
import com.tmobile.wfe.idc.web.ca
che.HTTPCa
che;
/**
* Cache which automatically converts source images into JPEGs.
* Uses JAI classes to perform the conversion (so needs jai_core.jar and jai_codec.jar).
*/
public class ImageTranscoderCache extends HTTPCache {
/** Log4j logger. */
private static Logger log = Logger.getLogger(ImageTran
scoderCach
e.class);
/** Mime-type for GIF images */
public static final String GIF_MIME_TYPE = "image/gif";
/** Mime-type for JPEG images */
public static final String JPEG_MIME_TYPE = "image/jpeg";
/** Quality setting for JPEG compression. */
private float jpegQuality;
public ImageTranscoderCache(int expiryMinutes, int maxSize,
String username, String password, float jpegQuality)
{
super(expiryMinutes, maxSize, username, password);
this.jpegQuality = jpegQuality;
}
/**
* Converts GIF images into JPEGs, leaving other types of media unchanged.
* Uses the HTTPCache superclass's createCacheItem method to actually fetch the
* contents of the target URL (as a byte[] array), which both simplifies things here,
* and ensures that any enhancements to that method get used here.
* TODO Monochrome GIFs don't seem to get work, so left alone. Fix!
*/
protected CacheItem createCacheItem(String itemId) throws IOException {
// Get the source image data
CacheItem sourceCacheItem = super.createCacheItem(item
Id);
// Return the cached item unchanged if it isn't a GIF (doesn't start with GIF mimetype).
if (sourceCacheItem.getMime_t
ype().inde
xOf(GIF_MI
ME_TYPE) != 0) {
log.debug("The MIME Type is: " + sourceCacheItem.getMime_ty
pe());
log.debug("Not a GIF: " + itemId);
return sourceCacheItem;
}
// Read the source image as a JAI object
final byte[] sourceData = sourceCacheItem.getData();
if (sourceData == null || sourceData.length == 0) {
throw new IOException("No image data read for: " + itemId);
}
RenderedOp sourceImage = JAI.create("stream", new ByteArraySeekableStream(so
urceData))
;
// If monochrome then leave unchanged (since JAI then seems to generate a JPEG that
// browsers can't understand).
// TODO Fix this! Try changing the colour rep?
if (sourceImage.getColorModel
().getPixe
lSize() == 1) {
log.debug("Monochrome, so leaving unchanged");
return sourceCacheItem;
}
// Then convert it to a JPEG
JPEGEncodeParam encodeParam = new JPEGEncodeParam();
encodeParam.setQuality(jpe
gQuality);
// TODO change this to 1.0F high quality!
// Write it to a byte array
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
ImageEncoder encoder = ImageCodec.createImageEnco
der("JPEG"
, outStream, encodeParam);
encoder.encode(sourceImage
);
outStream.close(); // close actually does nothing, so don't need finally
// And return the converted image
final byte[] jpegData = outStream.toByteArray();
log.debug("Converted to JPEG. Was " + sourceData.length + " bytes, now "
+ jpegData.length);
return createCacheItem(itemId, jpegData, JPEG_MIME_TYPE);
}
}