The Artima Developer Community
Sponsored Link

Java Answers Forum
converting the extensions

1 reply on 1 page. Most recent reply: Apr 4, 2002 11:31 AM by Jay Kandy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Alireza Aghasi

Posts: 5
Nickname: bluesky
Registered: Apr, 2002

converting the extensions Posted: Apr 4, 2002 1:05 AM
Reply to this message Reply
Advertisement
Hi ...
I would be so happy if anyone can help me with this problem ...
I have prepared an applet for mapping the keyboard and enabling the client to write Persian ...
But I have a problem , I want to use this applet for an Email interface , in order to send pages in Persian I want to convert my texts to Gif or Jpg extension . can anyone tell me how to make such an applet or tell me where I can find it?
Thanks very much for your attention ...
sicerely yours : Ali


Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: converting the extensions Posted: Apr 4, 2002 11:31 AM
Reply to this message Reply
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" );
	}
}

Flat View: This topic has 1 reply on 1 page
Topic: Saving a 2D draw to a vectorial file? Previous Topic   Next Topic Topic: TreeMap problems!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use