重构v2.0

This commit is contained in:
Jenly
2020-12-23 19:03:44 +08:00
parent 7c87efa216
commit 5eb7511ed9
61 changed files with 2032 additions and 4150 deletions

View File

@@ -1,4 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion build_versions.compileSdk
@@ -22,10 +24,15 @@ android {
abortOnError false
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
@@ -33,11 +40,14 @@ dependencies {
testImplementation deps.test.junit
androidTestImplementation deps.test.runner
androidTestImplementation deps.test.espresso
//support
implementation deps.support.design
implementation deps.support.appcompat
implementation deps.support.constraintlayout
implementation deps.kotlin
implementation deps.corektx
implementation deps.easypermissions
implementation project(':lib')

View File

@@ -46,9 +46,16 @@
android:screenOrientation="portrait"
android:theme="@style/CaptureTheme"/>
<activity
android:name=".QRCodeActivity"
android:screenOrientation="portrait"
android:theme="@style/CaptureTheme"/>
<activity
android:name=".CodeActivity"
android:screenOrientation="portrait"/>
</application>
</manifest>

View File

@@ -9,31 +9,33 @@ import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.Result;
import com.king.zxing.CaptureHelper;
import com.king.zxing.OnCaptureCallback;
import com.king.zxing.CameraScan;
import com.king.zxing.DefaultCameraScan;
import com.king.zxing.ICameraScan;
import com.king.zxing.ViewfinderView;
import com.king.zxing.app.util.StatusBarUtils;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.camera.view.PreviewView;
import androidx.fragment.app.Fragment;
/**
* 自定义扫码当直接使用CaptureActivity
* 自定义扫码,切记自定义扫码需在{@link Activity}或者{@link Fragment}相对应的生命周期里面调用{@link #mCaptureHelper}对应的生命周期
* 自定义扫码,切记自定义扫码需在{@link Activity}或者{@link Fragment}相对应的生命周期里面调用{@link #mCameraScan}对应的生命周期
* @author <a href="mailto:jenly1314@gmail.com">Jenly</a>
*/
public class CustomActivity extends AppCompatActivity implements OnCaptureCallback {
public class CustomActivity extends AppCompatActivity implements CameraScan.OnScanResultCallback {
private boolean isContinuousScan;
private CaptureHelper mCaptureHelper;
private CameraScan mCameraScan;
private SurfaceView surfaceView;
private PreviewView previewView;
private ViewfinderView viewfinderView;
private View ivTorch;
private View ivFlash;
private Toast toast;
@@ -53,60 +55,38 @@ public class CustomActivity extends AppCompatActivity implements OnCaptureCallba
tvTitle.setText(getIntent().getStringExtra(MainActivity.KEY_TITLE));
surfaceView = findViewById(R.id.surfaceView);
previewView = findViewById(R.id.previewView);
viewfinderView = findViewById(R.id.viewfinderView);
ivTorch = findViewById(R.id.ivFlash);
ivTorch.setVisibility(View.INVISIBLE);
ivFlash = findViewById(R.id.ivFlash);
ivFlash.setVisibility(View.INVISIBLE);
isContinuousScan = getIntent().getBooleanExtra(MainActivity.KEY_IS_CONTINUOUS,false);
mCaptureHelper = new CaptureHelper(this,surfaceView,viewfinderView,ivTorch);
mCaptureHelper.setOnCaptureCallback(this);
mCaptureHelper.onCreate();
mCaptureHelper.vibrate(true)
.fullScreenScan(true)//全屏扫码
.supportVerticalCode(true)//支持扫垂直条码,建议有此需求时才使用。
.supportLuminanceInvert(true)//是否支持识别反色码(黑白反色的码),增加识别率
.continuousScan(isContinuousScan);
mCameraScan = new DefaultCameraScan(this,previewView);
mCameraScan.setOnScanResultCallback(this);
}
mCameraScan.startCamera();
@Override
protected void onResume() {
super.onResume();
mCaptureHelper.onResume();
}
@Override
protected void onPause() {
super.onPause();
mCaptureHelper.onPause();
}
@Override
protected void onDestroy() {
mCameraScan.release();
super.onDestroy();
mCaptureHelper.onDestroy();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mCaptureHelper.onTouchEvent(event);
return super.onTouchEvent(event);
}
/**
* 扫码结果回调
* @param result 扫码结果
* @return
*/
@Override
public boolean onResultCallback(Result result) {
public boolean onScanResultCallback(Result result) {
if(isContinuousScan){
showToast(result.getText());
}
return false;
//如果需支持连扫返回true即可
return isContinuousScan;
}
private void showToast(String text){

View File

@@ -52,21 +52,16 @@ public class CustomCaptureActivity extends CaptureActivity {
tvTitle.setText(getIntent().getStringExtra(MainActivity.KEY_TITLE));
isContinuousScan = getIntent().getBooleanExtra(MainActivity.KEY_IS_CONTINUOUS,false);
//获取CaptureHelper里面有扫码相关的配置设置
getCaptureHelper().playBeep(false)//播放音效
.vibrate(true)//震动
.fullScreenScan(true)
.supportVerticalCode(true)//支持扫垂直条码,建议有此需求时才使用。
// .decodeFormats(DecodeFormatManager.QR_CODE_FORMATS)//设置只识别二维码会提升速度
// .framingRectRatio(0.9f)//设置识别区域比例范围建议在0.625 ~ 1.0之间。非全屏识别时才有效
// .framingRectVerticalOffset(0)//设置识别区域垂直方向偏移量,非全屏识别时才有效
// .framingRectHorizontalOffset(0)//设置识别区域水平方向偏移量,非全屏识别时才有效
.tooDarkLux(45f)//设置光线太暗时,自动触发开启闪光灯的照度值
.brightEnoughLux(100f)//设置光线足够明亮时,自动触发关闭闪光灯的照度值
.continuousScan(isContinuousScan)//是否连扫
.supportLuminanceInvert(true);//是否支持识别反色码(黑白反色的码),增加识别率
}
@Override
public void initCameraScan() {
super.initCameraScan();
//获取CaptureHelper里面有扫码相关的配置设置
getCameraScan().setPlayBeep(false)//播放音效
.setVibrate(true);//震动
}
/**
* 扫码结果回调
@@ -74,12 +69,12 @@ public class CustomCaptureActivity extends CaptureActivity {
* @return
*/
@Override
public boolean onResultCallback(Result result) {
if(isContinuousScan){//连续扫码时,直接弹出结果
public boolean onScanResultCallback(Result result) {
if(isContinuousScan){
showToast(result.getText());
}
return super.onResultCallback(result);
//如果支持连扫返回true即可
return isContinuousScan;
}
private void showToast(String text){

View File

@@ -42,10 +42,15 @@ public class EasyCaptureActivity extends CaptureActivity {
StatusBarUtils.immersiveStatusBar(this,toolbar,0.2f);
TextView tvTitle = findViewById(R.id.tvTitle);
tvTitle.setText(getIntent().getStringExtra(MainActivity.KEY_TITLE));
getCaptureHelper()
// .decodeFormats(DecodeFormatManager.QR_CODE_FORMATS)//设置只识别二维码会提升速度
.playBeep(true)
.vibrate(true);
}
@Override
public void initCameraScan() {
super.initCameraScan();
getCameraScan()
.setPlayBeep(true)
.setVibrate(true);
}
public void onClick(View v){

View File

@@ -26,12 +26,12 @@ import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.king.zxing.CameraScan;
import com.king.zxing.CaptureActivity;
import com.king.zxing.Intents;
import com.king.zxing.app.util.UriUtils;
import com.king.zxing.util.CodeUtils;
import com.king.zxing.util.LogUtils;
import org.w3c.dom.Text;
import java.util.List;
@@ -78,6 +78,7 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
@@ -86,7 +87,7 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
if(resultCode == RESULT_OK && data!=null){
switch (requestCode){
case REQUEST_CODE_SCAN:
String result = data.getStringExtra(Intents.Scan.RESULT);
String result = CameraScan.parseScanResult(data);
showToast(result);
break;
case REQUEST_CODE_PHOTO:
@@ -109,7 +110,7 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
private void parsePhoto(Intent data){
final String path = UriUtils.getImagePath(this,data);
Log.d("Jenly","path:" + path);
LogUtils.d("path:" + path);
if(TextUtils.isEmpty(path)){
return;
}
@@ -254,6 +255,11 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
case R.id.btn7:
checkExternalStoragePermissions();
break;
case R.id.btn8:
this.cls = QRCodeActivity.class;
this.title = ((Button)v).getText().toString();
checkCameraPermissions();
break;
}
}

View File

@@ -0,0 +1,49 @@
package com.king.zxing.app;
import android.os.Bundle;
import android.widget.TextView;
import com.google.zxing.Result;
import com.king.zxing.CaptureActivity;
import com.king.zxing.DecodeFormatManager;
import com.king.zxing.analyze.MultiFormatAnalyzer;
import com.king.zxing.app.util.StatusBarUtils;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
/**
* @author <a href="mailto:jenly1314@gmail.com">Jenly</a>
*/
public class QRCodeActivity extends CaptureActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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));
}
@Override
public int getLayoutId() {
return R.layout.qr_code_activity;
}
@Override
public void initCameraScan() {
super.initCameraScan();
//在启动预览之前,设置分析器,只识别二维码,如果只有识别二维码的需求,这样效率更多高
getCameraScan()
.setVibrate(true)
.setAnalyzer(new MultiFormatAnalyzer(DecodeFormatManager.QR_CODE_HINTS));
}
@Override
public boolean onScanResultCallback(Result result) {
return super.onScanResultCallback(result);
}
}

View File

@@ -131,4 +131,17 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn6"
style="@style/OnClick"/>
<Button
android:id="@+id/btn8"
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/btn7"
style="@style/OnClick"/>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -5,8 +5,8 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="@+id/surfaceView"
<androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.king.zxing.ViewfinderView

View File

@@ -5,8 +5,8 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="@+id/surfaceView"
<androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.king.zxing.ViewfinderView
@@ -23,7 +23,7 @@
app:laserStyle="grid"
app:showResultPoint="true"/>
<ImageView
android:id="@+id/ivTorch"
android:id="@+id/ivFlashlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/flash_selected_selector"

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<androidx.camera.view.PreviewView
android:id="@+id/previewView"
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:showResultPoint="true"/>
<ImageView
android:id="@+id/ivFlashlight"
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" />
<include layout="@layout/toolbar_capture"/>
</androidx.constraintlayout.widget.ConstraintLayout>