频道栏目
首页 > 资讯 > Android > 正文

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

16-09-05        来源:[db:作者]  
收藏   我要投稿

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

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

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

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

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

 

核心核心图像解码库和测试代码

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

android android 客户条形码扫描器

androidtest 安卓测试应用,zx测试

android-integration 支持集成通过意图与条形码扫描器

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

zxingorg zxing.org 源码

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、生成二维码的示例代码

 

	public  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();
		}
	}

	  private Bitmap bitMatrix2Bitmap(BitMatrix matrix) {  
	        int w = matrix.getWidth();  
	        int h = matrix.getHeight();  
	        int[] rawData = new int[w * h];  
	        for (int i = 0; i < w; i++) {  
	            for (int j = 0; j < h; j++) {  
	                int color = Color.WHITE;  
	                if (matrix.get(i, j)) {  
	                    color = Color.BLACK;  
	                }  
	                rawData[i + (j * w)] = color;  
	            }  
	        }  
	  
	        Bitmap bitmap = Bitmap.createBitmap(w, h, Config.RGB_565);  
	        bitmap.setPixels(rawData, 0, w, 0, 0, w, h);  
	        return bitmap;  
	    }  

 

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、相关的权限

 

 

    
    
    
    
    

 

7、相关的xml

 

        

 

 

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

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

相关TAG标签
上一篇:iOS开发的前端之路
下一篇:Android -- Wifi热点的打开与关闭流程简介
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站