Android 二维码扫描是很常用的工具,是不是很Cool,到底如何实现的呢,下面我们就来探讨一下Zxing的实现方法(底部附上下载链接):
首先
工程结构:
如何引用:(内容来自雪炭网SnowCoal.com)
一般来说,你就可以改改就用了,但如果你只想把它当成一个小小的子集加入项目,你需要将三个包Copy至你的项目中:(camera、decoding、view),然后引入相对应的资源进去,不要访记还有一个Jar包哦!(Zxing.jar)
关于布局:
com.example.qr_codescan包里面有一个MipcaActivityCapture,也是直接引入,这个Activity主要处理扫描界面的类,比如,扫描成功有声音和振动等等,主要关注里面的handleDecode(Result result, Bitmap barcode)方法,扫描完成之后将扫描到的结果和二维码的bitmap当初参数传递到handleDecode(Result result, Bitmap barcode)里面,我们只需要在里面写出相对应的处理代码即可,其他的地方都不用改得,这里处理扫描结果和扫描拍的照片.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/**
* 处理扫描结果
* @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();
}
|
对MipcaActivityCapture界面的布局做了自己的改动,先看下效果图,主要是用到FrameLayout,里面嵌套RelativeLayout。
//xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<FrameLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
>
<RelativeLayout
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
>
<SurfaceView
android:id=
"@+id/preview_view"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
android:layout_gravity=
"center"
/>
<com.mining.app.zxing.view.ViewfinderView
android:id=
"@+id/viewfinder_view"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
/>
<
include
android:id=
"@+id/include1"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:layout_alignParentTop=
"true"
layout=
"@layout/activity_title"
/>
</RelativeLayout>
</FrameLayout>
|
实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
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.ImageView;
import
android.widget.TextView;
public
class
MainActivity
extends
Activity {
private
final
static
int
SCANNIN_GREQUEST_CODE =
1
;
/**
* 显示扫描结果
*/
private
TextView mTextView ;
/**
* 显示扫描拍的图片
*/
private
ImageView mImageView;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.result);
mImageView = (ImageView) findViewById(R.id.qrcode_bitmap);
//点击按钮跳转到二维码扫描界面,这里用的是startActivityForResult跳转
//扫描完了之后调到该界面
Button mButton = (Button) findViewById(R.id.button1);
mButton.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
Intent intent =
new
Intent();
intent.setClass(MainActivity.
this
, MipcaActivityCapture.
class
);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent, SCANNIN_GREQUEST_CODE);
}
});
}
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
super
.onActivityResult(requestCode, resultCode, data);
switch
(requestCode) {
case
SCANNIN_GREQUEST_CODE:
if
(resultCode == RESULT_OK){
Bundle bundle = data.getExtras();
//显示扫描到的内容
mTextView.setText(bundle.getString(
"result"
));
//显示
mImageView.setImageBitmap((Bitmap) data.getParcelableExtra(
"bitmap"
));
}
break
;
}
}
}
|
..............................................
最终运行效果
更多代码详见源码:源码下载 QR_CodeScan.zip
好东西大家分享,如有需要源码的请至雪炭网下载
转载原文链接:Android 二维码扫描源码下载
。。。。。。。。。。。。。。。。。。。。。。。。。。。
更多内容推荐:雪炭工具 --- 二维码在线生成器