androidstudio 连接打印机

我这里连接的是打印机,不是小票机,大家看清楚了,目前我连接的是Hp 1112这个是可以的。


首先 要下载hp的插件,android设备和打印机相连,android4.4以后,当然也可以修改底层,这个我不会,所以在只能在表层做一点,下载hp插件:Hp Print Service插件(这个在360手机助手里面有)下载安装就可以在设置里面的打印看到HP Inc. 就是它了。就不管了,进行下面操作。


打印三种:webview,bitmap,自定义。你自己看了webview和bitmap都是十分简单的,几句话就ok,但是自定义这个就比较麻烦。是不是在想自定义可以把view转换成bitmap。代码:

private Bitmap getViewBitmap(View v) {
        v.clearFocus();
        v.setPressed(false);

        boolean willNotCache = v.willNotCacheDrawing();
        v.setWillNotCacheDrawing(false);

        // Reset the drawing cache background color to fully transparent
        // for the duration of this operation
        int color = v.getDrawingCacheBackgroundColor();
        v.setDrawingCacheBackgroundColor(0);


        if (color != 0) {
            v.destroyDrawingCache();
        }
        v.buildDrawingCache();
        Bitmap cacheBitmap = v.getDrawingCache();
        if (cacheBitmap == null) {
            Log.e("Folder", "failed getViewBitmap(" + v + ")", new RuntimeException());
            return null;
        }

        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

        // Restore the view
        v.destroyDrawingCache();
        v.setWillNotCacheDrawing(willNotCache);
        v.setDrawingCacheBackgroundColor(color);

        return bitmap;
    }

这里有问题就是:如果背景是有颜色的就十分耗墨,节约成本的原则上是自定义最好 想打印什么就打印什么,当然你可以选择黑白的。

打印bitmap:

PrintHelper printHelper = new PrintHelper(this);
Bitmap bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();

printHelper.printBitmap("Print Bitmap", bitmap);


打印webview:

PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
printManager.print("Print  webview",mWebView.createPrintDocumentAdapter(), null);


自定义这个我就不说了,比较麻烦。我给代码http://download.csdn.net/detail/u012254541/9884352

运行时候就会出现HP的插件,就ok了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值