2016-06-02 3 views
0

私は.txtファイルを私のJavaコードを使って.bmpファイルに変換しています。私の.txtファイルには複数の行が含まれています。私はすべての行を読み、イメージに印刷しています。ファイルを読み込み、.bmpイメージを作成する際に次の行に移動するにはどうすればいいですか?

**私のテキストファイル**

werwerwr

ABC ABC ABC

xxxxの

**作成取得された画像(.BMP)**

すべてが次のように1行に印刷されます。

45werwerwrabc ABC abcxxxx

マイコード:

import java.awt.Color; 
import java.awt.Font; 
import java.awt.FontMetrics; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.image.BufferedImage; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import javax.imageio.ImageIO; 

public class TextToGraphics { 

    public static void main(String[] args) { 
     try { 
      String storeData = ""; 
      String data = ""; 
      File f = new File("/home/vipul/Desktop/123.txt"); 
      BufferedReader br = new BufferedReader(new FileReader(f)); 

      while ((data = br.readLine()) != null) { 
       storeData =storeData + data +System.lineSeparator(); 
      } 


      BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR); 
      Graphics2D g2d = img.createGraphics(); 
      Font font = new Font("Arial", Font.PLAIN, 24); 
      g2d.setFont(font); 

      FontMetrics fm = g2d.getFontMetrics(); 

      int width = fm.stringWidth(storeData); 
      int height = fm.getHeight(); 
      g2d.dispose(); 

      img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); 
      g2d = img.createGraphics(); 
      g2d.setPaint (new Color (255, 255, 255)); 
      g2d.fillRect (0, 0, img.getWidth(), img.getHeight()); 
      System.out.println(img.getWidth()+" "+img.getHeight()); 
      g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); 
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
      g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); 
      g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); 
      g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); 
      g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
      g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 
      g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); 
      g2d.setFont(font); 
      fm = g2d.getFontMetrics(); 


      g2d.setColor(Color.BLACK); 


      g2d.drawString(storeData, 0, fm.getAscent()); 
      g2d.dispose(); 

      ImageIO.write(img, "bmp", new File("/home/vipul/Desktop/123.bmp")); 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

    } 

} 

私はテキストファイルとして画像を同じようにしたいです。テキストファイルが新しい行に移動する場合、イメージは新しい行に同じ単語をもたなければなりません。

助けてください。 事前に感謝!

+1

主な問題は、画像のための改行文字としての\ N ' '検討しているということです! http://stackoverflow.com/questions/27658986/how-to-convert-multi-line-text-to-image-using-javaの回答をご覧ください。これはトリックを行う必要があります。 – blackSmith

答えて

1

ファイルを文字列で保存しています。各行の後にlineSeparatorを追加しても、imgは改行文字が何であるかを知らない。自分でデータを分割し、各行に対してdrawStringを呼び出さなければなりません。例:

public class TextToGraphics { 

public static void main(String[] args) { 
    try { 
     String storeData = ""; 
     String data = ""; 
     File f = new File("/home/vipul/Desktop/123.txt"); 
     BufferedReader br = new BufferedReader(new FileReader(f)); 

     while ((data = br.readLine()) != null) { 
      storeData =storeData + data +System.lineSeparator(); 
     } 
     String [] sArr = storeData.split("\n"); 
     int longest =0; 
     int lines = 0; 
     for (String s : sArr){  //find the longest string 
      if(s.length()>longest){ 
      longest = s.length(); 
      } 
      lines++;    //count lines 
     } 

     BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR); 
     Graphics2D g2d = img.createGraphics(); 
     Font font = new Font("Arial", Font.PLAIN, 24); 
     g2d.setFont(font); 

     FontMetrics fm = g2d.getFontMetrics(); 

     int width = fm.stringWidth(storeData); 
     int height = fm.getHeight(); 
     g2d.dispose(); 

     img = new BufferedImage(longest*font.getSize(), lines*font.getSize(), BufferedImage.TYPE_3BYTE_BGR); // set width and height in accordance with longest string and number of lines 
     g2d = img.createGraphics(); 
     g2d.setPaint (new Color (255, 255, 255)); 
     g2d.fillRect (0, 0, img.getWidth(), img.getHeight()); 
     System.out.println(img.getWidth()+" "+img.getHeight()); 
     g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); 
     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); 
     g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); 
     g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); 
     g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
     g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 
     g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); 
     g2d.setFont(font); 
     fm = g2d.getFontMetrics(); 


     g2d.setColor(Color.BLACK); 
     int i = 0; 
     for(String s : storeData.split("\n")){    
      g2d.drawString(s, 0, fm.getAscent()+i); // call drawString for each line 
      i+=10; 
     } 
     g2d.dispose(); 

     ImageIO.write(img, "bmp", new File("/home/vipul/Desktop/123.bmp")); 

    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 

} 

}

1

私は私の問題のための解決策を見つけた:

import java.awt.Color; 
import java.awt.Font; 
import java.awt.FontMetrics; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.image.BufferedImage; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import javax.imageio.ImageIO; 

public class TextToGraphics { 

    public static void main(String[] args) { 
     try { 
      int imgWid = 300; 
      int imgHei = 300; 

      String text = "Hello\nlklkj"; 
      String storeData = ""; 
      String data = ""; 
      File f = new File("/home/vipul/Desktop/123.txt"); 
      BufferedReader br = new BufferedReader(new FileReader(f)); 
      int nextLinePosition=100; 
      int fontSize = 24; 


      // BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_GRAY);//png 
      BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR); 
      Graphics2D g2d = img.createGraphics(); 
      Font font = new Font("Arial", Font.PLAIN, fontSize); 
      g2d.setFont(font); 

      FontMetrics fm = g2d.getFontMetrics(); 
      // int width = fm.stringWidth(text); 
      // int width = fm.stringWidth(storeData); 
      int width =imgWid; 

      // int height = fm.getHeight(); 
      int height =imgHei; 
      g2d.dispose(); 

      img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); 
    //   img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
      g2d = img.createGraphics(); 
      g2d.setPaint(new Color(255, 255, 255)); 
      g2d.fillRect(0, 0, img.getWidth(), img.getHeight()); 
      System.out.println(img.getWidth() + " " + img.getHeight()); 
      g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); 
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
      g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); 
      g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); 
      g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); 
      g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
      g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 
      g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); 
      g2d.setFont(font); 
      fm = g2d.getFontMetrics(); 

      g2d.setColor(Color.BLACK); 
     // g2d.setBackground(Color.ORANGE); 
      // g2d.drawString(text, 0, fm.getAscent()); 
//   for(String line : text.split("\n")){ 
//   g2d.drawString(storeData, 0, fm.getAscent()); 
//  } 
      // private void drawString(Graphics2D g2d, String text, int x, int y) { 
//   for (String line : storeData.split("\n")) // g2d.drawString(line, width, height += g2d.getFontMetrics().getHeight()); 
//   { 
      while ((data = br.readLine()) != null) { 
       g2d.drawString(data, 0, nextLinePosition); 
       // storeData = storeData + data + nextLinePosition; 
       nextLinePosition=nextLinePosition+fontSize; 
       //storeData = storeData + data + '\n'; 
      } 



//     g2d.drawString(line, 0, 12); 
//   } 
      //} 

      // g2d.drawString(storeData, 0, fm.getAscent()); 
      g2d.dispose(); 
      // try { 
      ImageIO.write(img, "bmp", new File("/home/vipul/Desktop/123.bmp")); 
      // } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

    } 

} 
関連する問題