1、新增CaptureFragment

2、将扫码相关逻辑与界面分离,ZXingLite使用更容易扩展。
This commit is contained in:
jenly1314
2019-04-19 18:45:13 +08:00
parent 4c7757fcb0
commit 36ccf48b7f
33 changed files with 1483 additions and 1186 deletions

Binary file not shown.

View File

@@ -1 +1 @@
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":8,"versionName":"1.0.7","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":9,"versionName":"1.1.0","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]

View File

@@ -21,12 +21,25 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="com.king.zxing.CaptureActivity"
android:screenOrientation="portrait"/>
<activity
android:name=".EasyCaptureActivity"
android:screenOrientation="portrait"/>
<activity
android:name=".CustomCaptureActivity"
android:screenOrientation="portrait"/>
<activity
android:name=".CaptureFragmentActivity"
android:screenOrientation="portrait"/>
<activity
android:name=".CustomActivity"
android:screenOrientation="portrait"/>
<activity
android:name=".CodeActivity"
android:screenOrientation="portrait"/>

View File

@@ -0,0 +1,39 @@
package com.king.zxing.app;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;
import com.king.zxing.CaptureFragment;
import com.king.zxing.app.util.StatusBarUtils;
/**
* Fragment扫码
* @author <a href="mailto:jenly1314@gmail.com">Jenly</a>
*/
public class CaptureFragmentActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_activity);
Toolbar toolbar = findViewById(R.id.toolbar);
StatusBarUtils.immersiveStatusBar(this,toolbar,0.2f);
TextView tvTitle = findViewById(R.id.tvTitle);
tvTitle.setText(getIntent().getStringExtra(MainActivity.KEY_TITLE));
replaceFragment(CaptureFragment.newInstance());
}
public void replaceFragment(Fragment fragment){
replaceFragment( R.id.fragmentContent,fragment);
}
public void replaceFragment(@IdRes int id, Fragment fragment) {
getSupportFragmentManager().beginTransaction().replace(id, fragment).commit();
}
}

View File

@@ -0,0 +1,143 @@
package com.king.zxing.app;
import android.hardware.Camera;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.king.zxing.CaptureHelper;
import com.king.zxing.OnCaptureCallback;
import com.king.zxing.ViewfinderView;
import com.king.zxing.app.util.StatusBarUtils;
/**
* 自定义扫码当直接使用CaptureActivity
* 自定义扫码,切记自定义扫码需在{@link Activity}或者{@link Fragment}相对应的生命周期里面调用{@link #mCaptureHelper}对应的生命周期
* @author <a href="mailto:jenly1314@gmail.com">Jenly</a>
*/
public class CustomActivity extends AppCompatActivity implements OnCaptureCallback {
private boolean isContinuousScan;
private CaptureHelper mCaptureHelper;
private SurfaceView surfaceView;
private ViewfinderView viewfinderView;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.custom_activity);
initUI();
}
private void initUI(){
Toolbar toolbar = findViewById(R.id.toolbar);
StatusBarUtils.immersiveStatusBar(this,toolbar,0.2f);
TextView tvTitle = findViewById(R.id.tvTitle);
tvTitle.setText(getIntent().getStringExtra(MainActivity.KEY_TITLE));
surfaceView = findViewById(R.id.surfaceView);
viewfinderView = findViewById(R.id.viewfinderView);
isContinuousScan = getIntent().getBooleanExtra(MainActivity.KEY_IS_CONTINUOUS,false);
mCaptureHelper = new CaptureHelper(this,surfaceView,viewfinderView);
mCaptureHelper.onCreate();
mCaptureHelper.vibrate(true)
.continuousScan(isContinuousScan);
}
@Override
protected void onResume() {
super.onResume();
mCaptureHelper.onResume();
}
@Override
protected void onPause() {
super.onPause();
mCaptureHelper.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mCaptureHelper.onDestroy();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mCaptureHelper.onTouchEvent(event);
return super.onTouchEvent(event);
}
/**
* 关闭闪光灯(手电筒)
*/
private void offFlash(){
Camera camera = mCaptureHelper.getCameraManager().getOpenCamera().getCamera();
Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
}
/**
* 开启闪光灯(手电筒)
*/
public void openFlash(){
Camera camera = mCaptureHelper.getCameraManager().getOpenCamera().getCamera();
Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
}
/**
* 扫码结果回调
* @param result 扫码结果
* @return
*/
@Override
public boolean onResultCallback(String result) {
if(isContinuousScan){
Toast.makeText(this,result,Toast.LENGTH_SHORT).show();
}
return false;
}
private void clickFlash(View v){
if(v.isSelected()){
offFlash();
v.setSelected(false);
}else{
openFlash();
v.setSelected(true);
}
}
public void OnClick(View v){
switch (v.getId()){
case R.id.ivLeft:
onBackPressed();
break;
case R.id.ivFlash:
clickFlash(v);
break;
}
}
}

