Solved

Decompress and display jpeg image from memory (not file!)

Posted on 1997-09-29
8
873 Views
Last Modified: 2008-03-17
I know I can read and display jpeg images directly from a jpeg file, but I've got a jpeg image (compressed) already in memory (via native code out of a QuickTime movie).
Can I utilize Java methods to decompress (and then display) this jpeg image directly from memory?
0
Comment
Question by:riemer
  • 4
  • 4
8 Comments
 
LVL 3

Expert Comment

by:gwalters
ID: 1227639
I know this sounds inefficient, but given the complexity of decompressing JPEG (and the fact that Toolkit.getImage triggers native code to do the actual decoding), it may be more efficient than decompressing in java (which would be the other, more difficult way to do it):

Save the memory buffer to a temporary file.
Load the temporary file with Toolkit.getImage()


BTW, if you wanted GIF, I could give you a class that does it (and also supports GIF animations!)


0
 

Author Comment

by:riemer
ID: 1227640
Thanks for the instant answer BUT:
Sorry, that's not what I want. I'm keeping compressed jpeg images in memory to have a sort of cache to
a) speed up image re-display e.g. from a slower net or cdrom I/O and
b) keep it compressed to save wasting memory like a raw image would do.

Writing it to a local disk and reading it back is still costing too much time for me.
0
 
LVL 3

Expert Comment

by:gwalters
ID: 1227641
Well, it will HAVE to be uncompressed in order to be displayed!


0
 

Author Comment

by:riemer
ID: 1227642
I DO agree. Currently I'm using a native jpeg library which is probably less efficient **) (I hope) than a genuine Java solution doing it all with its Java jpeg library I can see e.g. in win95 "jpeg.dll".

**) less efficient is: I/O + decompress (60 % of the time) + display (40 %) (100 % = 300 ms for a 256x256 image on a Pentium 120 MHz) - I'm hoping to reduce the 60 % portion.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Accepted Solution

by:
gwalters earned 100 total points
ID: 1227643
OK.  Here's another answer:

------------------BEGIN jpeg.java---------------------------
import sun.awt.image.*;
import java.io.*;

public class jpeg extends InputStreamImageSource {
  InputStream in;
  public jpeg(byte[] data) {
    in=new ByteArrayInputStream(data);    
  }

  public boolean checkSecurity(Object obj, boolean flag) {
    return true;
  }

  protected ImageDecoder getDecoder() {
    return new JPEGImageDecoder(this,in);
  }
}
--------------------END jpeg.java-----------------------------

Now, let's say you have a jpeg in a byte[] foo.  Just do this:

Image img=Toolkit.getDefaultToolkit().createImage(new jpeg(foo));



Beware of using the sun.* classes, though.  Unlike the java.* classes, they are not supported, documented, or even guaranteed to be there!


0
 

Author Comment

by:riemer
ID: 1227644
I'm tempted to grade your answer as "Good" - if there really is no 100% pure Java solution ... isn't there any, how sure are you?
(I'm hesitating to put something in which is not 100% pure Java)
0
 
LVL 3

Expert Comment

by:gwalters
ID: 1227645
No, I'm sure there is a 100% pure java solution.

1) I don't have JPEG code (like I said, I already wrote a class for GIF, 100% java).
2) It would probably be slow.

You may want to try it, though.  See if you can find JPEG code in C, then port it to Java.  I can give you my GIF code as a guide on how images work in Java.


0
 

Author Comment

by:riemer
ID: 1227646
Thanks for your last comment. I DO need speed. I've been hoping for a 100% pure STANDARD Java solution - possibly internally in native code like (I suppose) some of the display functions are to speed it up (e.g. using the win95 jpeg.dll). I think I'll have to go back to my own native code and port it from platform to platform (written in C and - important for me - quite FAST)

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sites similar to codingbat to improve coding hanson skills 3 41
what is a "java.lang.System Property"   ? 20 57
print map entry 34 55
tomcat administrtor 12 29
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

896 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now