日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java连接打印机

發布時間:2023/12/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 UserTicket implements Printable {private String tit1;private String con1;private String tit2;private String con2;private BufferedImage image;private Font font;public UserTicket(String tit1, String con1, String tit2, String con2, BufferedImage image) {super();this.tit1 = tit1;this.con1 = con1;this.tit2 = tit2;this.con2 = con2;this.image = image;}public int print(Graphics graphics, PageFormat pageFormat, int pageIndex){int right = 115; //右邊一張距離左邊的距離int first_second = 0; //第一行與第二行的距離// 轉換成Graphics2D 拿到畫筆Graphics2D g2 = (Graphics2D) graphics;// 設置打印顏色為黑色g2.setColor(Color.black);// 打印起點坐標double x = pageFormat.getImageableX();double y = pageFormat.getImageableY();// 設置打印字體(字體名稱、樣式和點大小)(字體名稱可以是物理或者邏輯名稱)font = new Font("宋體", Font.BOLD, 10);g2.setFont(font);// 設置字體float heigth = font.getSize2D();// 字體高度// 標題 第一行g2.drawString("杭州公司資產", (float) x+11, (float) y + heigth+5);g2.drawImage(image,null,0,20);float line = 2 * heigth + first_second;g2.scale(1, 1.5); //對接下來的內容(字體)進行拉高font = new Font("宋體", Font.BOLD, 5);g2.setFont(font);// 設置字體heigth = font.getSize2D();// 字體高度// 第二行g2.drawString(tit1, (float) x+50, (float) y + line+5);line += 1.3 * heigth;g2.drawString(con1, (float) x + 50, (float) y + line+5);line += 1.3 * heigth;font = new Font("宋體", Font.BOLD, 5);g2.setFont(font);// 設置字體heigth = font.getSize2D();// 字體高度// 第二行:地址g2.drawString(tit2, (float) x+50, (float) y + line+5);line += 1.3 * heigth;g2.drawString(con2, (float) x + 50, (float) y + line+5);/*line += heigth;g2.scale(1, 0.67); //將比例還原// 第三行:廣告語font = new Font("宋體", Font.BOLD, 5);g2.setFont(font);// 設置字體g2.drawString("杭州思軟信息技術有限公司", (float) x, (float)y+line+30);*/switch (pageIndex) {case 0:return PAGE_EXISTS;default:return NO_SUCH_PAGE;}} }

測試類

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

總結

以上是生活随笔為你收集整理的java连接打印机的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。