android开发打印票据或文档的方法:android打印文档的类

最近在做个一个基于android平台的工厂ERP项目,需要用到android平板连接打印机打印文档的功能。经过在网上一顿狂搜之后,发现目前好像打印机对android的直接没有任何标准, 再不就是自家封的API,再不就是直接调用ESC/POS命令,通过调用打印函数来打印。 如果直接使用ESC/POS命令,这个到是通用,但是调用打印机指令进行走纸、扫点这个开发成本有点高。如果调用打印机的API,那么如果有N个项目,客户采购N种不同的打印机,我们就要实现N套打印程序。
       针对以上的情况,是否可以通过图片或者PDF等通用格式进行打印呢?好吧,我们使用android自带的xml编辑器来生成layout,在程序里通过layout给控件赋值,之后把layout生成view,转化成图片。再通过打印机提供的厂商提供的图片打印API来实现单据的打印。这个程序目前可以把xml转化为图片并保存到本地,由于没有真机,无法进行打印测试。但按照市场调研,目前市面的热敏打印机都支持图片打印。下面附上源码:
 
打印工具类
  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.  } 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值