Android使用ZXing扫描二维码,并返回二维码结果
1.首先到https://github.com/zxing/zxing上下载zxing最新版本是3.3.1
2.解压zxing,我们主要使用下图所示的两个目录
3.把上图中的两个目录中的代码拷贝到你的Android项目中,下面是我的项目目录
4.把zxing中的layout目录中布局文件拷贝到你的项目对应目录中
5.下图是我的项目添加zxing后的结果
6.下图是从zxing中拷贝出来的布局文件
7.下来修改AndroidManifest.xml文件
加入一下代码开启权限
<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-permission android:name="android.permission.READ_CONTACTS" />
8.以上都OK了,但是zxing中的代码还是会提示R不存在,这个时候需要手动加入 import com.example.suyulin.scanqrcodetoweb.R即可,所有相关的文件都要修改。
9.新建一个Activity 代码如下,里面我加入了一个WebView
重写onActivityResult取得扫描结果
重写onDestroy是为了在关闭Activity是,释放WebVIew
onClick是按钮点击事件,用于打开摄像头开始扫描二维码。
public void onActivityResult(int requestCode, int resultCode, Intent intent)
protected void onDestroy()
public void onClick(View view)
WebActivity.java文件
package com.example.suzyulin.scanqrcodetoweb; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.TextView; import com.google.zxing.client.android.Intents; import com.google.zxing.client.android.history.HistoryItem; public class WebActivity extends Activity implements View.OnClickListener{ private static final int SCAN_CODE_REQUEST_CODE = 1; //内嵌网页 WebView mWebview; WebSettings mWebSettings; TextView beginLoading,endLoading,loading,mtitle,QRCodeResult; Button btnWeb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web); QRCodeResult =(TextView)findViewById(R.id.text_QRCodeResult) ;//为了放置扫描后的结果 btnWeb =(Button)findViewById(R.id.action_web) ; //内嵌网页 mWebview = (WebView) findViewById(R.id.webView1); //beginLoading = (TextView) findViewById(R.id.text_beginLoading); endLoading = (TextView) findViewById(R.id.text_endLoading); //loading = (TextView) findViewById(R.id.text_Loading); mtitle = (TextView) findViewById(R.id.title); mWebSettings = mWebview.getSettings(); mWebview.loadUrl("http://www.baidu.com/"); //设置不用系统浏览器打开,直接显示在当前Webview mWebview.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); //设置WebChromeClient类 mWebview.setWebChromeClient(new WebChromeClient() { //获取网站标题 @Override public void onReceivedTitle(WebView view, String title) { System.out.println("标题在这里"); // mtitle.setText(title); } //获取加载进度 @Override public void onProgressChanged(WebView view, int newProgress) { if (newProgress < 100) { String progress = newProgress + "%"; // loading.setText(progress); } else if (newProgress == 100) { String progress = newProgress + "%"; // loading.setText(progress); } } }); //设置WebViewClient类 mWebview.setWebViewClient(new WebViewClient() { //设置加载前的函数 @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { System.out.println("开始加载了"); //beginLoading.setText("开始加载了"); } //设置结束加载函数 @Override public void onPageFinished(WebView view, String url) { // endLoading.setText("结束加载了"); } }); } public void onClick(View view) { System.out.println("onClick按钮被点击*********************!"); Intent intent = null; switch (view.getId()){ case R.id.ButtonScan: System.out.println("按钮被点击!onClick"); intent = new Intent(this, com.google.zxing.client.android.CaptureActivity.class); startActivityForResult(intent, SCAN_CODE_REQUEST_CODE); break; default: break; } } public void BtnWebClick(View view) { System.out.print("BtnWebClick 按钮被点击*********************!"); switch (view.getId()){ case R.id.action_web: System.out.print("按钮被点击!BtnWebClick"); break; default: break; } } @Override protected void onDestroy() { //inactivityTimer.shutdown(); if (mWebview != null) { mWebview.loadDataWithBaseURL(null, "", "text/html", "utf-8", null); mWebview.clearHistory(); ((ViewGroup) mWebview.getParent()).removeView(mWebview); mWebview.destroy(); mWebview = null; } //super.onDestroy(); super.onDestroy(); } @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { if(requestCode == SCAN_CODE_REQUEST_CODE && resultCode == RESULT_OK) { System.out.println("WebActivity onActivityResult begin"); String result = intent.getExtras().getString("ResultQRCode");//intent.getStringExtra("ResultQRCode"); System.out.println("WebActivity onActivityResult end" + result); //处理 if (result==null){ System.out.println("result == null "); } else { System.out.println("result !== null "); QRCodeResult.setText(result); } if (result != null) { // QRCodeResult.setText(result); } } } }activity_web.xml文件内容
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="136dp"> <Button android:id="@+id/ButtonScan" android:layout_width="267dp" android:layout_height="55dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:background="@color/colorFlex" android:onClick="onClick" android:text="@string/action_ScanQRCode" /> <TextView android:id="@+id/text_QRCodeResult" android:layout_width="match_parent" android:layout_height="73dp" android:layout_alignParentBottom="true" android:layout_alignParentStart="true" android:layout_gravity="bottom|center_horizontal" android:background="@color/transparent" android:text="aaaaaannnnnn" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!--显示网页区域--> <WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_gravity="bottom" /> </RelativeLayout> </LinearLayout>10.现在开始修改CaptureActivity.java文件中handleDecode方法
public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor)加入一下代码
Intent result = getIntent(); //Intent result = new Intent(CaptureActivity.this,WebActivity.class); result.putExtra("ResultQRCode",rawResult.getText()); setResult(RESULT_OK,result); finish();加入CaptureActivity.java文件位置
if (fromLiveScan) { historyManager.addHistoryItem(rawResult, resultHandler); // Then not from history, so beep/vibrate and we have an image to draw on beepManager.playBeepSoundAndVibrate(); drawResultPoints(barcode, scaleFactor, rawResult); //这个是为了把扫描结果传输到调用界面,2017-11-10 Intent result = getIntent(); //Intent result = new Intent(CaptureActivity.this,WebActivity.class); result.putExtra("ResultQRCode",rawResult.getText()); setResult(RESULT_OK,result); finish(); }
以上都OK了,返回第9步在WebActivity.java中可以在onActivityResult中操作返回的扫描结果
public void onActivityResult(int requestCode, int resultCode, Intent intent)