Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-将多个字符串转换为图像格式_Java_String_Printing_Bytearray - Fatal编程技术网

Java-将多个字符串转换为图像格式

Java-将多个字符串转换为图像格式,java,string,printing,bytearray,Java,String,Printing,Bytearray,我正在寻找一种方法来重新排列作为字符串和一个字节[]图像接收的几个参数,并将它们放在一起作为一个图像(例如jpg)准备打印(immidatly) 例如: public static void printCard(String name, String LName, Image MainImage) 基本上,这个功能将是一个简单的卡片打印机。 我一直在寻找一个想法或一个可以指导我的人,如果有人能给我一些指导,这可能会很容易。这是一个简单的方法,我用来将文本添加到预先存在的图像上 我相信你能想出如

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

例如:

public static void printCard(String name, String LName, Image MainImage)
基本上,这个功能将是一个简单的卡片打印机。
我一直在寻找一个想法或一个可以指导我的人,如果有人能给我一些指导,这可能会很容易。

这是一个简单的方法,我用来将文本添加到预先存在的图像上

我相信你能想出如何传递一个空白图像,并添加你认为合适的其他行

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;
}

如果要直接从应用程序打印,可以使用
java.awt.print
package

试试这个方法

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
导入与打印相关的类。我使用graphics2d来实现我的函数。此方法接收2个图像和6个字符串: Bufferdimage bi是我的空白图像,在此图像上我添加了对象(例如:图像,字符串):

bi是所有其他对象的卡片
顺便说一句,我在字体中添加了smothing,如果没有它,文本将非常不愉快。

您正在寻找在图像上打印文本吗。如果是,这里是[1]。。[1] :我希望自己用固定大小和白色背景创建图像,然后添加其他参数above@Adir.el:这是一个讨论编程问题的论坛,不要求其他人为您实现软件。请再次阅读我的问题,看看我是否需要一位可以指导我的人。:)知道如何确定打印尺寸吗?@Adir.el您可以使用java.awt.print.PageFormat设置纸张尺寸。下面是一些有用的文档。你可以在谷歌上搜索更多的示例。我正在考虑这个解决方案,不久将尝试实现。我会更新你的
    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;
}