说明:
这里一共有四种扫描界面,分别为:
(tip:可根据需要,自行滑到相应的标题学习即可)
一:基本用法:
也就是Zxing的基本使用,最简便的扫描界面
二:自定义扫描界面:带闪光灯
在基本的界面上,添加了有闪光灯的按钮
三:自定义扫描界面:带闪光灯 并修改其扫描界面
这一种,就是对Zxing的扫描界面进行了大更改,可自定义扫描条,边框,等等,只要认真看代码步骤,自己也可以定义
四:自定义扫描界面:改进版,仿微信扫描条
这种,是仿照微信的界面的,扫描条是用图片通过动画做的,也可以更换图片,推荐使用这种!!
相比第三种,性能上也比较好,因为第三种是不断的刷新界面形成扫描条动画,而刷新界面是很浪费性能的,所以,就会造成扫描条运动不流畅的视觉(加快运动更明显)
而第四种的扫描条是通过图片动画实现的,所以运动是很平滑流畅的,也没有不断刷新界面,不浪费性能,所以,建议使用第四种!!!
基本用法:
国际惯例,先上图:
Step 1 :添加依赖
//第三方zxing
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
Step 2 :添加权限
<uses-permission android:name="android.permission.CAMERA"/>
Step 3 :activity_main.xml布局 添加 测试用的两个控件
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"
android:id="@+id/button"/>
<ImageView
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Step 4 :MainActivity 代码:
public class MainActivity extends AppCompatActivity {
private Button button;
private ImageView ivImage;
// Step 1 : 初始化 获取控件 设置监听
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取测试的控件
button = findViewById(R.id.button);//点击跳转到扫码活动
ivImage = findViewById(R.id.iv_image);//输出二维码图片
//控件监听
listenerView();
}
private void listenerView() {
// Step 2 :跳转到扫描活动
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//=======设置扫描活动 可根据需求设置以下内容
IntentIntegrator intentIntegrator = new IntentIntegrator(MainActivity.this);
// 1.扫描成功后的提示音,默认关闭
intentIntegrator.setBeepEnabled(true);
// 2.启动后置摄像头扫描,若为 1 为前置摄像头,默认后置
intentIntegrator.setCameraId(0);
/* 3.设置扫描的条码的格式:默认为所有类型
* IntentIntegrator.PRODUCT_CODE_TYPES:商品码类型
* IntentIntegrator.ONE_D_CODE_TYPES:一维码类型
* IntentIntegrator.QR_CODE:二维码
* IntentIntegrator.DATA_MATRIX:数据矩阵类型
* IntentIntegrator.ALL_CODE_TYPES:所类有型
* */
intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
/* 4.方向锁:true为锁定,false反之,默认锁定.
ps:在AndroidManifest.xml里设置以下属性,则扫码界面完全依赖传感器(tools红色提示,指向它会提示,点击左边蓝色Create...即可)
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="fullSensor"
tools:replace="screenOrientation" />
* */
intentIntegrator.setOrientationLocked(true);
// 5.设置扫描界面的提示信息:默认为:请将条码置于取景框内扫描。(ps:设置没提示文字:setPrompt(""))
intentIntegrator.setPrompt("请选择二维码");
// 6.设置关闭扫描的时间(单位:毫秒),不设置不关闭
intentIntegrator.setTimeout(60000);
// 7.保存二维码图片:在onActivityResult方法里可获取保存的路径,根据需要来是否需要保存
intentIntegrator.setBarcodeImageEnabled(true);
//启动扫描
intentIntegrator.initiateScan();
}
});
}
// Step 3 :处理扫码后返回的结果
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
if(result!=null){
//==是否扫到内容
if (result.getContents()!=null){
Toast.makeText(this,"扫描结果:"+result.getContents(),Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this,"取消扫码",Toast.LENGTH_LONG).show();
}
//==是否有保存照片的路径 在intentIntegrator已设置保存照片
if(result.getBarcodeImagePath()!=null){
FileInputStream file=null;
try {
file=new FileInputStream(new File(result.getBarcodeImagePath()));
ivImage.setImageBitmap(BitmapFactory.decodeStream(file));//显示获取的照片
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
try {
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/* 获取条码种类:在intentIntegrator.setDesiredBarcodeFormats那设置扫码格式后(点击格式可进入查看该格式有多少个类型)
例如:PRODUCT_CODE_TYPES:商品码类型,它就有 UPC_A, UPC_E, EAN_8, EAN_13, RSS_14 种类
public static final Collection<String> PRODUCT_CODE_TYPES = list(UPC_A, UPC_E, EAN_8, EAN_13, RSS_14);
根据getFormatName获取到的种类,就知道是哪个扫码格式,进而根据需求进行相关操作
*/
if (result.getFormatName()!=null){
Toast.makeText(this,"图片格式:"+result.getFormatName(),Toast.LENGTH_LONG).show();
}
}else{
super.onActivityResult(requestCode, resultCode, data);
}
}
}
自定义扫描界面:带闪光灯**
国际惯例,先上图:图中白点为闪光灯按钮
Step 1 :引入依赖:
//第三方zxing
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
Step 2 :申请权限:
<!--相机-->
<uses-permission android:name="android.permission.CAMERA"/>
<!--若需要闪光灯权限 ,请加入此权限(自测不需要)-->
<!--
<uses-permission android:name="android.permission.FLASHLIGHT" />
-->
Step 3 :准备3个布局
activity_main.xml
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"
android:id="@+id/button"/>
</LinearLayout>
content_scan.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!--
layout_width、layout_height:启动扫描界面的布局参数
zxing_framing_rect_width、zxing_framing_rect_height:
在扫描界面中,只能扫描二维码的宽高,去掉后会有默认的宽高
-->
<com.journeyapps.barcodescanner.BarcodeView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/zxing_barcode_surface"
app:zxing_framing_rect_width="250dp"
app:zxing_framing_rect_height="250dp"/>
<com.journeyapps.barcodescanner.ViewfinderView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/zxing_viewfinder_view"
app:zxing_possible_result_points="@color/zxing_custom_possible_result_points"
app:zxing_result_view="@color/zxing_custom_result_view"
app:zxing_viewfinder_laser="@color/zxing_custom_viewfinder_laser"
app:zxing_viewfinder_mask="@color/zxing_custom_viewfinder_mask"/>
</merge>
activity_scan.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=