Android蓝牙打印二维码打印外卖单打印

公司在做一个类似美团,饿了么的平台, 所以就会涉及小票打印, 自己懒也难得自己重头开始研究, 就去网上各种找demo,教程之类的. 但是大都是半成品, 只能简单打印一下文字,要么就是没有完整的demo来完美的解决需求. 自己折腾了一阵子,看了很多博客, 自己总结了一下,弄了一个自己觉得比较完整的DEMO. 大部分代码都不是自己写的啦, 写这个博客也是为了希望能帮到跟我一样正在奋斗的小码农~~

废话不多说,先看看效果:
这里写图片描述
demo截图~
这里写图片描述
附上源码及安装包下载地址:
https://github.com/yuchong123/BluetoothPrint
http://download.csdn.net/detail/yuchong1234/9375436

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
要在 Android 中生成二维码图片并打印,你可以使用以下步骤: 1. 添加依赖项:在 build.gradle 文件中添加以下依赖项: ``` implementation 'com.google.zxing:core:3.4.0' ``` 2. 生成二维码:使用以下代码生成二维码图片 ```java String content = "your_content_here"; int width = 500; int height = 500; QRCodeWriter writer = new QRCodeWriter(); try { BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height); int bitmapWidth = bitMatrix.getWidth(); int bitmapHeight = bitMatrix.getHeight(); Bitmap bmp = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.RGB_565); for (int x = 0; x < bitmapWidth; x++) { for (int y = 0; y < bitmapHeight; y++) { bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE); } } // bmp 即为生成的二维码图片 } catch (WriterException e) { e.printStackTrace(); } ``` 3. 打印二维码:将生成的二维码图片通过打印打印出来。你可以使用 Android打印机 API 来实现打印功能,或者使用第三方打印库。 示例代码: ```java // 获取打印服务 PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE); // 创建打印任务 PrintDocumentAdapter adapter = new PrintDocumentAdapter() { @Override public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) { try { OutputStream out = new FileOutputStream(destination.getFileDescriptor()); bmp.compress(Bitmap.CompressFormat.PNG, 100, out); callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES}); out.close(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) { callback.onLayoutFinished(new PrintDocumentInfo.Builder("print_output.pdf").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build(), true); } }; // 执行打印任务 String jobName = "二维码打印任务"; printManager.print(jobName, adapter, null); ``` 注意:以上代码仅供参考,实际使用中需要根据具体需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值