2010-11-20 16 views
6

Como el gobierno brasileño no tiene una API pública para códigos postales, estoy tratando de utilizar el código correios.com.br para generar la imagen a continuación, de modo que pueda generar mis propias imágenes para entrenar el programa OCR .Java awt font spacing options

alt text

Creo que he conseguido ya casi todo bien, además de la separación entre el texto y los colores:

alt text

No estoy interesado en los colores ahora, pero el espaciado del texto es realmente me molesta Por ejemplo, eche un vistazo al 'Ti' en 'Tijuca'. Las dos letras están muy juntas en la imagen original y no puedo reproducir esta característica. Ya he intentado derivar la fuente, estableciendo valores en TextAttribute.TRACKING y TextAttribute.KERNING, pero no funcionó.

aquí según mi código:

import java.awt.Color; 
import java.awt.font.TextAttribute; 
import java.awt.Font; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.util.Hashtable; 
import javax.imageio.ImageIO; 

public class CreateImage { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     int width = 570; 
     int height = 120; 
     boolean new_image = true; 

     BufferedImage img;  

     if (!new_image) { 
      try { 
       img = ImageIO.read(new File("original.jpg"));       
      } catch (IOException e) { 
       e.printStackTrace();    
       new_image = true; 
       img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
      } 
     } else { 
      img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);   
     } 

     Graphics2D g2d = img.createGraphics(); 
     if (new_image) { 
      // white background 
      g2d.setPaint(Color.WHITE); 
      g2d.fillRect(0, 0, width, height); 

      g2d.setPaint(Color.BLACK); 
     } else { 
      g2d.setPaint(Color.RED); 
     } 


     Font myfont = new Font("SansSerif", Font.BOLD, 11);  

     /* 
     Hashtable<TextAttribute, Float> attributes = new Hashtable<TextAttribute, Float>(); 
     attributes.put(TextAttribute.TRACKING, new Float(-0.01)); 
     myfont = myfont.deriveFont(attributes); 
     */ 

     g2d.setFont(myfont); 

     g2d.drawString("Logradouro:", 5, 13); 
     g2d.drawString("Bairro:", 5, 33); 
     g2d.drawString("Localidade/UF:", 5, 53); 
     g2d.drawString("CEP:", 5, 73); 

     g2d.drawString("Avenida das Américas - de 3979 a 5151 - lado ímpar", 105, 13); 
     g2d.drawString("Barra da Tijuca", 105, 33); 
     g2d.drawString("Rio de Janeiro/RJ", 105, 53); 
     g2d.drawString("22631-004", 105, 73); 

     g2d.dispose();  

     File file = new File("clone.jpg"); 
     try { 
      ImageIO.write(img, "jpg", file); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     System.out.println("clone.jpg file created."); 
    } 

} 

Mi pregunta es: ¿Cuáles son las otras opciones para controlar cómo está separada una cadena cuando se dibuja? ¿Tiene alguna idea sobre lo que el código original puede estar haciendo?

Gracias!

Respuesta

2

Realmente no puedo decir cómo hacerlo correctamente en Swing, pero lo que está buscando es en tipografía llamada "kerning", por lo que ese atributo debería hacer algo similar.

+0

Véase también http://download.oracle.com/javase/6/docs/api/java/awt/font/TextAttribute.html – trashgod

+0

Hola Jolta. Gracias por su sugerencia, pero, como dije en la pregunta, ya he intentado con Kerning y Tracking, pero no funcionó. – jbochi