Re: Image from byte[]
Ivan Danicic <ivie@localhost.localdomain> writes:
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 ); }}}