android 生成二维码图片

转自:http://www.maxiaoguo.com/shipin/235.html

zxing是google的一个开源二维码项目,目前基本上和二维码打交道的东西,都会用到它. 最近项目中用到了android手机需要根据提供的字符串生成二维码图片,之前用zxing做过二维码解码,编码还没做过,看了一些demo都是用到了zxing的j2se包的内容,这个在android或者其他平台上显然无法实现,所以我们要利用zxing生成二维矩阵,然后根据android平台的提供的api来生成图片.

[java]  view plain copy
  1. /** 
  2.      * 用字符串生成二维码 
  3.      * @param str 
  4.      * @author zhouzhe@lenovo-cw.com 
  5.      * @return 
  6.      * @throws WriterException 
  7.      */  
  8.     public Bitmap Create2DCode(String str) throws WriterException {  
  9.         //生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败  
  10.         BitMatrix matrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, 300300);  
  11.         int width = matrix.getWidth();  
  12.         int height = matrix.getHeight();  
  13.         //二维矩阵转为一维像素数组,也就是一直横着排了  
  14.         int[] pixels = new int[width * height];  
  15.         for (int y = 0; y < height; y++) {  
  16.             for (int x = 0; x < width; x++) {  
  17.                 if(matrix.get(x, y)){  
  18.                     pixels[y * width + x] = 0xff000000;  
  19.                 }  
  20.                   
  21.             }  
  22.         }  
  23.           
  24.         Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);  
  25.         //通过像素数组生成bitmap,具体参考api  
  26.         bitmap.setPixels(pixels, 0, width, 00, width, height);  
  27.         return bitmap;  
  28.     }  


以下是运行结果,这里最好让背景是白色才能看出效果,因为遍历矩阵以后,是把有标记的点描黑

要在 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); ``` 注意:以上代码仅供参考,实际使用中需要根据具体需求进行调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值