Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Image from byte[]

Thread view: 
Ivan Danicic - 29 May 2007 19:26 GMT
Hello, I have a byte[]img (constructed from an actual .bmp image)
and want to use it to make an object of type Image, not to display
it but apply to it getPixels(). I know the width and height.
I've tried various things without success. I'd be most grateful for a
simple solution.
visionset - 29 May 2007 19:32 GMT
> Hello, I have a byte[]img (constructed from an actual .bmp image)
> and want to use it to make an object of type Image, not to display
> it but apply to it getPixels(). I know the width and height.
> I've tried various things without success. I'd be most grateful for a
> simple solution.

Have you tried:

javax.imageio.ImageIO.read(new java.io.ByteArrayInputStream(yourByteArray);

Signature

Mike W

Knute Johnson - 30 May 2007 02:14 GMT
>> Hello, I have a byte[]img (constructed from an actual .bmp image)
>> and want to use it to make an object of type Image, not to display
[quoted text clipped - 5 lines]
>
> javax.imageio.ImageIO.read(new java.io.ByteArrayInputStream(yourByteArray);

or

Toolkit.createImage(byte[] imagedata)

Signature

Knute Johnson
email s/nospam/knute/

Ivan Danicic - 30 May 2007 13:23 GMT
>>> Hello, I have a byte[]img (constructed from an actual .bmp image)
>>> and want to use it to make an object of type Image, not to display
[quoted text clipped - 9 lines]
>
> Toolkit.createImage(byte[] imagedata)
Hello Knute, this gives error: cannot be referenced from static context.
Ivan
Lew - 30 May 2007 13:38 GMT
Knute Johnson wrote:
>> Toolkit.createImage(byte[] imagedata)

> Hello Knute, this gives error: cannot be referenced from static context.

Your response to Knute's suggestion is rather surreal.

You show no code and did not quote the error message, so I will make some
guesses absent that crucial information:

Toolkit is a class, and createImage() a static method, so that cannot be the
problem.  Therefore, assuming the quoted line is where the error occurs, the
compiler must be barfing over imagedata.  (A better name would be imageData,
btw.)  That means that imagedata must be a non-static variable, and you must
be calling Toolkit from a static method (main(), perhaps?).

These errors have nothing to do with the value of Knute's suggestion.

Fix your bugs and try again.

Signature

Lew

Ivan Danicic - 30 May 2007 15:31 GMT
> Knute Johnson wrote:
>>> Toolkit.createImage(byte[] imagedata)
[quoted text clipped - 15 lines]
>
> Fix your bugs and try again.
Hello, I have this:
Image img;
void applytexture(byte[]texture,etc){
bla-bla;
img=
Toolkit.createImage(texture);
etc
}
javac gives:
non-static method createImage(byte[]) cannot be referenced from a
static context
img=Toolkit.createImage(texture);
           ^
           
I've got nothing static.
Thanks for your interest.
Ivan
Lew - 30 May 2007 22:39 GMT
Lew wrote:
>> You show no code and did not quote the error message, so I will make some
>> guesses absent that crucial information:

>> Toolkit is a class, and createImage() a static method,

I was wrong.  That's what I get for guessing.
<http://java.sun.com/javase/6/docs/api/java/awt/Toolkit.html#createImage(java.lan
g.String
)>

Of course, I would not have made this wrong guess if you had provided the
error message in the first place.

>> These errors have nothing to do with the value of Knute's suggestion.
>>
>> Fix your bugs and try again.

Here I was not wrong.

> Hello, I have this:
> Image img;
> void applytexture(byte[]texture,etc){
> bla-bla;

"Bla-bla" is not good for an SSCCE, since it doesn't compile, but it didn't
matter this time.

> img=
> Toolkit.createImage(texture);
[quoted text clipped - 7 lines]
>            
> I've got nothing static.

Yes, you do.  The call to createImage() is made through a static context, but
it's not a static method.  Ergo, error.

Whenever you call a method just through the class name it's a static call.
Since the method is not declared static, you got an error.

Read the Javadoc on the method.  Invoke it through an instance of Toolkit.
<http://java.sun.com/javase/6/docs/api/java/awt/Component.html#getToolkit()>

Signature

Lew

Lew - 30 May 2007 22:46 GMT
Lew wrote:
>> You show no code and did not quote the error message, so I will make some guesses absent that crucial information:
>> Toolkit is a class, and createImage() a static method,

I was wrong.  That's what I get for guessing.
<http://java.sun.com/javase/6/docs/api/java/awt/Toolkit.html#createImage(java.lan
g.String
)>

Of course, I would not have made this wrong guess if you had provided the
error message in the first place.

>> These errors have nothing to do with the value of Knute's suggestion.
>>
>> Fix your bugs and try again.

Here I was not wrong.

> Hello, I have this:
> Image img;
> void applytexture(byte[]texture,etc){
> bla-bla;

"Bla-bla" is not good for an SSCCE, since it doesn't compile, but it didn't
matter this time.

> img= Toolkit.createImage(texture);
> etc
[quoted text clipped - 4 lines]
>             ^
>             I've got nothing static.

Yes, you do.  The call to createImage() is made through a static context, but
it's not a static method.  Ergo, error.

Whenever you call a method just through the class name it's a static call.
Since the method is not declared static, you got an error.

Read the Javadoc on the method.  Invoke it through an instance of Toolkit.
<http://java.sun.com/javase/6/docs/api/java/awt/Component.html#getToolkit()>

Signature

Lew

Ivan Danicic - 30 May 2007 13:21 GMT
>> Hello, I have a byte[]img (constructed from an actual .bmp image)
>> and want to use it to make an object of type Image, not to display
[quoted text clipped - 5 lines]
>
> javax.imageio.ImageIO.read(new java.io.ByteArrayInputStream(yourByteArray);
Hello Mike, this worked fine. Thanks very much.
Ivan
Stefan Ram - 30 May 2007 15:43 GMT
>Hello, I have a byte[]img (constructed from an actual .bmp image)
>and want to use it to make an object of type Image, not to display

 Here is a simple example how to paint into an int array
 and then create a BufferedImage object from it. This image
 then is saved as a jpeg image.

 WARNING: A file »generated.jpg« will be   o v e r w r i t t e n
 by the process.

public class Main
{ public static void main ( final java.lang.String[] args )
 { int  width = 800; int height =  600;
   int[] target = new int[ width * height ];
   for( int y = 0; y < height; y++ )
   { for( int x = 0; x < width; x++ )
     { target[ x + y * width ]=( x )% 255 +(( y )% 255 )* 256; }}
   java.awt.image.BufferedImage output =
   new java.awt.image.BufferedImage
   ( width, height, java.awt.image.BufferedImage.TYPE_INT_RGB );
   output.setRGB( 0, 0, width, height, target, 0, width );
   java.io.BufferedOutputStream out = null;
   try { out = new java.io.BufferedOutputStream
     ( new java.io.FileOutputStream( "generated.jpg" )); }
   catch( final java.io.FileNotFoundException fileNotFoundException )
   { throw new java.lang.RuntimeException( fileNotFoundException ); }
   com.sun.image.codec.jpeg.JPEGImageEncoder encoder =
   com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder( out );
   com.sun.image.codec.jpeg.JPEGEncodeParam param =
   encoder.getDefaultJPEGEncodeParam( output );
   param.setQuality( 1.0f, false);
   encoder.setJPEGEncodeParam( param );
   try { encoder.encode( output ); out.close(); }
   catch( final java.io.IOException ioException )
   { throw new java.lang.RuntimeException( ioException ); }}}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.