Trolltech Home | Qt-jambi-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-jambi-interest Archive, December 2007
QPixmap or QImage from (Java-) InputStream


Message 1 in thread

Hello

is there any chance to read a QPixmap (or a QImage) from a Java - 
InputStream (i.E. a ZipFileInputStream). I tried to copy every Byte of 
the Input Stream in a QByteArray, then build a QBuffer from the 
ByteArray an pass this to QImageReader, but that doesn't seem to work, 
the QPixmap was empty.(and QImageReader.canRead() returns false)

I would like to create a QPixmap directly from a zipfile (in the way I 
create it from a existing file = new QPixmap("file.jpg") without copy it 
to the lokal filesystem.

thank you
Arne


Message 2 in thread

Arne Stocker wrote:
> Hello
> 
> is there any chance to read a QPixmap (or a QImage) from a Java - 
> InputStream (i.E. a ZipFileInputStream). I tried to copy every Byte of 
> the Input Stream in a QByteArray, then build a QBuffer from the 
> ByteArray an pass this to QImageReader, but that doesn't seem to work, 
> the QPixmap was empty.(and QImageReader.canRead() returns false)
> 
> I would like to create a QPixmap directly from a zipfile (in the way I 
> create it from a existing file = new QPixmap("file.jpg") without copy it 
> to the lokal filesystem.

Hi Arne,

I don't know why your approach wouldn't work. Maybe you didn't have the 
jpeg plugin in the QT_PLUGIN_PATH? Anyway, below is an example of how 
you can use QPixmap.loadFromData() to achieve this quickly.

import com.trolltech.qt.core.*;
import com.trolltech.qt.gui.*;

public class ImageFromBytes
{
     public static void main(String args[]) {
         QApplication.initialize(args);

         QFile f = new QFile("image.png");
         if (!f.open(QFile.OpenModeFlag.ReadOnly)) {
             System.out.println("failed to open image.png...");
             return;
         }

         QByteArray data = f.readAll();

         QPixmap pixmap = new QPixmap();
         if (!pixmap.loadFromData(data, "PNG")) {
             System.out.println("failed to load image data...");
             return;
         }

         QLabel label = new QLabel();
         label.setPixmap(pixmap);
         label.show();

         QApplication.exec();
     }
}

Another way of doing this would be to use our resource system. Say that 
you have 50 icons in your app, like close.png, fileopen.png, etc. If you 
put these into a .jar file and put the .jar file into the CLASSPATH you 
would be able to do:

QPixmap closeIcon = new QPixmap("classpath:path_in_jar_to/close.png");

and it would just load it automatically.

best regards,
Gunnar


Message 3 in thread

> I don't know why your approach wouldn't work. Maybe you didn't have the jpeg plugin in the QT_PLUGIN_PATH? Anyway, b

Hello Gunnar

sorry it was my fault, but there is no problem to load a QPixmap from an 
existing file (that works well). The problem is that I have a zipped 
file wich includes several pictures (i.e. jpg's).

In Java I could open an InputStream for everey file in the zip-archiv 
withoud extract the whole zipfile to the disk. In return I get an (Java) 
InputStream from wich I could read the data (as I do with the included 
metainfo file in xml-format).

Now I want to create a QPixmap direktly from that (Java) InputStream 
(the Object is from the type ZipFileInputStream). So all I need is to 
read the Bytes from the InputStream to the QByteArray.

I did it byte for byte (which needs a lot of time), then create an Image 
in that way

int read = 1;
QByteArray bytearray = new QByteArray();
while (read >= 0) {
     read = inputstream.read(); // read() returns 0-255 or -1
     if (read >= 0) {
         bytearray.add((byte) inputstream);
     }
}

QBuffer buffer = new QBuffer(bytearray);
QImageReader ireader = new QImageReader(buffer,new QByteArray("jpg"));
if (imagereader.canRead() {
    return imagereader.read();
}

this way failed.


I will try it with the QPixmap.LoadFromData() again.

By the way : is there a better (faster) way to read a (Java) InputStream 
in a QByteArray except copy it byte for byte ? It would be nice if 
QByteArray has an Interface to the (Java) InputStream.


best regards Arne