最近项目中要用到扫描条形码的功能,以前写过扫描二维码的功能,其实功能实现是一样的。那下面我就给大家讲一下二维码的集成与使用,希望可以帮助到大家。
QRCode 简介
二维码 (QRCode):最早是日本的一家公司,所定义的一个编码标准,全称 Quick Response Code。它通过在一个矩形区域内使用黑白两种像素来进行编码,它具有高纠错性、高可用性、高识别性。
二维码的实现和解析是非常复杂的,但是现在有很多的第三方的库已经将这样一个解析的过程封装起来了,而开发者只需要调用其封装好的API即可完成二维码的生成和解析。
关于二维码的第三库很多,做的比较出色的还是ZXing。
ZXing简介
ZXing是一个开放源码的,用java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。ZXing可以实现使用手机的内置的摄像头完成二维码(条形码)的扫描及解码。
ZXing项目地址
https://github.com/zxing/zxing
如果下载不下来的话,可以去我的资源里面下载,我把ZXing的源码和我的项目放在一起了,可以直接去我的资源里进行下载,下载地址在最后面。
效果展示
使用方式
ZXing的源码是很多的,把ZXing的源码直接当做module导入是不怎么好的。我们可以将别人集成过的代码当做module导入,这样的话就会方便很多。
打开AndroidStudio,创建新的项目,将集成好的代码当做module导入,这样的话,我们就可以直接调用二维码所需要的功能了。
1.导入module
2.创建布局文件
<SurfaceView
android:id="@+id/capture_preview"
android:layout_width="match_parent"
android:layout_centerInParent="true"
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:layout_centerInParent="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_marginBottom="5dp"
android:layout_marginTop="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>
3.调用相机功能进行二维码的扫描。
注意:在android6.0以后,调用相机权限要手动加载。
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
//如果没有就进行申请
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(this, "权限被拒绝", Toast.LENGTH_SHORT).show();
}
return;
}
}
4.进行扫描
Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
//第二个参数是请求码
startActivityForResult(intent, 0);
5.对扫描获得的数据进行解析
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK){
Bundle bundle=data.getExtras();
String result=bundle.getString("result");
mTvResult.setText(result);
}
}
6.生成二维码
Bitmap bitmap = EncodingUtils.createQRCode(input,
500,500,mCbLogo.isChecked()?
BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher):null);
mIvPhoto.setImageBitmap(bitmap);
总结
这就是我实现的扫描二维码和生成二维码的过程,具体的代码可以去我的github下载,也可以去我的资源进行下载。
demo
csdn
https://download.youkuaiyun.com/download/wen_haha/10644118
github
https://github.com/kongkongdaren/ZXingDemo