Jay Kandy
Posts: 77
Nickname: jay
Registered: Mar, 2002
|
|
Re: converting the extensions
|
Posted: Apr 4, 2002 11:31 AM
|
|
Ali,
I think what you are doing is a cool idea. And to support that, heres a class that can generate JPEGs of Strings. To run, you need JAI which, you can download from http://java.sun.com/products/java-media/jai/downloads/download.html Also, this is a crude JFrame but you can use JPEGView class to pretty things up.
But I dont see why you would want to generate images of text!
Anyways heres the class:
import java.util.Random;
import java.io.IOException;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.image.BufferedImage;
import com.sun.media.jai.codec.JPEGEncodeParam;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.ImageCodec;
class JPEGView
{
private OutputStream out = null;
private String regionReaderClassName = null;
private RegionReader reader = null;
private Graphics2D graphics = null;
public JPEGView()
{
}
/**
* @param stream <code>OutputStream</code> on which image is drawn.
*/
public String createImage(OutputStream stream, String text)
{
try
{
out = stream;
JPEGEncodeParam jep = new JPEGEncodeParam();
jep.setQuality( 1.0F );
BufferedImage bi = new BufferedImage( 400, 600, BufferedImage.TYPE_3BYTE_BGR );
graphics = bi.createGraphics();
draw( text );
ImageEncoder encoder = ImageCodec.createImageEncoder( "JPEG", out, jep );
encoder.encode( bi );
out.close();
stream.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
return "image/jpg";
}
void draw(String t)
{
System.out.println( "Drawing text..." );
Random r = new Random();
graphics.setColor( new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255) ) );
graphics.drawString( t, 0, 100 );
}
}
public class JPEGViewRunner extends JFrame
{
protected String text; /* Text to be drawn. */
protected ByteArrayOutputStream stream = null;
protected ImageIcon image = null;
protected JPEGView view = null;
JPEGViewRunner(String t)
{
text = t;
stream = new ByteArrayOutputStream();
view = new JPEGView();
view.createImage( stream, text );
image = new ImageIcon( stream.toByteArray() );
getContentPane().add( new JLabel( image ) );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize( 400, 600 );
setVisible( true );
}
public static void main( String s[] )
{
new JPEGViewRunner( "Hello World" );
}
}
|
|