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

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 客户条形码扫描器

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

    <uses-permission android:name="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" />

7、相关的xml

        <com.mining.app.zxing.view.ViewfinderView
            android:id="@+id/viewfinder_view"
            android:layout_width="wrap_content"
            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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mmsx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值