最近在做个一个基于android平台的工厂ERP项目,需要用到android平板连接打印机打印文档的功能。经过在网上一顿狂搜之后,发现目前好像打印机对android的直接没有任何标准,
再不就是自家封的API,再不就是直接调用ESC/POS命令,通过调用打印函数来打印。
如果直接使用ESC/POS命令,这个到是通用,但是调用打印机指令进行走纸、扫点这个开发成本有点高。如果调用打印机的API,那么如果有N个项目,客户采购N种不同的打印机,我们就要实现N套打印程序。
针对以上的情况,是否可以通过图片或者PDF等通用格式进行打印呢?好吧,我们使用android自带的xml编辑器来生成layout,在程序里通过layout给控件赋值,之后把layout生成view,转化成图片。再通过打印机提供的厂商提供的图片打印API来实现单据的打印。这个程序目前可以把xml转化为图片并保存到本地,由于没有真机,无法进行打印测试。但按照市场调研,目前市面的热敏打印机都支持图片打印。下面附上源码:
打印工具类
- public class PrintUtil {
- private static final String TAG = "PrintUtil";
- public String SCREEN_SHOTS_LOCATION = Environment
- .getExternalStorageDirectory().getPath();
- /**
- * 对当前view进行截屏,并保存到SCREEN_SHOTS_LOCATION指定的目录下
- * @param view
- * @param name
- * @return
- * @throws Exception
- */
- public void takeScreenShot(View view, String name,TakeScreenShotCallBack callback) throws Exception{
- takeScreenShot(view,name,SCREEN_SHOTS_LOCATION,callback);
- }
- TakeScreenShotCallBack mCallback = new TakeScreenShotCallBack(){
- @Override
- public void excute(File file) {
- // TODO Auto-generated method stub
- }
- };
- /**
- * 把传入的view保存为图片
- * @param parent 事件触发视图,可以是button等
- * @param view 保存为图片的视图
- * @param isShow 是否显示展示视图
- * @return
- */
- public void shotViewAsImage(View parent,View view, boolean isShow){
- PopupWindow popupWindow = getPopuptWindow(view);
- if(isShow)
- popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
- File newFile = null;
- try {
- takeScreenShot(popupWindow.getContentView(),null,mCallback);
- } catch (Exception e) {
- Log.e(TAG, e.getMessage());
- }
- }
- /**
- * 创建popupWindow
- * @param popupWindow_view
- * @return
- */
- protected PopupWindow getPopuptWindow(View popupWindow_view) {
- PopupWindow popupWindow = null;
- popupWindow = new PopupWindow(popupWindow_view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);// 创建PopupWindow实例
- popupWindow.setBackgroundDrawable(new BitmapDrawable());
- popupWindow.setFocusable(true);
- popupWindow.setOutsideTouchable(false);
- return popupWindow;
- }
- public interface TakeScreenShotCallBack{
- public void excute(File file);
- }
- /**
- * 对当前view进行截屏,并保存到path指定的目录下
- * @param view
- * @param name
- * @param path
- * @return
- * @throws Exception
- */
- public void takeScreenShot(final View view, final String name,final String path,final TakeScreenShotCallBack callback) throws Exception {
- final Bitmap bitmap = convertViewToBitmap(view);
- Runnable runnable = new Runnable() {
- @Override
- public void run() {
- Canvas canvas = new Canvas(bitmap);
- int w = bitmap.getWidth();
- int h = bitmap.getHeight();
- Paint paint = new Paint();
- paint.setColor(Color.YELLOW);
- String n = null;
- if(null == name || "".equals(name)){
- SimpleDateFormat simple = new SimpleDateFormat("yyyyMMddhhmmss");
- n = simple.format(new Date());
- }else{
- n = name;
- }
- canvas.drawText("", w - w / 2, h - h / 10, paint);
- canvas.save();
- canvas.restore();
- FileOutputStream fos = null;
- File file = null;
- try {
- File sddir = new File(path);
- if (!sddir.exists()) {
- sddir.mkdirs();
- }
- //生成以时间为名称的文件
- file = new File(path + File.separator
- + n + ".png");
- fos = new FileOutputStream(file);
- if (fos != null) {
- bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
- fos.close();
- }
- } catch (Exception e) {
- Log.e(TAG, e.getMessage(),e);
- }
- callback.excute(file);
- }
- };
- Thread thread = new Thread(runnable);
- thread.start();
- }
- /**
- * 转换view为bitmap
- * @param view
- * @return
- */
- public Bitmap convertViewToBitmap(View view){
- view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
- view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
- view.buildDrawingCache();
- Bitmap bitmap = view.getDrawingCache();
- return bitmap;
- }
- private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- }
- 调用函数
- public void print(String data) {
- View view = LayoutInflater.from(this).inflate(R.layout.print_test,
- null, false);
- //这几句可以注释掉,是另一个条形码生成的方法
- /* ImageView bar_img = (ImageView) view
- .findViewById(R.id.print_img_barcode);
- try {
- bar_img.setImageBitmap(testCODE39(data));
- } catch (Exception e) {
- } */
- PrintUtil printer = new PrintUtil();
- printer.shotViewAsImage(btn_print, view, true);
- }