Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Gif to JPEG Conversion with Java1.3

Posted on 2004-11-11
27
Medium Priority
?
349 Views
Last Modified: 2013-11-19
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.ByteArrayOutputStream;
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.ByteArraySeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.JPEGEncodeParam;
import com.tmobile.wfe.idc.web.cache.CacheItem;
import com.tmobile.wfe.idc.web.cache.HTTPCache;

/**
 * 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(ImageTranscoderCache.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(itemId);

            // Return the cached item unchanged if it isn't a GIF (doesn't start with GIF mimetype).
            if (sourceCacheItem.getMime_type().indexOf(GIF_MIME_TYPE) != 0) {
                  log.debug("The MIME Type is: " + sourceCacheItem.getMime_type());
                  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(sourceData));

            // 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().getPixelSize() == 1) {
                  log.debug("Monochrome, so leaving unchanged");
                  return sourceCacheItem;
            }
            
            // Then convert it to a JPEG
        JPEGEncodeParam encodeParam = new JPEGEncodeParam();
        encodeParam.setQuality(jpegQuality);  // TODO change this to 1.0F high quality!
       
        // Write it to a byte array
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        ImageEncoder encoder = ImageCodec.createImageEncoder("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);
      }
}
0
Comment
Question by:Wilkhu
  • 11
  • 7
  • 6
24 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 2000 total points
ID: 12553196
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12553198
Hi, I gave up to do all those conversions myself,
it's possible to call a good tool from JAVA.
F.i. irfan@linux.tuwien.ac.at  is a good freeware tool from anything to anything and commandline-enabled,
so you can call it from JAVA.
<*>
0
 

Author Comment

by:Wilkhu
ID: 12553243
I am calling the conversion from a servlet and caching the result hence I cannot use commandline.

Are there any other external jars etc that have been developed that I can use to perform this conversion under Java1.3?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Wilkhu
ID: 12553260
I have the following environment restrictions:
HP-UX running jdk1.3 hence, I need some freeware that I can use in my application to convert the images.

If there are commercially available jars that you may be aware of, I would also look into this.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12553270
Even a servlet can practise             Runtime.exec("..........");
<*>
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12553275
Sorry, mine was an MSWindows utility.
<*>
0
 

Author Comment

by:Wilkhu
ID: 12553286
Does anyone know of any commercial API's available to go image conversion using Java1.3? (Apart from the ones that dont function well like the JAI etc)
0
 
LVL 35

Expert Comment

by:girionis
ID: 12553299
0
 

Author Comment

by:Wilkhu
ID: 12553329
Yes, I have seen this list but is there one that has been recommended or used successfully for these converstions?
What is highly used by people to solve this issue? (Apart from upgrading to jdk1.4)
0
 
LVL 35

Expert Comment

by:girionis
ID: 12553359
Not sure what else you could do. The links I posted are the only solutions I know (apart from upgrading).
0
 

Author Comment

by:Wilkhu
ID: 12553370
Do you know which one works though?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12553382
I guess geocities is the best choice in your situation.
There is not very much left.
Even google doesn't list anything else.
<*>
0
 

Author Comment

by:Wilkhu
ID: 12553421
OK, Here is another final question related to this. If I am running a server with Java 1.3 but also have Java 1.4 installed on the same server, is it possible to have one web application running on the weblogic instance(instance running jdk1.3) to use jdk1.4 which is available on the server.
0
 
LVL 35

Expert Comment

by:girionis
ID: 12553424
No.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12553447
It should be possible to launch a JAVA program that runs on another JAVA release
on the same machine, if only you can keep the environments separated
and if you can provide the right path settings to it.
Note: it's not possible to CALL such a program from another JVM.
<*>
0
 

Author Comment

by:Wilkhu
ID: 12553459
Yeap, I thought so. The weblogic instance is running the 1.3 JVM hence it would not be possible for the application to call 1.4 libraries.

Still not sure which library to use. Have read the geocities site but cannot seem to find the best one that does the job. Any ideas?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12553478
Per definition:
to call a JAVA program from a JAVA program is:      class.method(....);
to launch ditto is:                                                  Runtime.exec(<path to the other java>, <path to that program>);
<*>
0
 

Author Comment

by:Wilkhu
ID: 12553546
sciuriware,
You sent the following email address: F.i. irfan@linux.tuwien.ac.at  

How to I get hold on more information about this tool?


0
 

Author Comment

by:Wilkhu
ID: 12553562
Trying to look for this freeware tool you mentioned but cannot seem to get to it. Can you please provide the direct link or information.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12553597
Hey! I never noticed that!!!!
If he developed it on LINUX it will surely run on UNIXes.
I do use GIMP on LINUX but that's not directly(!) availavle on HPUX.
The credits list:
                http://members.chello.at/irfan.skiljan/   :: home address in Austria,
                http://www.ryansimmons.com/users/irfanview/  :: USA contacts,
                http://www.irfanview.com/  :: for Luxembourg.
I used this version 3.36 over 2 years now, so, maybe things have changed a lot.
<*>
0
 

Author Comment

by:Wilkhu
ID: 12553646
The software seems to be supported only on the following platforms:
Windows 9x/ME/NT/2000/XP/2003.

So wont be of much use for me using HP-UX
0
 

Author Comment

by:Wilkhu
ID: 12553726
Still looking for some support for a solution.....
Anyone can help?
0
 
LVL 35

Expert Comment

by:girionis
ID: 12553796
Try the link in my first post, it should do the trick.
0
 
LVL 35

Expert Comment

by:girionis
ID: 12800850
I think that my fisrt comment answers the question.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Not only does Prezi allow you to create non-linear presentations, it also makes it easy to apply transition animations between your frames. Learn how to apply a fade-in transition to select items and entire frames in this tutorial Select "Edit Path"…
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question