0

我正在寻找一种方法来重新排列作为字符串和一个字节 [] 图像接收的几个参数,并将它们全部放在一起作为一个图像(例如 jpg)准备打印(立即)。

例子:

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

基本上这个功能将是一个简单的卡片打印机。我正在寻找一个想法或可以指导我的人,如果有人能指导我一点,这可能会很容易。

4

3 回答 3

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;
}
于 2013-01-23T12:26:02.993 回答
0

如果您想直接从您的应用程序打印,您可以使用 java.awt.printpackage.json 。

试试这个方法

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.

于 2013-01-23T12:43:24.360 回答
0

我使用 graphics2d 来实现我的功能。此方法接收 2 个图像和 6 个字符串:Bufferdimage bi 是我的空白图像,在此图像上我添加对象(例如:图像,字符串):

    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 是带有所有其他对象的卡片,顺便说一句,我在字体中添加了 smothing,没有这个,文本非常令人不快。

于 2013-02-04T08:41:52.053 回答