0

文字列と1バイト[]の画像として受け取ったいくつかの引数を再配置し、それらすべてを1つの画像(たとえば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.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

于 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 に答える