android 使用开源库zxing生成二维码,扫描二维码【转】

转自:http://blog.csdn.net/qq_16064871/article/details/52422723

zxing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的接口。可以实现使用手机的内置的摄像头完成条形码和二维码的扫描与解码。可以实现条形码和二维码的编码与解码。

github官网源码地址:https://github.com/zxing/zxing

开源库api文档:https://zxing.github.io/zxing/apidocs/

本篇博客使用zxing的demo下载地址:http://download.csdn.net/detail/qq_16064871/9620772

1、zxing目前支持的的格式如下


2、github官网开源库模块代码

 

测试代码

Java se javase-specific 客户机代码“条形码扫描器”

Android android 客户条形码扫描器

安卓测试应用,zx测试

androidtest android-core android-related 代码之间共享安卓,玻璃玻璃简单的谷歌眼镜的应用程序

zxing.appspot.com 基于web的条形码生成器在zxing.appspot.com背后的来源


3、跨平台接口

 

ZXing-based第三方开源项目

qzxing  端口Qt框架

zxing-cpp  接口c++(分叉的弃用官方c++端口)

zxing_cpp  rb绑定Ruby(不仅仅是JRuby),由zxing-cpp

Python-zxing python框架

zx 网络端口。NET和c#,和相关的Windows平台

PHP php-qrcode-detector-decoder港口

4、生成二维码的示例代码

void encode(String contents) {  

  •     int WIDTH = 300, HEIGHT = 300 ;  
  •     MultiFormatWriter formatWriter = new MultiFormatWriter();  
  •     try {  
  •         // 按照指定的宽度,高度和附加参数对字符串进行编码  
  •         BitMatrix bitMatrix = formatWriter.encode(contents, BarcodeFormat.QR_CODE, WIDTH, HEIGHT/*, hints*/);         
  •         Bitmap bitmap=StringUtil.bitMatrix2Bitmap(bitMatrix);     
  •         Intent intent = new Intent(this, SurveyPointShowQrCodeActivity.class);  
  •         ByteArrayOutputStream bOutputStream = new ByteArrayOutputStream();  
  •         bitmap.compress(Bitmap.CompressFormat.PNG, 100, bOutputStream);  
  •         byte[] bytes = bOutputStream.toByteArray();  
  •         intent.putExtra("bitmap", bytes);  
  •         startActivity(intent);  
  •     } catch (WriterException e) {  
  •         e.printStackTrace();  
  •     } catch (Exception e) {  
  •         e.printStackTrace();  
  •     }  
  • }  


  1. private Bitmap bitMatrix2Bitmap(BitMatrix matrix) {    
  2.       int w = matrix.getWidth();    
  3.       int h = matrix.getHeight();    
  4.       int[] rawData = new int[w * h];    
  5.       for (int i = 0; i < w; i++) {    
  6.           for (int j = 0; j < h; j++) {    
  7.               int color = Color.WHITE;    
  8.               if (matrix.get(i, j)) {    
  9.                   color = Color.BLACK;    
  10.               }    
  11.               rawData[i + (j * w)] = color;    
  12.           }    
  13.       }    
  14.   
  15.       Bitmap bitmap = Bitmap.createBitmap(w, h, Config.RGB_565);    
  16.       bitmap.setPixels(rawData, 0, w, 0, 0, w, h);    
  17.       return bitmap;    
  18.   }    

5、扫描生成处理示例代码

  •  * 处理扫描结果 
  •  * @param result 
  •  * @param barcode 
  •  */  
  • public void handleDecode(Result result, Bitmap barcode) {  
  •     inactivityTimer.onActivity();  
  •     playBeepSoundAndVibrate();  
  •     String resultString = result.getText();  
  •     if (resultString.equals("")) {  
  •         Toast.makeText(MipcaActivityCapture.this, "Scan failed!", Toast.LENGTH_SHORT).show();  
  •     }else {  
  •         Intent resultIntent = new Intent();  
  •         Bundle bundle = new Bundle();  
  •         bundle.putString("result", resultString);  
  •         bundle.putParcelable("bitmap", barcode);  
  •         resultIntent.putExtras(bundle);  
  •         this.setResult(RESULT_OK, resultIntent);  
  •     }  
  •     MipcaActivityCapture.this.finish();  
  • }  

6、相关的权限

"android.permission.VIBRATE" />  

  • <uses-permission android:name="android.permission.CAMERA" />  
  •   
  • <uses-feature android:name="android.hardware.camera" />  
  • <uses-feature android:name="android.hardware.camera.autofocus" />  
  1. <com.mining.app.zxing.view.ViewfinderView  
  2.     android:id="@+id/viewfinder_view"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content" />  


github官网源码地址:https://github.com/zxing/zxing

开源库api文档:https://zxing.github.io/zxing/apidocs/

本篇博客使用zxing的demo下载地址:http://download.csdn.net/detail/qq_16064871/9620772

原文地址:https://www.cnblogs.com/sky-heaven/p/7216480.html