Einlesen eines jpg-Bildes und Ermitteln der Breite.
- Main.java
class Observer
implements java.awt.image.ImageObserver
{ public boolean imageUpdate
( final java.awt.Image img, final int flags,
final int x, final int y, final int width, final int height )
{ if(( flags & java.awt.image.ImageObserver.ERROR )!= 0 )
throw new java.lang.RuntimeException( "ERROR" );
if(( flags & java.awt.image.ImageObserver.ABORT )!= 0 )
throw new java.lang.RuntimeException( "ABORT" );
return(( flags & java.awt.image.ImageObserver.ALLBITS )== 0 ); }} public class Main
{ public static void main ( final java.lang.String[] args )
{ java.awt.Image image =
java.awt.Toolkit.getDefaultToolkit().getImage( "example.jpg" );
final java.awt.image.ImageObserver observer = new Observer();
int width; while(( width = image.getWidth( observer ))< 0 );
java.lang.System.out.println( width ); }} // einfacher: ImageIO.read(...).getWidth();
Erzeugen eines jpg-Bildes
- Main.java
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++ )
{ final int r =(( int )( 128 + 128 * Math.tan
( 0.00001 * ( x - 30 )* y / width * height ))<< 16 )& 0xff0000;
final int g =(( int )( 128 + 128 * Math.tan
( 0.00002 *( width - x )*( y - 30 )/ width * height ))<< 8 )& 0xff00;
final int b =(( int )( 128 + 128 * Math.tan
( 0.00003 * x * y / width * height ))<< 0 )& 0xff;
target[ x + y * width ]= 0xff000000 | r | g | b; }}
for( int j = 0; j <( int )( 1 ); ++j )
{ int x =( int )( Math.random() * width );
int y =( int )( Math.random() * height );
int r =( int )( Math.random() * 256 );
int g =( int )( Math.random() * 256 );
int b =( int )( Math.random() * 256 );
for( long i = 0; i <( long )( 1E12 * Math.random() ); ++i )
{ if( x > 0 && x < width && y > 0 && y < height )
{ r = r < 0 ? 0 : r > 255 ? 255 : r;
g = g < 0 ? 0 : g > 255 ? 255 : g;
b = b < 0 ? 0 : b > 255 ? 255 : b;
final int rgb =(( r << 16 )&0xff0000)|(( g << 8 )&0xff00)|( b & 0xff );
target[ x + y * width ]= 0xff000000 | rgb; }
x +=( int )(( Math.random() - 0.5 )* 3 );
y +=( int )(( Math.random() - 0.5 )* 3 );
r +=( int )(( Math.random() - 0.5 )* 3 );
g +=( int )(( Math.random() - 0.5 )* 3 );
b +=( int )(( Math.random() - 0.5 )* 3 ); }}
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 ); }}}- Animation
http://javaboutique.internet.com/TumbleItem/