Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Problem saving and retrieving ImageData from Database
Problem saving and retrieving ImageData from Database [message #465450] Wed, 14 December 2005 04:10 Go to next message
Eclipse UserFriend
Originally posted by: brianj.NoSpam.sbcglobal.net

Hi, All,

I am attempting to store an ImageData object in a BLOB field stored in a
Derby database. It looks like the ImageData object is storing fine, but
when I try to load it, I get:

Exception in thread "main"
org.eclipse.swt.SWTException: Unsupported or unrecognized format

Relevant code snippets:

public static void SaveToDB(Connection conn, String name,
ImageData imgData, String strings) {
try {
PreparedStatement stmt = conn.prepareStatement("INSERT IGNORE INTO TESTTABLE (NAME, NOTES, PICTURE) VALUES (?,?,?)");
stmt.setString(1, name);

InputStream inputStream = new ByteArrayInputStream(strings.getBytes());
stmt.setAsciiStream(2, inputStream, strings.length());

InputStream bis = new ByteArrayInputStream(imgData.data);
stmt.setBinaryStream(3, bis, imgData.data.length);

stmt.execute();
}
catch (SQLException e) {
e.printStackTrace();
}
}

public static void LoadFromDB(Connection conn, String name, ImageData imgData, String strings) {
try {
PreparedStatement stmt = conn.prepareStatement("SELECT NAME, NOTES, PICTURE FROM TESTTABLE WHERE NAME = ?");
stmt.setString(1, name);

if (stmt.execute()) {
ResultSet rs = stmt.getResultSet();
rs.next();

Clob clob = rs.getClob(2);
strings = clob.getSubString((long)1, (int)clob.length());
System.out.println(strings);

Blob blob = rs.getBlob(3);
Exception here ---> imgData = new ImageData(blob.getBinaryStream());
}
}
catch (SQLException e) {
e.printStackTrace();
}
}


So, am I overlooking something easy, or am I just barking up the wrong
tree completely?

Thanks for any assistance,

Brian
Re: Problem saving and retrieving ImageData from Database [message #465465 is a reply to message #465450] Wed, 14 December 2005 14:55 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
There is more to ImageData than just the bytes in the data field - there is
information about transparency, scanLinePad, width, height, ...

In the constructor ImageData(InputStream stream) the input stream is
expected to be the full description of an image e.g.
myObject.class.getResourceAsStream ("C:\\abc.bmp").

Have a look at ImageLoader.save(OutputStream stream, int format) and
ImageLoader.load(InputStream stream).

"Brian L. Juergensmeyer" <brianj@NoSpam.sbcglobal.net> wrote in message
news:pan.2005.12.14.04.09.56.175323@NoSpam.sbcglobal.net...
> Hi, All,
>
> I am attempting to store an ImageData object in a BLOB field stored in a
> Derby database. It looks like the ImageData object is storing fine, but
> when I try to load it, I get:
>
> Exception in thread "main"
> org.eclipse.swt.SWTException: Unsupported or unrecognized format
>
> Relevant code snippets:
>
> public static void SaveToDB(Connection conn, String name,
> ImageData imgData, String strings) {
> try {
> PreparedStatement stmt = conn.prepareStatement("INSERT IGNORE INTO TESTTABLE
> (NAME, NOTES, PICTURE) VALUES (?,?,?)");
> stmt.setString(1, name);
>
> InputStream inputStream = new ByteArrayInputStream(strings.getBytes());
> stmt.setAsciiStream(2, inputStream, strings.length());
>
> InputStream bis = new ByteArrayInputStream(imgData.data);
> stmt.setBinaryStream(3, bis, imgData.data.length);
>
> stmt.execute();
> }
> catch (SQLException e) {
> e.printStackTrace();
> }
> }
>
> public static void LoadFromDB(Connection conn, String name, ImageData
> imgData, String strings) {
> try {
> PreparedStatement stmt = conn.prepareStatement("SELECT NAME, NOTES,
> PICTURE FROM TESTTABLE WHERE NAME = ?");
> stmt.setString(1, name);
>
> if (stmt.execute()) {
> ResultSet rs = stmt.getResultSet();
> rs.next();
>
> Clob clob = rs.getClob(2);
> strings = clob.getSubString((long)1, (int)clob.length());
> System.out.println(strings);
>
> Blob blob = rs.getBlob(3);
> Exception here ---> imgData = new ImageData(blob.getBinaryStream());
> }
> }
> catch (SQLException e) {
> e.printStackTrace();
> }
> }
>
>
> So, am I overlooking something easy, or am I just barking up the wrong
> tree completely?
>
> Thanks for any assistance,
>
> Brian
Re: Problem saving and retrieving ImageData from Database [message #465475 is a reply to message #465465] Wed, 14 December 2005 18:33 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: bjuergensmeyer.NoSpam.sbcglobal.net

Hi, Veronika,

On Wed, 14 Dec 2005 09:55:36 -0500, Veronika Irvine wrote:

> There is more to ImageData than just the bytes in the data field - there is
> information about transparency, scanLinePad, width, height, ...
>
> In the constructor ImageData(InputStream stream) the input stream is
> expected to be the full description of an image e.g.
> myObject.class.getResourceAsStream ("C:\\abc.bmp").
>
> Have a look at ImageLoader.save(OutputStream stream, int format) and
> ImageLoader.load(InputStream stream).
>

Thank you for your help! I was able to get a save and restore working
from the database using ImageLoader.

I'm still looking at this, though. If I'm interpreting things
correctly, it looks like ImageLoader was really designed to manage a
group of ImageData objects, which then could be passed to Image objects
as required, and that ImageData objects were really only designed to be
manipulated in the context of an ImageLoader. The big problem is that
there doesn't seem to be any effective way of adding an ImageData object
from an ImageLoader other than to either load it directly from the hard
drive or by using an input stream/output stream pair to save an ImageData
from one ImageLoader to another.

And removing an ImageData from the data array seems to be impossible. As
a matter of fact, it looks like the only way to remove an item from the
array would be to delete the image from the database and reload the
ImageLoader.

Am I correct in all this?

Thanks,

Brian
Re: Problem saving and retrieving ImageData from Database [message #465476 is a reply to message #465465] Wed, 14 December 2005 18:51 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: bjuergensmeyer.NoSpam.sbcglobal.net

Hi, Veronika,

On Wed, 14 Dec 2005 09:55:36 -0500, Veronika Irvine wrote:

> There is more to ImageData than just the bytes in the data field - there is
> information about transparency, scanLinePad, width, height, ...
>
> In the constructor ImageData(InputStream stream) the input stream is
> expected to be the full description of an image e.g.
> myObject.class.getResourceAsStream ("C:\\abc.bmp").
>
> Have a look at ImageLoader.save(OutputStream stream, int format) and
> ImageLoader.load(InputStream stream).


Just as a followup to my 2nd post, I also realize that you can generate a
new ImageData[] and set ImageLoader.data to the new array.

It just seems like there should have been an
ImageLoader.AddImageData(ImageData) and an
ImageLoader.RemoveImageData(ImageData) rather than having to do the work
myself.

I realize that there isn't anything you personally can be expected to do
about this. :)

Thanks anyway,

Brian
Re: Problem saving and retrieving ImageData from Database [message #465537 is a reply to message #465475] Wed, 14 December 2005 21:52 Go to previous message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
The reason the ImageLoader class takes an array of ImageData is because
there are image types that result in multiple ImageData objects e.g.
animated gifs - one ImageData for each frame, multi-page tiffs, and ico
images with multiple sizes in one image. The ImageLoader is intended to
work with one image at a time. That is not say that you can't use it to
stream multiple images at once but you shold know that some of the images
you are streaming may be made up of more than one ImageData.

ImageData objects can be created and manipulated without the ImageLoader.
For example you can create an ImageData directly from a file name or an
input stream. You can scale it, change individual pixel values, change the
alpha transparency - all without using the ImageLoader.

It appears that you are talking about having only one InputStream for the
entire database. I would think it would be better to have an input stream
for each image in the database.


"Brian L. Juergensmeyer" <bjuergensmeyer@NoSpam.sbcglobal.net> wrote in
message news:pan.2005.12.14.18.33.56.924862@NoSpam.sbcglobal.net...
>
> I'm still looking at this, though. If I'm interpreting things
> correctly, it looks like ImageLoader was really designed to manage a
> group of ImageData objects, which then could be passed to Image objects
> as required, and that ImageData objects were really only designed to be
> manipulated in the context of an ImageLoader. The big problem is that
> there doesn't seem to be any effective way of adding an ImageData object
> from an ImageLoader other than to either load it directly from the hard
> drive or by using an input stream/output stream pair to save an ImageData
> from one ImageLoader to another.
>
> And removing an ImageData from the data array seems to be impossible. As
> a matter of fact, it looks like the only way to remove an item from the
> array would be to delete the image from the database and reload the
> ImageLoader.
>
> Am I correct in all this?
>
> Thanks,
>
> Brian
Previous Topic:Calling a C library from a SWT app ?
Next Topic:org.eclipse.search
Goto Forum:
  


Current Time: Thu Apr 18 05:15:09 GMT 2024

Powered by FUDForum. Page generated in 0.02034 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top