1,导入zxingdecode.jar和core-3.0.0.jar(google二维码扫描和生成所需jar),同步库
2,添加权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
3,复制扫描界面布局文件和图片资源
布局文件activity_capture.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/capture_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:id="@+id/capture_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/capture_mask_top"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_alignParentTop="true"
android:background="@drawable/shadow" />
<RelativeLayout
android:id="@+id/capture_crop_view"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@id/capture_mask_top"
android:layout_centerHorizontal="true"
android:background="@drawable/qr_code_bg">
<ImageView
android:id="@+id/capture_scan_line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:src="@drawable/scan_line" />
</RelativeLayout>
<ImageView
android:id="@+id/capture_mask_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@id/capture_crop_view"
android:background="@drawable/shadow" />
<ImageView
android:id="@+id/capture_mask_left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="@id/capture_mask_bottom"
android:layout_alignParentLeft="true"
android:layout_below="@id/capture_mask_top"
android:layout_toLeftOf="@id/capture_crop_view"
android:background="@drawable/shadow" />
<ImageView
android:id="@+id/capture_mask_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="@id/capture_mask_bottom"
android:layout_alignParentRight="true"
android:layout_below="@id/capture_mask_top"
android:layout_toRightOf="@id/capture_crop_view"
android:background="@drawable/shadow" />
</RelativeLayout>
</RelativeLayout>
4,扫描二维码页面:QRActivity.java,复制过去也可以
package com.fff.qrdemo9;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.rbj.zxing.decode.QrcodeDecode;
public class QRActivity extends Activity {
private SurfaceView scanPreview;//相机
private RelativeLayout scanCropView;//扫描框
private ImageView scanLine;//扫描框中间的线
private QrcodeDecode qd;
private TextView mResultText;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_capture);
scanPreview = (SurfaceView) findViewById(R.id.capture_preview);
scanCropView = (RelativeLayout) findViewById(R.id.capture_crop_view);
scanLine = (ImageView) findViewById(R.id.capture_scan_line);
qd = new QrcodeDecode(this, scanPreview, scanCropView) {
@Override
public void handleDecode(Bundle bundle) {
//扫描成功后调用
String QRtext=bundle.getString(QrcodeDecode.BARCODE_RESULT);
//数据是使用Intent返回
Intent intent = new Intent();
//把返回数据存入Intent
intent.putExtra("result", QRtext);
//设置返回数据
QRActivity.this.setResult(RESULT_OK, intent);
//关闭Activity
QRActivity.this.finish();
}
};
TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT,
0.9f);
animation.setDuration(4500);
animation.setRepeatCount(-1);
animation.setRepeatMode(Animation.RESTART);
scanLine.startAnimation(animation);
}
@Override
protected void onResume() {
super.onResume();
//在此处开起扫描
qd.onResume();
}
@Override
protected void onPause() {
//
qd.onPause();
super.onPause();
}
@Override
protected void onDestroy() {
//释放资源
qd.onDestroy();
super.onDestroy();
}
}
记得注册一下QRActivity,不然会闪退
<activity android:name=".QRActivity"/>
5,测试调用
布局文件activity_main,一个按钮,一个展示扫码结果
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_qrcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="开始扫码" />
<TextView
android:id="@+id/txt_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="扫描结果" />
</LinearLayout>
MainActivity.java通过重写onActivityResult得到扫码结果
package com.fff.qrdemo9;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.btn_qrcode);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//得到新打开Activity关闭后返回的数据
//第二个参数为请求码,可以根据业务需求自己编号 打开扫码界面,重写方法接受返回
startActivityForResult(new Intent(MainActivity.this, QRActivity.class), 1);
}
});
}
/**
* 为了得到传回的数据,必须在前面的Activity中(指MainActivity类)重写onActivityResult方法
* requestCode 请求码,即调用startActivityForResult()传递过去的值
* resultCode 结果码,结果码用于标识返回数据来自哪个新Activity
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//扫描结果回调
if (resultCode == RESULT_OK) {
String result = data.getExtras().getString("result");//得到新Activity 关闭后返回的数据
TextView tv=findViewById(R.id.txt_result);
tv.setText(result);
}
}
}