sv_in
sv_in

Reputation: 14049

Returning a gif image from a method

I am creating an application in java which will be the part of an external application. My application contains a viewport which shows some polygons and stuff like that. The external application needs to get the image of the viewport in gif format. For that it calls a method in an interface (implemented by my application) and my application returns the image. The external application needs to store the image in database (or something related to it which I dont need to worry about).

My question is:- What should be the data container type of the image when my application send it to the external application? I mean what should be the return type of the method? Currently my gif encoder class returns a byte array. Is there any other 'better' option?

Upvotes: 2

Views: 2182

Answers (4)

asalamon74
asalamon74

Reputation: 6170

I'd create two methods:

  1. First method creates the image and returns a java.awt.Image. Here you can put the drawing part of your method.
  2. The second method creates a gif representation of the java.awt.Image as requested by the external application. It should return OutputStream as already suggested.

Upvotes: 0

DJClayworth
DJClayworth

Reputation: 26876

If your 'application' is actually calling a Java method then it should understand Java return types and you should return java.awt.image.

If you are doing this through some kind of remote procedure that can't understand Java types I would return a byte array and let the receiving app decode it.

Upvotes: 0

Neal Swearer
Neal Swearer

Reputation: 2572

A more intuitive return type might be java.awt.Image.

Here are some examples: http://www.google.com/codesearch?q=java+gif+image&hl=en&btnG=Search+Code

Upvotes: 2

Jason Cohen
Jason Cohen

Reputation: 83051

A byte array could be appropriate if you expect the GIFs to be small, but you might consider using an OutputStream so you can stream bits more efficiently.

Even if today you just return a fully-populated ByteArrayOutputStream, this would allow you to change your implementation in future without affecting cilent code.

Upvotes: 3

Related Questions