gpt4 book ai didi

java - 使用java打印条形码

转载 作者:太空宇宙 更新时间:2023-11-04 11:19:26 25 4
gpt4 key购买 nike

我正在尝试使用 servlet 打印条形码,但打印尺寸不正确。我想打印具有特定宽度和高度的标签。如果我使用第 3 方应用程序打印此条形码,它打印正确,但如果我使用我的代码,打印尺寸会变成两倍,使用多个标签。

package com.service;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.krysalis.barcode4j.impl.int2of5.Interleaved2Of5Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;

/**
*
* @author akash
*/
public class Barcode extends HttpServlet {
private String filePath = "/home/chimeralabs/public_html/uploads/";
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");

String id = request.getParameter("patientId");

Interleaved2Of5Bean bean = new Interleaved2Of5Bean();

bean.setHeight(10d);

bean.doQuietZone(false);

OutputStream out = new java.io.FileOutputStream(new File("Barcode_"+id+".png"));

BitmapCanvasProvider provider =
new BitmapCanvasProvider(out, "image/x-png", 510,
BufferedImage.TYPE_BYTE_GRAY, false,
0);
bean.generateBarcode(provider, id);

provider.finish();

BufferedImage barcodeImage = provider.getBufferedImage();
response.setContentType("image/x-png");
OutputStream outputStream = response.getOutputStream();
ImageIO.write(barcodeImage, "png", outputStream);
outputStream.close();
request.setAttribute("patientId", id);
RequestDispatcher rd = request.getRequestDispatcher("Print");
rd.forward(request, response);
}

}

enter image description here

import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Print extends HttpServlet {
private String filePath = "/home/chimeralabs/public_html/uploads/";

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));

String id = (String)request.getAttribute("patientId");

PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);

PrintService ps = pss[0];
System.out.println("Printing to " + ps);

DocPrintJob job = ps.createPrintJob();

FileInputStream fin = new FileInputStream("Barcode_1082.png");
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);

try {
job.print(doc, pras);
} catch (PrintException ex) {
Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, ex);
}

fin.close();
}
}

最佳答案

(由于我还不能发表评论,我想我必须将此作为答案?)

我对 Java 打印不太熟悉,但看起来您尝试打印的文件是 PNG,但 DocFlavor 设置为 GIF。好像有一个PNG DocFlavor。你尝试过吗?

此外,您可能想查看 this question这看起来很相似。

关于java - 使用java打印条形码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45088274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com