View File

@@ -27,6 +27,7 @@ import com.king.zxing.CaptureActivity;
import com.king.zxing.app.util.StatusBarUtils;
/**
* 自定义继承CaptureActivity
* @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
*/
public class CustomCaptureActivity extends CaptureActivity {
@@ -46,9 +47,10 @@ public class CustomCaptureActivity extends CaptureActivity {
tvTitle.setText(getIntent().getStringExtra(MainActivity.KEY_TITLE));
isContinuousScan = getIntent().getBooleanExtra(MainActivity.KEY_IS_CONTINUOUS,false);
getBeepManager().setPlayBeep(true);
getBeepManager().setVibrate(true);
//获取CaptureHelper里面有扫码相关的配置设置
getCaptureHelper().playBeep(true)//播放音效
.vibrate(true)//震动
.continuousScan(isContinuousScan);//是否连扫
}
/**
@@ -71,37 +73,21 @@ public class CustomCaptureActivity extends CaptureActivity {
camera.setParameters(parameters);
}
/**
* 接收扫码结果,想支持连扫时,可将{@link #isContinuousScan()}返回为{@code true}并重写此方法
* 如果{@link #isContinuousScan()}支持连扫,则默认重启扫码和解码器;当连扫逻辑太复杂时,
* 请将{@link #isAutoRestartPreviewAndDecode()}返回为{@code false},并手动调用{@link #restartPreviewAndDecode()}
* 扫码结果回调
* @param result 扫码结果
* @return
*/
@Override
public void onResult(Result result) {
super.onResult(result);
public boolean onResultCallback(String result) {
if(isContinuousScan){//连续扫码时,直接弹出结果
Toast.makeText(this,result.getText(),Toast.LENGTH_SHORT).show();
Toast.makeText(this,result,Toast.LENGTH_SHORT).show();
}
return super.onResultCallback(result);
}
/**
* 是否连续扫码,如果想支持连续扫码,则将此方法返回{@code true}并重写{@link #onResult(Result)}
* @return 默认返回 false
*/
@Override
public boolean isContinuousScan() {
return isContinuousScan;
}
/**
* 是否自动重启扫码和解码器,当支持连扫时才起作用。
* @return 默认返回 true
*/
@Override
public boolean isAutoRestartPreviewAndDecode() {
return super.isAutoRestartPreviewAndDecode();
}
private void clickFlash(View v){
if(v.isSelected()){

View File

@@ -18,7 +18,6 @@ package com.king.zxing.app;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
@@ -30,8 +29,6 @@ import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.zxing.DecodeHintType;
import com.google.zxing.common.StringUtils;
import com.king.zxing.CaptureActivity;
import com.king.zxing.Intents;
import com.king.zxing.app.util.UriUtils;
@@ -42,6 +39,20 @@ import java.util.List;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
/**
* 扫码Demo示例说明
*
* 快速实现扫码有以下几种方式:
*
* 1、直接使用CaptureActivity或者CaptureFragment。(纯洁的扫码,无任何添加剂)
*
* 2、通过继承CaptureActivity或者CaptureFragment并自定义布局。适用于大多场景并无需关心扫码相关逻辑
*
* 3、在你项目的Activity或者Fragment中创建创建一个CaptureHelper并在相应的生命周期中调用CaptureHelper的周期。适用于想在扫码界面写交互逻辑又因为项目架构或其它原因无法直接或间接继承CaptureActivity或CaptureFragment时使用
*
* 4、参照CaptureHelper写一个自定义的扫码帮助类其它步骤同方式3。扩展高级用法谨慎使用
*
*/
public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks{
public static final String KEY_TITLE = "key_title";
@@ -195,18 +206,18 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
isContinuousScan = false;
switch (v.getId()){
case R.id.btn0:
this.cls = CaptureActivity.class;
this.title = ((Button)v).getText().toString();
checkCameraPermissions();
break;
case R.id.btn1:
this.cls = CustomCaptureActivity.class;
this.title = ((Button)v).getText().toString();
isContinuousScan = true;
checkCameraPermissions();
break;
case R.id.btn1:
this.cls = CaptureActivity.class;
this.title = ((Button)v).getText().toString();
checkCameraPermissions();
break;
case R.id.btn2:
this.cls = EasyCaptureActivity.class;
this.cls = CaptureFragmentActivity.class;
this.title = ((Button)v).getText().toString();
checkCameraPermissions();
break;
@@ -216,12 +227,17 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
checkCameraPermissions();
break;
case R.id.btn4:
startCode(false);
this.cls = CustomActivity.class;
this.title = ((Button)v).getText().toString();
checkCameraPermissions();
break;
case R.id.btn5:
startCode(true);
startCode(false);
break;
case R.id.btn6:
startCode(true);
break;
case R.id.btn7:
checkExternalStoragePermissions();
break;
}

View File

@@ -26,13 +26,6 @@
android:layout_gravity="center_horizontal"
android:text="@string/app_name"/>
</android.support.v7.widget.Toolbar>
<android.support.constraint.Guideline
android:id="@+id/line"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintGuide_percent="0.2"/>
<Button
android:id="@+id/btn0"
android:layout_width="match_parent"
@@ -41,10 +34,10 @@
android:layout_marginRight="20dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:text="连续扫码"
android:text="默认扫码"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/line"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
style="@style/OnClick"/>
<Button
android:id="@+id/btn1"
@@ -54,7 +47,7 @@
android:layout_marginRight="20dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:text="默认扫码"
android:text="连续扫码"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn0"
@@ -67,7 +60,8 @@
android:layout_marginRight="20dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:text="简单自定义布局扫码"
android:text="Fragment扫码"
android:textAllCaps="false"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn1"
@@ -93,7 +87,7 @@
android:layout_marginRight="20dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:text="生成条形码"
android:text="自定义扫码"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn3"
@@ -106,7 +100,7 @@
android:layout_marginRight="20dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:text="生成二维码"
android:text="生成条形码"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn4"
@@ -119,9 +113,22 @@
android:layout_marginRight="20dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:text="识别一维码/二维码图片"
android:text="生成二维码"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn5"
style="@style/OnClick"/>
<Button
android:id="@+id/btn7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:text="识别一维码/二维码图片"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn6"
style="@style/OnClick"/>
</android.support.constraint.ConstraintLayout>

View File

@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.king.zxing.ViewfinderView
android:id="@+id/viewfinderView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:labelText="@string/tips_scan_code"
app:labelTextSize="@dimen/size_14sp"
app:laserColor="@color/colorAccent"
app:frameColor="@color/colorPrimary"
app:cornerColor="@color/colorPrimary"
app:resultPointColor="@color/colorAccent"
app:labelTextLocation="bottom"
app:laserStyle="grid"
app:gridHeight="0dp"/>
<ImageView
android:id="@+id/ivFlash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/flash_selected_selector"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="160dp"
style="@style/OnClick"/>
<include layout="@layout/toolbar_capture"/>
</android.support.constraint.ConstraintLayout>

View File

@@ -6,11 +6,11 @@
android:layout_height="match_parent">
<SurfaceView
android:id="@+id/preview_view"
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.king.zxing.ViewfinderView
android:id="@+id/viewfinder_view"
android:id="@+id/viewfinderView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:labelText="@string/tips_scan_code"

View File

@@ -3,6 +3,6 @@
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/capture"/>
<include layout="@layout/zxl_capture"/>
<include layout="@layout/toolbar_capture"/>
</android.support.constraint.ConstraintLayout>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/fragmentContent"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<include layout="@layout/toolbar_capture"/>
</FrameLayout>

View File

@@ -2,7 +2,7 @@
<string name="app_name">ZXingLite</string>
<string name="permission_camera">App扫码需要用到拍摄权限</string>
<string name="permission_external_storage">App需要用到读写权限</string>
<string name="tips_scan_code">将二维码/条形码放入框内,即可自动扫</string>
<string name="tips_scan_code">将二维码/条形码放入框内,即可自动扫</string>
<string name="qr_code">二维码</string>
<string name="bar_code">条形码</string>
</resources>