在嵌入式Linux系统上安装打印机

refer from http://blog.csdn.net/ma100/article/details/4243241


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

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值