java连接打印机
驅動安裝
佳博打印機
在佳博官網下載驅動包,要和自己的機型匹配。我使用的是GP-3120TU打印機,驅動包如下:
點擊安裝,選擇GP-3120TU
設置打印機端口
使用連接線選擇usb端口,完成后可以打印空白頁進行測試。剛開始的時候因為找不到連接線,我使用筆記本藍牙進行連接,選用串口com4連接,也可以進行打印。
得力打印機
下載驅動,配置端口,安裝驅動。
后臺
pom.xml
<dependencies><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.52</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><!-- 谷歌二維碼--><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.0</version></dependency></dependencies>二維碼
public class MatrixToImageWriterWithLogo {//日志private static Logger log = Logger.getLogger(MatrixToImageWriterWithLogo.class);private static final int BLACK=0XFF000000;private static final int WHITE=0XFFFFFFFF;private MatrixToImageWriterWithLogo(){ }public static BufferedImage toBufferedImage(BitMatrix matrix){BufferedImage bi=new BufferedImage(matrix.getWidth(), matrix.getHeight(), BufferedImage.TYPE_INT_RGB);for(int i=0;i<matrix.getWidth();i++){for(int j=0;j<matrix.getHeight();j++){//有值的是黑色,沒有值是白色bi.setRGB(i, j,matrix.get(i, j)?BLACK:WHITE);}}return bi;}public static void MatrixToImage(BitMatrix matrix,String format,File f) {BufferedImage b = null;try {b = ImageIO.read(new File("C:\\Users\\Asus\\Desktop\\z.png"));Image image = b.getScaledInstance(10, 10, Image.SCALE_FAST);BufferedImage bi = toBufferedImage(matrix);//獲取二維碼畫刷Graphics g = bi.getGraphics();//定位g.drawImage(image, 250, 150, null);//二維碼畫到相應文件位置,結束。if (ImageIO.write(bi, format, f)) {}log.info("OK");} catch (IOException e) {log.error(e.getMessage());}}}生成的二維碼圖片:
打印實體類
測試類
public class UserTest {private static Logger log = Logger.getLogger(MatrixToImageWriterWithLogo.class); //日志private static final int printNum=1; //設置打印次數public static void PrintUser(String tit1, String con1, String tit2, String con2, BufferedImage image) {try {// 通俗理解就是書、文檔Book book = new Book();// 設置成豎打PageFormat pf = new PageFormat();pf.setOrientation(PageFormat.PORTRAIT);// 通過Paper設置頁面的空白邊距和可打印區域。必須與實際打印紙張大小相符。Paper paper = new Paper();paper.setSize(230, 85);// 紙張大小paper.setImageableArea(10, 3, 210, 75);pf.setPaper(paper);book.append(new UserTicket(tit1,con1,tit2,con2,image), pf);// 獲取打印服務對象PrinterJob job = PrinterJob.getPrinterJob();// 設置打印類job.setPageable(book);job.print();} catch (PrinterException e) {log.error(e.getMessage());}}public static void main(String[] args) {BufferedImage image =null;try {MultiFormatWriter mulwriter = new MultiFormatWriter();//寫入二維碼的內容,也可以是鏈接,xml,json字符串String content = "ihzsr";//對EncodeHintType的各種設置寫入一個map中HashMap<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();//設置我們放入的字符串編碼為utf-8hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//長寬,二維碼為QR碼BitMatrix bitmatrix= mulwriter.encode(content, BarcodeFormat.QR_CODE, 60, 60, hints);MatrixToImageWriterWithLogo.MatrixToImage(bitmatrix, "png", new File("D:\\z.png"));image = ImageIO.read(new FileInputStream(new File("D:\\z.png")));} catch (Exception e) {log.error(e.getMessage());}//打印for(int i=0;i<printNum;i++){PrintUser("資產編號:","HZSR156394562","資產名稱:","tjigh電腦主機",image);}} }總結
- 上一篇: 艾思瑞吡非尼酮在哪报销
- 下一篇: 基于javaweb的美发店理发店会员管理