Image from byte[]

I

Ivan Danicic

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.
 
V

visionset

Ivan Danicic said:
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);
 
K

Knute Johnson

visionset said:
Ivan Danicic said:
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);

or

Toolkit.createImage(byte[] imagedata)
 
I

Ivan Danicic

Ivan Danicic said:
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);
Hello Mike, this worked fine. Thanks very much.
Ivan
 
I

Ivan Danicic

visionset said:
Ivan Danicic said:
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);

or

Toolkit.createImage(byte[] imagedata)
Hello Knute, this gives error: cannot be referenced from static context.
Ivan
 
L

Lew

Knute said:
Toolkit.createImage(byte[] imagedata)

Ivan said:
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.
 
I

Ivan Danicic

Knute said:
Toolkit.createImage(byte[] imagedata)

Ivan said:
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.
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
 
S

Stefan Ram

Ivan Danicic said:
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 ); }}}
 
L

Lew

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

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

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
}
javac gives:
non-static method createImage(byte[]) cannot be referenced from a
static context
img=Toolkit.createImage(texture);
^

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()>
 
L

Lew

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

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

Here I was not wrong.

Ivan said:
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
}
javac gives:
non-static method createImage(byte[]) cannot be referenced from a static context
img=Toolkit.createImage(texture);
^
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()>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top