androidstudio 连接打印机

来源:互联网 发布:阿里云域名无法使用 编辑:程序博客网 时间:2024/05/16 18:53

我这里连接的是打印机,不是小票机,大家看清楚了,目前我连接的是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了。

原创粉丝点击