2013-01-23 37 views
0

我正在尋找一種重新排列幾個參數的方法,將其作爲字符串和一個字節[]圖像進行重新排列,並將它們全部作爲一個圖像(例如jpg)準備好打印(immidiatly) 。Java - 將幾個字符串轉換爲圖像格式

例如:

public static void printCard(String name, String LName, Image MainImage) 

Basicly此功能將是一個簡單的卡片打印機。 我正在尋找一個想法或者一個能夠指導我的想法,如果有人能夠指導我,這可能會非常簡單。

+1

您是否正在尋找在圖像上打印文本。 如果是這裏是[例子] [1] .. [1]:http://stackoverflow.com/questions/2658554/using-graphics2d-to-overlay-text-on-a -bufferedimage-and-return-a-bufferedimage/2658663#2658663 – user1983527

+0

即時消息正在尋找創建圖像我自己與一個固定的大小和白色背景,然後添加上面的其他參數 –

+0

@ Adir.el:這是一個論壇編程問題和不要要求其他人爲你實施軟件。 – jarnbjo

回答

0

如果你想直接從您的應用程序打印,您可以使用 java.awt.print包。

嘗試此方法

public static void printCard(final String name, final String lName, final Image mainImage){ 

    Printable contentToPrint = new Printable(){ 
     @Override 
     public int print(Graphics graphics, PageFormat pageFormat, int page) throws PrinterException { 
      if (page > 0) { 
       return NO_SUCH_PAGE; 
      } 
      pageFormat.setOrientation(PageFormat.PORTRAIT); 
      graphics.drawImage(mainImage, 0, 0, null); 
      graphics.drawString(lName, 100, 300); 
      graphics.drawString(name, 100, 100); 

      return PAGE_EXISTS; 
     } 
    }; 

    PrinterJob job = PrinterJob.getPrinterJob(); 
    job.setPrintable(contentToPrint); 
    //You can show a print dialog before printing by job by wrapping the following blocks with a conditional statement if(job.printDialog()){...} 
    try { 
     job.print(); 
    } catch (PrinterException e) { 
     System.err.println(e.getMessage()); 
    } 

} 

您需要從java.awt.print導入打印相關的類。

+0

任何想法如何可以修復打印大小? –

+0

@Adir.el你可以使用java.awt.print.PageFormat來設置紙張大小。這裏有一些有用的文檔。 http://www.massapi.com/class/java/awt/print/PageFormat.java.html你可以谷歌更多的樣本。 – harun

1

這是一個簡單的方法,我用它將文本添加到預先存在的圖像上。

我敢肯定,你可以制定出如何傳遞一個空白圖像,並添加其他行,你認爲合適的。

private BufferedImage drawText2(BufferedImage bi, String outputText) { 
    Graphics2D g2d = bi.createGraphics(); 
    g2d.setFont(new Font("Helvetica", Font.BOLD, 36)); 
    FontMetrics fm = g2d.getFontMetrics(); 
    int textWidth = fm.stringWidth(outputText); 
    int imageWidth = bi.getWidth(); 
    int leftAlignment; 
    int topAlignment; 

    // Align the text to the middle 
    leftAlignment = (imageWidth/2) - (textWidth/2); 

    // Align the text to the top 
    topAlignment = fm.getHeight() - 10; 

    // Create the drop shadow 
    g2d.setColor(Color.DARK_GRAY); 
    g2d.drawString(outputText, leftAlignment + 2, topAlignment + 2); 

    // Create the text itself 
    g2d.setColor(Color.LIGHT_GRAY); 
    g2d.drawString(outputText, leftAlignment, topAlignment); 

    g2d.dispose(); 
    return bi; 
} 
+0

我正在考慮這個解決方案,並會盡快實現暗示。將更新你 –

0

我用graphics2d來實現我的功能。這種方法recive 2幅圖像和6串: Bufferdimage BI是我的空白圖像,這個圖像I添加對象上(例如:圖像,字符串):

private static BufferedImage drawText2(BufferedImage logo,BufferedImage small,BufferedImage bi, String Headline,String outputText2,String outputText3,String outputText4,String outputText5,String outputText6) { 
    Graphics2D g2d = bi.createGraphics(); 
    RenderingHints rh = new RenderingHints(
      RenderingHints.KEY_TEXT_ANTIALIASING, 
      RenderingHints.VALUE_TEXT_ANTIALIAS_GASP); 
    g2d.setRenderingHints(rh); 
    g2d.setFont(new Font("Arial", Font.BOLD, 50)); 
    FontMetrics fm = g2d.getFontMetrics(); 
    int textWidth = fm.stringWidth(Headline); 
    int imageWidth = bi.getWidth(); 
    int leftAlignment; 
    int topAlignment; 

    // Align the text to the top 
    topAlignment = fm.getHeight() - 10; 

    // Create the text itself 
    //headline 
    leftAlignment = (imageWidth/2) - (textWidth); 
    g2d.setColor(Color.blue); 
    g2d.drawString(Headline, leftAlignment+290, topAlignment+60); 
    //property changed 
    g2d.setFont(new Font("Arial", Font.BOLD, 30)); 
    fm = g2d.getFontMetrics(); 
    textWidth = fm.stringWidth(Headline); 
    //second line 
    textWidth = fm.stringWidth(outputText2); 
    leftAlignment = (imageWidth/2) - (textWidth); 
    g2d.setColor(Color.black); 
    g2d.drawString(outputText2, leftAlignment+290, topAlignment+120); 
    //third line 
    textWidth = fm.stringWidth(outputText3); 
    leftAlignment = (imageWidth/2) - (textWidth); 
    g2d.setColor(Color.black); 
    g2d.drawString(outputText3, leftAlignment+290, topAlignment+160); 
    //4 line 
    textWidth = fm.stringWidth(outputText4); 
    leftAlignment = (imageWidth/2) - (textWidth); 
    g2d.setColor(Color.black); 
    g2d.drawString(outputText4, leftAlignment+290, topAlignment+200); 
    //5 line 
    textWidth = fm.stringWidth(outputText5); 
    leftAlignment = (imageWidth/2) - (textWidth); 
    g2d.setColor(Color.black); 
    g2d.drawString(outputText5, leftAlignment+290, topAlignment+240); 

    //property changed 
    g2d.setFont(new Font("Arial", Font.getFont("Arial").HANGING_BASELINE, 20)); 
    fm = g2d.getFontMetrics(); 
    //security line 
    textWidth = fm.stringWidth(outputText6); 
    leftAlignment = (textWidth); 
    g2d.setColor(Color.red); 
    g2d.drawString(outputText6, 10, topAlignment+300); 
    //logo 
    g2d.drawImage (logo, 44, 44,180,70, null); 
    //profile 
    g2d.drawImage (small, 60, 120,160,190, null); 
    g2d.dispose(); 
    return bi; 
} 

BI是該卡與所有其他對象順便說一句,我增加 對字體不起作用,沒有這個文字是非常不愉快的。

相關問題