最近在做个一个基于android平台的工厂ERP项目,需要用到android平板连接打印机打印文档的功能。经过在网上一顿狂搜之后,发现目前好像打印机对android的直接没有任何标准, 再不就是自家封的API,再不就是直接调用ESC/POS命令,通过调用打印函数来打印。 如果直接使用ESC/POS命令,这个到是通用,但是调用打印机指令进行走纸、扫点这个开发成本有点高。如果调用打印机的API,那么如果有N个项目,客户采购N种不同的打印机,我们就要实现N套打印程序。        针对以上的情况,是否可以通过图片或者PDF等通用格式进行打印呢?好吧,我们使用android自带的xml编辑器来生成layout,在程序里通过layout给控件赋值,之后把layout生成view,转化成图片。再通过打印机提供的厂商提供的图片打印API来实现单据的打印。这个程序目前可以把xml转化为图片并保存到本地,由于没有真机,无法进行打印测试。但按照市场调研,目前市面的热敏打印机都支持图片打印。下面附上源码:   打印工具类 [java] view plain copy
  1. public class PrintUtil {  
  2.    
  3.  private static final String TAG = "PrintUtil";  
  4.    
  5.  public String SCREEN_SHOTS_LOCATION = Environment  
  6.    .getExternalStorageDirectory().getPath();  
  7.  /** 
  8.   * 对当前view进行截屏,并保存到SCREEN_SHOTS_LOCATION指定的目录下 
  9.   * @param view 
  10.   * @param name 
  11.   * @return 
  12.   * @throws Exception 
  13.   */  
  14.  public void takeScreenShot(View view, String name,TakeScreenShotCallBack callback) throws Exception{  
  15.   takeScreenShot(view,name,SCREEN_SHOTS_LOCATION,callback);  
  16.  }  
  17.    
  18.  TakeScreenShotCallBack mCallback = new TakeScreenShotCallBack(){  
  19.   @Override  
  20.   public void excute(File file) {  
  21.    // TODO Auto-generated method stub  
  22.      
  23.   }  
  24.     
  25.  };  
  26.    
  27.    
  28.  /** 
  29.   * 把传入的view保存为图片 
  30.   * @param parent 事件触发视图,可以是button等 
  31.   * @param view 保存为图片的视图 
  32.   * @param isShow 是否显示展示视图 
  33.   * @return 
  34.   */  
  35.  public void shotViewAsImage(View parent,View view, boolean isShow){  
  36.   PopupWindow popupWindow = getPopuptWindow(view);  
  37.   if(isShow)  
  38.    popupWindow.showAtLocation(parent, Gravity.CENTER, 00);  
  39.   File newFile = null;  
  40.   try {  
  41.    takeScreenShot(popupWindow.getContentView(),null,mCallback);  
  42.   } catch (Exception e) {  
  43.    Log.e(TAG, e.getMessage());  
  44.   }  
  45.  }  
  46.    
  47.  /** 
  48.   * 创建popupWindow 
  49.   * @param popupWindow_view 
  50.   * @return 
  51.   */  
  52.  protected PopupWindow getPopuptWindow(View popupWindow_view) {  
  53.   PopupWindow popupWindow = null;  
  54.   popupWindow = new PopupWindow(popupWindow_view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);// 创建PopupWindow实例  
  55.   popupWindow.setBackgroundDrawable(new BitmapDrawable());  
  56.   popupWindow.setFocusable(true);  
  57.   popupWindow.setOutsideTouchable(false);  
  58.   return popupWindow;  
  59.  }  
  60.    
  61.  public interface TakeScreenShotCallBack{  
  62.   public void excute(File file);  
  63.  }  
  64.    
  65.  /** 
  66.   * 对当前view进行截屏,并保存到path指定的目录下 
  67.   * @param view 
  68.   * @param name 
  69.   * @param path 
  70.   * @return 
  71.   * @throws Exception 
  72.   */  
  73.  public void takeScreenShot(final View view, final String name,final String path,final TakeScreenShotCallBack callback) throws Exception {  
  74.     
  75.     
  76.     
  77.   final Bitmap bitmap = convertViewToBitmap(view);  
  78.   Runnable runnable = new Runnable() {  
  79.    @Override  
  80.    public void run() {  
  81.       
  82.     Canvas canvas = new Canvas(bitmap);  
  83.     int w = bitmap.getWidth();  
  84.     int h = bitmap.getHeight();  
  85.     Paint paint = new Paint();  
  86.     paint.setColor(Color.YELLOW);  
  87.     String n = null;  
  88.     if(null == name || "".equals(name)){  
  89.      SimpleDateFormat simple = new SimpleDateFormat("yyyyMMddhhmmss");  
  90.      n = simple.format(new Date());  
  91.     }else{  
  92.      n = name;  
  93.     }  
  94.     canvas.drawText("", w - w / 2, h - h / 10, paint);  
  95.     canvas.save();  
  96.     canvas.restore();  
  97.     FileOutputStream fos = null;  
  98.     File file = null;  
  99.     try {  
  100.      File sddir = new File(path);  
  101.      if (!sddir.exists()) {  
  102.       sddir.mkdirs();  
  103.      }  
  104.      //生成以时间为名称的文件  
  105.      file = new File(path + File.separator  
  106.        + n + ".png");  
  107.      fos = new FileOutputStream(file);  
  108.      if (fos != null) {  
  109.       bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);  
  110.       fos.close();  
  111.      }  
  112.     } catch (Exception e) {  
  113.      Log.e(TAG, e.getMessage(),e);  
  114.     }  
  115.     callback.excute(file);  
  116.    }  
  117.   };  
  118.   Thread thread = new Thread(runnable);  
  119.   thread.start();  
  120.     
  121.  }  
  122.    
  123.  /** 
  124.   * 转换view为bitmap 
  125.   * @param view 
  126.   * @return 
  127.   */  
  128.  public Bitmap convertViewToBitmap(View view){  
  129.   view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));  
  130.   view.layout(00, view.getMeasuredWidth(), view.getMeasuredHeight());  
  131.   view.buildDrawingCache();  
  132.   Bitmap bitmap = view.getDrawingCache();  
  133.   return bitmap;  
  134.  }  
  135.  private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
  136. }  
  137.    
  138. 调用函数  
  139.    
  140. public void print(String data) {  
  141.   View view = LayoutInflater.from(this).inflate(R.layout.print_test,  
  142.     nullfalse);  
  143. //这几句可以注释掉,是另一个条形码生成的方法  
  144.   /* ImageView bar_img = (ImageView) view 
  145.     .findViewById(R.id.print_img_barcode); 
  146.   try { 
  147.    bar_img.setImageBitmap(testCODE39(data)); 
  148.   } catch (Exception e) { 
  149.   } */  
  150.   PrintUtil printer = new PrintUtil();  
  151.   printer.shotViewAsImage(btn_print, view, true);  
  152.  } 

更多相关文章

  1. 实现Android支付宝声波支付时的波纹视图
  2. Android 高级编程读书笔记 视图View定制与拓展
  3. 【Android 内存优化】Bitmap 内存占用计算 ( Bitmap 图片内存占
  4. 转载——Android大图片裁剪终极解决方案
  5. Android ListView 图片异步加载和图片内存缓存
  6. Android进阶——Android控制端连接同一网段Wi-Fi家用打印机小结
  7. 一起写一个Android图片轮播控件

随机推荐

  1. 大量正规厂商进驻Android Market,请国人务
  2. android 图片占用进程的内存算法
  3. Android 修炼之路
  4. Android Zip文件解压缩代码
  5. Android之布局属性重点
  6. 用Android Studio 运行ndk 程序
  7. Android Gradle 插件
  8. Android的webview做web应用研究
  9. Android 教程之实现动作感应技术
  10. Android Fresco图片加载库基础使用详解