Android项目实战(五十四):zxing 生成二维码图片去除白色内边距的解决方案
目录:zxing->encoding->EncodingHandler类 中修改 createQRCode方法
private static final int BLACK = 0xff000000; private static final int WHITE = 0xffffffff; public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException { String contentsToEncode = str; if (contentsToEncode == null) { return null; } Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class); //hints.put(EncodeHintType.CHARACTER_SET, encoding); hints.put(EncodeHintType.MARGIN, 0); /* default = 4 */ MultiFormatWriter writer = new MultiFormatWriter(); BitMatrix result; try { result = writer.encode(contentsToEncode, BarcodeFormat.QR_CODE , widthAndHeight, widthAndHeight, hints); } catch (Exception e) { // Unsupported format e.printStackTrace(); return null; } int width = result.getWidth(); int height = result.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? BLACK : Color.WHITE; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
作者:听着music睡
出处:http://www.cnblogs.com/xqxacm/
Android交流群:38197636
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
2017-04-01 自定义控件详解(二):Path类 相关用法