在嵌入式Linux系統上安裝印表機__Linux

來源:互聯網
上載者:User

 

1.簡介:
  在Linux環境中安裝印表機,通常是cups, ghostscript等,但體積通常很大,幾十兆
在我應用的環境,要求列印模組不大於5M,在網上搜尋的方案是將cups的一部分提取出來,
即 imagetoraster,  rastertohp, rastertoepson.  經過精簡,最後我把後兩者合并,
去掉其他圖片格式支援,只保留BMP,並靜態編譯,產生兩個檔案imagetoraster, rastertoany,
總計1M,並在HP-1010上測試成功。由於我的工作比較簡單,所以沒有考慮列印任務管理的問題。
由於HP-Deskjet需要ijs驅動,用此方法失敗,用ghostscript測試成功,但比較大,15M左右,
所以不在這裡介紹了。

2.列印流程
  export PPD=/sa_user_data/up/laserjet.ppd
  /root/imagetoraster 10 "" "" 1 "" "/root/1.bmp" | /root/rastertoany hp 10 "" "" 1 "" | cat > /dev/usb/lp0

  執行上述語句,可以將1.bmp列印到我的印表機HP-1010 USB雷射印表機上,其中
  laserjet.ppd  印表機的驅動,可以從 linuxprinting.org上下載,不一定是1010,也許通用的就可以,比如我這裡就
                用的HP雷射通用驅動程式
  imagetoraster 負責將圖片轉換為 raster資料
  rastertoany   負責將raster資料轉換為印表機認識的資料
  cat           負責將列印資料輸出給印表機, 如果沒有裝置,就建立一個主裝置好180,從裝置號0的lp0
 
  imagetoraster 後面的參數分別為
                1:job_id
                2:user
                3:title
                4:Copies
                5:num_options
                6:filename
                前幾個都沒什麼用,就按我寫的就可以了
   rastertoepson 第一個參數為 hp 或  epson,表示用哪種印表機,後面的參數和imagetoraster一樣

3. CUPS裁剪
   平台 Linux-2.4 + gcc3.2.2 + cups-1.1.23
  
 3.1 編譯選項,需要的是靜態編譯,並且把沒用的去掉,可以寫一個指令碼:
   ./configure --prefix=/root/mycupsinstall /
               LDFLAGS=--static /
               --enable-static /
               --disable-shared /
               --disable-debug /
               --disable-mallinfo /
               --disable-slp /
               --disable-ssl /
               --disable-openssl /
               --disable-gnutls /
               --disable-cdsassl /
               --disable-pam /
               --without-cups-user /
               --without-cup-group /
               --without-fontpath /
               --without-docdir /
               --without-rcdir /
               --without-openslp-libs /
               --without-openslp-includes /
               --without-openssl-libs /
               --without-openssl-includes /
               --without-java /
               --without-perl /
               --without-php /
               --without-python

     要看全部選項,./configure --help 執行完後就會產生相應的Makefile了,先不著急Make,
   先修改cups目錄下config.h將#define HAVE_LIBZ 1 注掉,然後就開始修改原始碼了

 3.2 刪除列印資訊
     預設情況下,列印時輸出一堆資訊,諸如檢測到的圖片大小等資訊,我把他去掉。
   將filter下的檔案common.c, rastertohp, rastertoepson.c, image.c imagetoraster.c, image-bmp.c
   中的有stderr的地方都注掉。
 3.3 去除其他圖片格式,只保留BMP 
     刪除 filter/image.c中 ImageOpen函數中,檔案開啟部分,除bmp的東西都刪除,僅保留
     if (memcmp(header, "BM", 2) == 0)
         status = ImageReadBMP(img, fp, primary, secondary, saturation, hue, lut);
     else
     {
        fclose(fp);
        status = -1;
     }
    
     修改filter/Makefile
     在IMAGEOBJS = 中刪除 image-gif.o image-jpeg.o image-photocd.o image-pix.o image-png.o image-pnm.o /
                          image-sgi.o image-sgilib.o image-sun.o image-tiff.o /
 
 3.4 合并rastertohp  rastertoepson
      這兩個檔案開啟看看差不多,由於是靜態編譯,若寫成兩個檔案,每個都佔用400K,合并後大約還是400K。
    合并思路如下:
        將rastertohp和rastertoepson的main函數更名為普通函數
        建立一個rastertoany.c,裡面只有main函數,根據輸入的參數調用rastertohp或toepson
    filter/rastertoany.c:
       #include <string.h>
       extern int rastertohp ( int argc, char *argv[] );
       extern int rastertoepson ( int argc, char *argv[] );

       int main(int  argc, char *argv[])
       { 
        if ( strcmp ( argv[1], "hp" ) == 0 )
            return rastertohp ( argc-1, &argv[1] );
        else if ( strcmp ( argv[1], "epson" ) == 0 )
         return rastertoepson ( argc-1, &argv[1] );
        else
         return 0;
       }
    filter/rastertohp.c  
      將main函數更名rastertohp
      將下列函數更名,更名的目的是rastertoepson的這些函數同名,同一個工程不能有兩個同樣的函數
         Setup        -->  SetupHP
         StartPage    -->  StartPageHP
         EndPage      -->  EndPageHP
         Shutdown     -->  ShutdownHP
         CancelJob    -->  CancelJobHP
         CompressData -->  CompressDataHP
         OutputLine   -->  OutputLineHP

    filter/rastertoepson.c  
       將main函數更名rastertoepson
       由於rastertohp已經更名了,這裡就可以不改了。
      
    filter/Makefile
     FILTERS = 增加 rastertoany,刪除 rastertohp rastertoepson
     OBJS  = 增加 rastertoany.o
     增加一個輸出檔案,模仿rastertohp
     rastertoany: rastertoany.o rastertohp.o rastertoepson.o ../Makedefs ../cups/$(LIBCUPS) $(LIBCUPSIMAGE)
        echo Linking $@...
        $(CC) $(LDFLAGS) -o $@ rastertoany rastertohp.o rastertoepson.o $(LINKCUPSIMAGE) $(IMGLIBS) $(LIBS)

 3.5
   編譯,在cups目錄下make即可,

4. 結束語
  此方法可以解決部分印表機在linux下的簡單列印問題,本人只驗證了 HP-1010, 列印效果達到要求,實驗HP DeskJet 3420失敗。修改的原始碼,將來會上傳到csdn資源裡面。
  參考資料:
      www.linuxprinting.org               找ppd驅動
      www.cups.org                        bbs挺好的
      google + 嵌入式linux + 列印 + B超,  這是本文思路的發源地
     http://www.gccgle.com/book/samba/CUPS-printing.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.