Android高級控件(三)—— 使用Google ZXing實現二維碼的掃描和生成相關功能體系
摘要
如今的二維碼可謂是爛大街了。到處都是二維碼。什么都是二維碼,掃一掃似乎已經流行到習以為常了,今天我們也來實現下面二維碼的相關功能,我們使用到的是Google開源的Zxing項目
Zxing GitHub:https://github.com/zxing/zxing
這個項目非常大,亂七八糟的,我們還是直接使用jar包吧,這里感謝一下醫生,他為我們封裝了一個3.1的jar,我們能夠拿來用:https://github.com/xuyisheng/ZXingLib。可是我還是認為依賴好用,本文也是用依賴做的,這里也提供一個依賴,下載地址:http://download.csdn.net/detail/qq_26787115/9434734,話不多說,我們把這個依賴導入到我們的Eclipse中去。然后再新建一個project——ZXing
好的,我們如今右鍵項目,選擇Properties——Android——add——ZXingLibrary
准備工作做好了之后,我們就能夠來實現了
二維碼掃描
1.權限
這可是非常重要的喲,我們須要兩個權限
<!-- 相機 -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- 振動 -->
<uses-permission android:name="android.permission.VIBRATE" />
2.相關類
這里看了醫生的介紹,我也就按部就班的寫過來了。是依賴里面的類。你也能夠自己去翻看一下依賴。ZXing畢竟太龐大了,所以這個是提取出來的Android二維碼的相關核心類
CaptureActivity
ZXing暴露的調用Activity。在handleDecode方法中對掃碼成功后的動作作處理。
ViewfinderView
ZXing掃碼窗體的繪制,原始的ZXing使用這種方式去繪制,在上面提供的開源庫中,作者將掃描框的繪制直接抽取到了XML文件里。這樣改動起來更加方便了。
CameraConfigurationManager
改動橫豎屏、處理變形效果的核心類。
我們掃描就是要用到這個CaptureActivity類,我們既然依賴了。那么我們也是能夠拿來直接用,這里我們能夠直接把依賴里面的關於CaptureActivity類的AndroidManifest.xml的注冊信息拷貝過來放在我們這個項目中
<activity android:configChanges="orientation|keyboardHidden" android:name="com.zxing.activity.CaptureActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateAlwaysHidden" >
</activity>
好的。這些我們都寫完了之后。我們就開始我們的邏輯處理了,非常的簡單
3.實現掃描
我們在activity_main.xml中聲明一個Button
<Button
android:id="@+id/btnSan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="掃描二維碼" />
我們把他初始化之后在他的點擊事件里面加入這樣一行代碼
startActivity(new Intent(this, CaptureActivity.class));
就能夠調用依賴里面的掃描功能了,我們執行一下試試效果,這里就須要使用真機了
是不是感覺非常6?可是你非常快就會發現。你掃描之后振動了一下之后什么都沒有。這里我們既要在做一些其它操作了,這里我們在activity_main.xml中新建一個TextView讓他顯示掃描的東西
<TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" />
好的,既然我們要接受返回值。那startIntent也是須要更換了,換成我們的startActivityForResult,這里我們也就要重寫我們的onActivityResult方法了
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
String result = data.getExtras().getString("result");
tv_content.setText(result);
}
}
如今我們執行一下看看效果,我們隨便掃描一張二維碼。這里我掃描了一下我自己的微信二維碼,看看
就是這么吊
生成二維碼
二維碼生成起來,我們須要三個元素,要生成的內容,生成的button,生成內容的存放,所以我們layou_main.xml里面要加入這種
<EditText android:id="@+id/et_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入要生成的二維碼文字" />
<Button android:id="@+id/btn_generate" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="生成二維碼" />
<RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" >
<ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</RelativeLayout>
我們把這幾個控件都初始化一下。然后在Button的點擊事件中寫
case R.id.btnGenerate:
String str = et_input.getText().toString();
if (str.equals("")) {
Toast.makeText(this, "不能為空", Toast.LENGTH_SHORT).show();
} else {
// 位圖
try {
/** * 參數:1.文本 2.二維碼的寬高(正方形) */
Bitmap bitmap = EncodingHandler.createQRCode(str, 500);
// 設置圖片
img.setImageBitmap(bitmap);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
我們來執行一下,非常奇妙的一幕發生了
OK。是不是認為非常的簡單?沒錯,就是這么的簡單,你也能夠試試
完整代碼
layout_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<Button android:id="@+id/btnSan" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="掃描二維碼" />
<TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<EditText android:id="@+id/et_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入要生成的二維碼文字" />
<Button android:id="@+id/btnGenerate" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="生成二維碼" />
<RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" >
<ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
MainActivity
package com.lgl.zxing;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.zxing.activity.CaptureActivity;
import com.zxing.encoding.EncodingHandler;
public class MainActivity extends Activity implements OnClickListener {
private Button btnSan, btnGenerate;
private TextView tv_content;
private EditText et_input;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSan = (Button) findViewById(R.id.btnSan);
btnSan.setOnClickListener(this);
tv_content = (TextView) findViewById(R.id.tv_content);
btnGenerate = (Button) findViewById(R.id.btnGenerate);
btnGenerate.setOnClickListener(this);
et_input = (EditText) findViewById(R.id.et_input);
img = (ImageView) findViewById(R.id.img);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSan:
Intent intent = new Intent(this, CaptureActivity.class);
startActivityForResult(intent, 0);
break;
case R.id.btnGenerate:
String str = et_input.getText().toString();
if (str.equals("")) {
Toast.makeText(this, "不能為空", Toast.LENGTH_SHORT).show();
} else {
// 位圖
try {
/** * 參數:1.文本 2.二維碼的寬高(正方形) */
Bitmap bitmap = EncodingHandler.createQRCode(str, 500);
// 設置圖片
img.setImageBitmap(bitmap);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
String result = data.getExtras().getString("result");
tv_content.setText(result);
}
}
}