2013-01-23 25 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是该卡与所有其他对象顺便说一句,我增加 对字体不起作用,没有这个文字是非常不愉快的。

相关问题