Skip to content

Commit

Permalink
1、支持真实识别区域比例和识别区域偏移量可配置
Browse files Browse the repository at this point in the history
2、对外暴露更多可配置参数
  • Loading branch information
jenly1314 committed Sep 24, 2019
1 parent 3465b76 commit 39f13c3
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 93 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ ZXingLite for Android 是ZXing的精简版,基于ZXing库优化扫码和生成
| laserStyle | enum | line | 扫描激光的样式 |
| gridColumn | integer | 20 | 网格扫描激光列数 |
| gridHeight | integer | 40dp | 网格扫描激光高度,为0dp时,表示动态铺满 |
| cornerRectWidth | dimension | 4dp | 扫描区边角的宽 |
| cornerRectHeight | dimension | 16dp | 扫描区边角的高 |
| scannerLineMoveDistance | dimension | 2dp | 扫描线每次移动距离 |
| scannerLineHeight | dimension | 5dp | 扫描线高度 |
| frameLineWidth | dimension | 1dp | 边框线宽度 |
| scannerAnimationDelay | integer | 15 | 扫描动画延迟间隔时间,单位:毫秒 |
| frameRatio | float | 0.625f | 扫码框与屏幕占比 |


## 引入
Expand All @@ -48,21 +55,21 @@ ZXingLite for Android 是ZXing的精简版,基于ZXing库优化扫码和生成
<dependency>
<groupId>com.king.zxing</groupId>
<artifactId>zxing-lite</artifactId>
<version>1.1.2</version>
<version>1.1.3</version>
<type>pom</type>
</dependency>
```
### Gradle:
```gradle
//AndroidX 版本
implementation 'com.king.zxing:zxing-lite:1.1.2-androidx'
implementation 'com.king.zxing:zxing-lite:1.1.3-androidx'
//Android 版本
implementation 'com.king.zxing:zxing-lite:1.1.2'
implementation 'com.king.zxing:zxing-lite:1.1.3'
```
### Lvy:
```lvy
<dependency org='com.king.zxing' name='zxing-lite' rev='1.1.2'>
<dependency org='com.king.zxing' name='zxing-lite' rev='1.1.3'>
<artifact name='$AID' ext='pom'></artifact>
</dependency>
```
Expand Down Expand Up @@ -140,6 +147,10 @@ api 'com.google.zxing:core:3.3.3'

## 版本记录

#### v1.1.3:2019-9-24
* 支持真实识别区域比例和识别区域偏移量可配置
* 对外暴露更多可配置参数

#### v1.1.2:2019-6-27
* 优化部分细节,为迁移至AndroidX做准备
* 支持AndroidX对应版本
Expand Down Expand Up @@ -196,6 +207,8 @@ api 'com.google.zxing:core:3.3.3'

CSDN: <a title="CSDN博客" href="http://blog.csdn.net/jenly121" target="_blank">jenly121</a>

博客园: <a title="博客园" href="https://www.cnblogs.com/jenly" target="_blank">jenly</a>

Github: <a title="Github开源项目" href="https://github.com/jenly1314" target="_blank">jenly1314</a>

加入QQ群: <a title="点击加入QQ群" href="http://shang.qq.com/wpa/qunwpa?idkey=8fcc6a2f88552ea44b1411582c94fd124f7bb3ec227e2a400dbbfaad3dc2f5ad" target="_blank">20867961</a>
Expand Down
Binary file modified app/release/app-release.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion app/release/output.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":11,"versionName":"1.1.2","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":13,"versionName":"1.1.3","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ public void onCreate(Bundle icicle) {
//获取CaptureHelper,里面有扫码相关的配置设置
getCaptureHelper().playBeep(false)//播放音效
.vibrate(true)//震动
// .decodeFormats(DecodeFormatManager.QR_CODE_FORMATS)//设置只识别二维码会提升速度
.supportVerticalCode(true)//支持扫垂直条码,建议有此需求时才使用。
// .decodeFormats(DecodeFormatManager.QR_CODE_FORMATS)//设置只识别二维码会提升速度
// .framingRectRatio(0.9f)//设置识别区域比例,范围建议在0.625 ~ 1.0之间。非全屏识别时才有效
// .framingRectVerticalOffset(0)//设置识别区域垂直方向偏移量,非全屏识别时才有效
// .framingRectHorizontalOffset(0)//设置识别区域水平方向偏移量,非全屏识别时才有效
.continuousScan(isContinuousScan);//是否连扫
}

Expand Down
6 changes: 4 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
android:background="@color/colorPrimary"
android:layout_height="wrap_content"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp">
app:contentInsetStart="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
Expand All @@ -32,7 +34,7 @@
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="6dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="6dp"
android:text="默认扫码"
app:layout_constraintLeft_toLeftOf="parent"
Expand Down
102 changes: 74 additions & 28 deletions lib/src/main/java/com/king/zxing/CaptureHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.RectF;
import android.hardware.Camera;
import android.support.annotation.FloatRange;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.MotionEvent;
Expand Down Expand Up @@ -114,6 +114,19 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
*/
private boolean isFullScreenScan;

/**
* 识别区域比例,范围建议在0.625 ~ 1.0之间,默认0.9
*/
private float framingRectRatio = 0.9f;
/**
* 识别区域垂直方向偏移量
*/
private int framingRectVerticalOffset;
/**
* 识别区域水平方向偏移量
*/
private int framingRectHorizontalOffset;

private OnCaptureCallback onCaptureCallback;


Expand All @@ -139,6 +152,9 @@ public void onCreate(){

cameraManager = new CameraManager(activity);
cameraManager.setFullScreenScan(isFullScreenScan);
cameraManager.setFramingRectRatio(framingRectRatio);
cameraManager.setFramingRectVerticalOffset(framingRectVerticalOffset);
cameraManager.setFramingRectHorizontalOffset(framingRectHorizontalOffset);
callback = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
Expand All @@ -162,15 +178,10 @@ public void surfaceDestroyed(SurfaceHolder holder) {
}
};

onCaptureListener = new OnCaptureListener() {

@Override
public void onHandleDecode(Result result, Bitmap barcode, float scaleFactor) {
inactivityTimer.onActivity();
beepManager.playBeepSoundAndVibrate();
onResult(result);
}

onCaptureListener = (result, barcode, scaleFactor) -> {
inactivityTimer.onActivity();
beepManager.playBeepSoundAndVibrate();
onResult(result);
};
//设置是否播放音效和震动
beepManager.setPlayBeep(isPlayBeep);
Expand Down Expand Up @@ -309,6 +320,7 @@ private void handleZoom(boolean isZoomIn, Camera camera) {
* @param event
* @param camera
*/
@Deprecated
private void focusOnTouch(MotionEvent event,Camera camera) {

Camera.Parameters params = camera.getParameters();
Expand All @@ -332,13 +344,10 @@ private void focusOnTouch(MotionEvent event,Camera camera) {
params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
camera.setParameters(params);

camera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
Camera.Parameters params = camera.getParameters();
params.setFocusMode(currentFocusMode);
camera.setParameters(params);
}
camera.autoFocus((success, camera1) -> {
Camera.Parameters params1 = camera1.getParameters();
params1.setFocusMode(currentFocusMode);
camera1.setParameters(params1);
});

}
Expand Down Expand Up @@ -421,18 +430,15 @@ public void onResult(Result result){
}

if(isPlayBeep){//如果播放音效,则稍微延迟一点,给予播放音效时间
captureHandler.postDelayed(new Runnable() {
@Override
public void run() {
//如果设置了回调,并且onCallback返回为true,则表示拦截
if(onCaptureCallback!=null && onCaptureCallback.onResultCallback(text)){
return;
}
Intent intent = new Intent();
intent.putExtra(Intents.Scan.RESULT,text);
activity.setResult(Activity.RESULT_OK,intent);
activity.finish();
captureHandler.postDelayed(() -> {
//如果设置了回调,并且onCallback返回为true,则表示拦截
if(onCaptureCallback!=null && onCaptureCallback.onResultCallback(text)){
return;
}
Intent intent = new Intent();
intent.putExtra(Intents.Scan.RESULT,text);
activity.setResult(Activity.RESULT_OK,intent);
activity.finish();
},100);
return;
}
Expand Down Expand Up @@ -599,6 +605,46 @@ public CaptureHelper fullScreenScan(boolean fullScreenScan) {
return this;
}

/**
* 设置识别区域比例,范围建议在0.625 ~ 1.0之间。非全屏识别时才有效
* 0.625 即与默认推荐显示区域一致,1.0表示与宽度一致
* @param framingRectRatio 默认0.9
* @return
*/
public CaptureHelper framingRectRatio(@FloatRange(from = 0.0f ,to = 1.0f) float framingRectRatio) {
this.framingRectRatio = framingRectRatio;
if(cameraManager!=null){
cameraManager.setFramingRectRatio(framingRectRatio);
}
return this;
}

/**
* 设置识别区域垂直方向偏移量,非全屏识别时才有效
* @param framingRectVerticalOffset 默认0,表示不偏移
* @return
*/
public CaptureHelper framingRectVerticalOffset(int framingRectVerticalOffset) {
this.framingRectVerticalOffset = framingRectVerticalOffset;
if(cameraManager!=null){
cameraManager.setFramingRectVerticalOffset(framingRectVerticalOffset);
}
return this;
}

/**
* 设置识别区域水平方向偏移量,非全屏识别时才有效
* @param framingRectHorizontalOffset 默认0,表示不偏移
* @return
*/
public CaptureHelper framingRectHorizontalOffset(int framingRectHorizontalOffset) {
this.framingRectHorizontalOffset = framingRectHorizontalOffset;
if(cameraManager!=null){
cameraManager.setFramingRectHorizontalOffset(framingRectHorizontalOffset);
}
return this;
}


/**
* 设置扫码回调
Expand Down
Loading

0 comments on commit 39f13c3

Please sign in to comment.