Zxing二维码的扫描

来源:互联网 发布:淘宝店旺旺5161718艾条 编辑:程序博客网 时间:2024/05/02 04:51

首先需要去下载二维码的module包:https://github.com/xuyisheng/ZXingLib
然后我们就要在工程项目中导包并且添加依赖
这些完成之后我们就再manifest中来看看吧:
首先添加相机权限:
uses-feature android:name=”android.hardware.camera” />
uses-feature android:name=”android.hardware.camera.autofocus” />
然后还要注册
activity
android:configChanges=”orientation|keyboardHidden”
android:name=”com.uuzuche.lib_zxing.activity.CaptureActivity”
android:screenOrientation=”portrait”
android:windowSoftInputMode=”stateAlwaysHidden”
android:label=”扫描二维码”
android:theme=”@style/Theme.AppCompat.NoActionBar” />
注册文件就算结束了,
扫描二维码:
界面布局,在主界面写一个button跳转就OK了
在主页面监听button的跳转
Intent intent = new Intent(getActivity(), MyZxingActivity.class);
startActivityForResult(intent, 1000);//这里的1000只是一个flag
还要在主界面复写一个方法是对扫描结果的一个反馈处理
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000) {
//处理扫描结果(在界面上显示)
if (null != data) {
Bundle bundle = data.getExtras();
if (bundle == null) {
return;
}
if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
String result = bundle.getString(CodeUtils.RESULT_STRING);
Toast.makeText(getContext(), “解析结果:” + result, Toast.LENGTH_LONG).show();
} else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
Toast.makeText(getContext(), “解析二维码失败”, Toast.LENGTH_LONG).show();
}
}
}
}
然后在扫描界面的类我直接就全部贴过来吧,这里的扫描界面我多加了一个按钮可以直接跳转到本地相册
public class MyZxingActivity extends AppCompatActivity implements CodeUtils.AnalyzeCallback {
@BindView(R.id.zxing_guanbi)
TextView zxingGuanbi;
@BindView(R.id.zxing_xiangce)
TextView zxingXiangce;
@BindView(R.id.zxing_top_back)
LinearLayout zxingTopBack;
@BindView(R.id.img_zxing)
ImageView imgZxing;
@BindView(R.id.activity_my_zxing)
RelativeLayout activityMyZxing;
private static final int PHOTO_REQUEST_CAREMA = 1;// 拍照
private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
private static final int PHOTO_REQUEST_CUT = 3;// 结果
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_zxing);
ButterKnife.bind(this);
CaptureFragment captureFragment = (CaptureFragment) getSupportFragmentManager().findFragmentById(R.id.fram_zxing);
captureFragment.setAnalyzeCallback(this);//添加解析回调
zxingTopBack.getBackground().setAlpha(80);
}
@Override
public void onAnalyzeSuccess(Bitmap mBitmap, String result) {//解析成功
if (mBitmap != null)
imgZxing.setImageBitmap(mBitmap);
imgZxing.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
imgZxing.setVisibility(View.GONE);
}
}, 3000);
if(result.startsWith(“http:”)) {
Intent intent = new Intent();
intent.setAction(“android.intent.action.VIEW”);
Uri content_url = Uri.parse(result.toString());
intent.setData(content_url);
startActivity(intent);
} else {
Toast.makeText(this, “二维码内容为: ” + result, Toast.LENGTH_SHORT).show();
finish();
}
}
@Override
public void onAnalyzeFailed() {//解析失败
}
@OnClick({R.id.zxing_guanbi, R.id.zxing_xiangce})
public void onClick(View view) {
switch (view.getId()) {
case R.id.zxing_guanbi:
finish();
break;
case R.id.zxing_xiangce:
// 激活系统图库,选择一张图片
Intent intent = new Intent(Intent.ACTION_GET_CONTENT,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType(“image/*”);
// 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_GALLERY
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
break;
}
}
}
扫描界面的布局如下
RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:id=”@+id/activity_my_zxing”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”aiyiqi.MyZxingActivity”>

fragment    android:id="@+id/fram_zxing"    android:name="com.uuzuche.lib_zxing.activity.CaptureFragment"    android:layout_width="match_parent"    android:layout_height="match_parent" />LinearLayout    android:layout_width="match_parent"    android:layout_height="60dp"    android:id="@+id/zxing_top_back"    android:orientation="horizontal"    android:padding="15dp"    android:gravity="center_vertical"    android:background="@color/dark">    TextView        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="wrap_content"        android:gravity="center"        android:text="关闭"        android:id="@+id/zxing_guanbi"        android:textSize="20dp"        android:textColor="@color/green_01"/>    TextView        android:layout_width="0dp"        android:layout_weight="4"        android:layout_height="wrap_content"        android:text="扫一扫"        android:textColor="@color/white"        android:textSize="25dp"        android:gravity="center"/>    TextView        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="wrap_content"        android:gravity="center"        android:text="相册"        android:id="@+id/zxing_xiangce"        android:textSize="20dp"        android:textColor="@color/green_01"/>/LinearLayout>ImageView    android:id="@+id/img_zxing"    android:layout_width="200dp"    android:layout_height="200dp"    android:layout_centerInParent="true" />

/RelativeLayout>

0 0