优化部分已知细节问题

This commit is contained in:
jenly1314
2019-06-24 17:01:55 +08:00
parent 6acd18fb98
commit d3db43b461
16 changed files with 116 additions and 85 deletions

Binary file not shown.

14
.idea/misc.xml generated
View File

@@ -5,22 +5,32 @@
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" />
<option name="myNullables">
<value>
<list size="5">
<list size="10">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" />
<item index="2" class="java.lang.String" itemvalue="javax.annotation.CheckForNull" />
<item index="3" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.Nullable" />
<item index="4" class="java.lang.String" itemvalue="android.support.annotation.Nullable" />
<item index="5" class="java.lang.String" itemvalue="androidx.annotation.Nullable" />
<item index="6" class="java.lang.String" itemvalue="androidx.annotation.RecentlyNullable" />
<item index="7" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.qual.Nullable" />
<item index="8" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NullableDecl" />
<item index="9" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NullableType" />
</list>
</value>
</option>
<option name="myNotNulls">
<value>
<list size="4">
<list size="9">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nonnull" />
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" />
<item index="3" class="java.lang.String" itemvalue="android.support.annotation.NonNull" />
<item index="4" class="java.lang.String" itemvalue="androidx.annotation.NonNull" />
<item index="5" class="java.lang.String" itemvalue="androidx.annotation.RecentlyNonNull" />
<item index="6" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.qual.NonNull" />
<item index="7" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NonNullDecl" />
<item index="8" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NonNullType" />
</list>
</value>
</option>

View File

@@ -124,7 +124,7 @@ api 'com.google.zxing:core:3.3.3'
> 4、参照CaptureHelper写一个自定义的扫码帮助类其它步骤同方式3。扩展高级用法谨慎使用
更多使用详情,请查看[app](app)中的源码使用示例
更多使用详情,请查看[app](app)中的源码使用示例或直接查看[API帮助文档](https://jenly1314.github.io/projects/ZXingLite/doc/)
## 版本记录

Binary file not shown.

View File

@@ -13,6 +13,7 @@ import android.widget.TextView;
import android.widget.Toast;
import com.king.zxing.CaptureHelper;
import com.king.zxing.DecodeFormatManager;
import com.king.zxing.OnCaptureCallback;
import com.king.zxing.ViewfinderView;
import com.king.zxing.app.util.StatusBarUtils;

View File

@@ -15,16 +15,19 @@
*/
package com.king.zxing.app;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.Result;
import com.google.zxing.DecodeHintType;
import com.king.zxing.CaptureActivity;
import com.king.zxing.app.util.StatusBarUtils;
import com.king.zxing.camera.CameraConfigurationUtils;
/**
* 自定义继承CaptureActivity
@@ -32,6 +35,8 @@ import com.king.zxing.app.util.StatusBarUtils;
*/
public class CustomCaptureActivity extends CaptureActivity {
private ImageView ivFlash;
private boolean isContinuousScan;
@Override
public int getLayoutId() {
@@ -40,39 +45,46 @@ public class CustomCaptureActivity extends CaptureActivity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
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));
ivFlash = findViewById(R.id.ivFlash);
if(!hasTorch()){
ivFlash.setVisibility(View.GONE);
}
isContinuousScan = getIntent().getBooleanExtra(MainActivity.KEY_IS_CONTINUOUS,false);
//获取CaptureHelper里面有扫码相关的配置设置
getCaptureHelper().playBeep(false)//播放音效
.vibrate(true)//震动
// .decodeFormats(DecodeFormatManager.QR_CODE_FORMATS)//设置只识别二维码会提升速度
.supportVerticalCode(true)//支持扫垂直条码,建议有此需求时才使用。
.continuousScan(isContinuousScan);//是否连扫
}
/**
* 开启或关闭闪光灯(手电筒)
* @param on {@code true}表示开启,{@code false}表示关闭
*/
public void setTorch(boolean on){
Camera camera = getCameraManager().getOpenCamera().getCamera();
Camera.Parameters parameters = camera.getParameters();
CameraConfigurationUtils.setTorch(parameters,on);
camera.setParameters(parameters);
}
/**
* 关闭闪光灯(手电筒)
* 检测是否支持闪光灯(手电筒)
* @return
*/
private void offFlash(){
Camera camera = getCameraManager().getOpenCamera().getCamera();
Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
}
/**
* 开启闪光灯(手电筒)
*/
public void openFlash(){
Camera camera = getCameraManager().getOpenCamera().getCamera();
Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
public boolean hasTorch(){
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}
@@ -92,14 +104,9 @@ public class CustomCaptureActivity extends CaptureActivity {
private void clickFlash(View v){
if(v.isSelected()){
offFlash();
v.setSelected(false);
}else{
openFlash();
v.setSelected(true);
}
boolean isSelected = v.isSelected();
setTorch(!isSelected);
v.setSelected(!isSelected);
}
public void onClick(View v){

View File

@@ -21,6 +21,7 @@ import android.view.View;
import android.widget.TextView;
import com.king.zxing.CaptureActivity;
import com.king.zxing.DecodeFormatManager;
import com.king.zxing.app.util.StatusBarUtils;
/**
@@ -41,7 +42,10 @@ 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().playBeep(true).vibrate(true);
getCaptureHelper()
// .decodeFormats(DecodeFormatManager.QR_CODE_FORMATS)//设置只识别二维码会提升速度
.playBeep(true)
.vibrate(true);
}
public void onClick(View v){

View File

@@ -1,34 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportHeight="108"
android:viewportWidth="108">
<path
android:fillType="evenOdd"
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
android:strokeColor="#00000000"
android:strokeWidth="1">
<aapt:attr name="android:fillColor">
<gradient
android:endX="78.5885"
android:endY="90.9159"
android:startX="48.7653"
android:startY="61.0927"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0"/>
<item
android:color="#00000000"
android:offset="1.0"/>
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
android:strokeColor="#00000000"
android:strokeWidth="1"/>
</vector>

View File

@@ -26,6 +26,11 @@ android {
}
}
//task javadoc(type: Javadoc) {
// source = android.sourceSets.main.java.srcDirs
// classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
//}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
testImplementation deps.test.junit

View File

@@ -58,7 +58,7 @@ public class CaptureHandler extends Handler implements ResultPointCallback {
CaptureHandler(Activity activity,ViewfinderView viewfinderView,OnCaptureListener onCaptureListener,
Collection<BarcodeFormat> decodeFormats,
Map<DecodeHintType,?> baseHints,
Map<DecodeHintType,Object> baseHints,
String characterSet,
CameraManager cameraManager) {
this.activity = activity;

View File

@@ -21,7 +21,6 @@ import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.RectF;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.MotionEvent;
@@ -36,6 +35,7 @@ import com.king.zxing.camera.CameraManager;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
@@ -63,7 +63,7 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
private SurfaceHolder.Callback callback;
private Collection<BarcodeFormat> decodeFormats;
private Map<DecodeHintType,?> decodeHints;
private Map<DecodeHintType,Object> decodeHints;
private String characterSet;
private boolean hasSurface;
@@ -376,10 +376,10 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
}
/**
*
* 根据范围限定值
* @param x
* @param min
* @param max
* @param min 范围最小值
* @param max 范围最大值
* @return
*/
private int clamp(int x, int min, int max) {
@@ -504,7 +504,7 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
/**
* 设置支持的解码一/二维码格式,默认常规的码都支持
* @param decodeFormats
* @param decodeFormats 可参见{@link DecodeFormatManager}
* @return
*/
public CaptureHelper decodeFormats(Collection<BarcodeFormat> decodeFormats) {
@@ -517,11 +517,25 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
* @param decodeHints
* @return
*/
public CaptureHelper decodeHints(Map<DecodeHintType,?> decodeHints) {
public CaptureHelper decodeHints(Map<DecodeHintType,Object> decodeHints) {
this.decodeHints = decodeHints;
return this;
}
/**
* {@link DecodeHintType}
* @param key {@link DecodeHintType}
* @param value {@link }
* @return
*/
public CaptureHelper decodeHint(DecodeHintType key,Object value){
if(decodeHints == null){
decodeHints = new EnumMap<>(DecodeHintType.class);
}
decodeHints.put(key,value);
return this;
}
/**
* 设置解码时编码字符集
* @param characterSet
@@ -596,21 +610,37 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
return this;
}
/**
* {@link CameraManager}
* @return {@link #cameraManager}
*/
@Override
public CameraManager getCameraManager() {
return cameraManager;
}
/**
* {@link BeepManager}
* @return {@link #beepManager}
*/
@Override
public BeepManager getBeepManager() {
return beepManager;
}
/**
* {@link AmbientLightManager}
* @return {@link #ambientLightManager}
*/
@Override
public AmbientLightManager getAmbientLightManager() {
return ambientLightManager;
}
/**
* {@link InactivityTimer}
* @return {@link #inactivityTimer}
*/
@Override
public InactivityTimer getInactivityTimer() {
return inactivityTimer;

View File

@@ -33,9 +33,12 @@ import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.PlanarYUVLuminanceSource;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.ResultPoint;
import com.google.zxing.common.GlobalHistogramBinarizer;
import com.google.zxing.common.HybridBinarizer;
import com.king.zxing.camera.CameraManager;
@@ -103,17 +106,22 @@ final class DecodeHandler extends Handler {
try {
rawResult = multiFormatReader.decodeWithState(bitmap);
} catch (Exception e) {
if(isSupportVerticalCode){
source = buildPlanarYUVLuminanceSource(data,width,height,!isScreenPortrait);
if(source!=null){
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
try{
rawResult = multiFormatReader.decodeWithState(bitmap1);
}catch (Exception e1){
BinaryBitmap bitmap1 = new BinaryBitmap(new GlobalHistogramBinarizer(source));
try{
rawResult = multiFormatReader.decodeWithState(bitmap1);
}catch (Exception e1){
if(isSupportVerticalCode){
source = buildPlanarYUVLuminanceSource(data,width,height,!isScreenPortrait);
if(source!=null){
BinaryBitmap bitmap2 = new BinaryBitmap(new HybridBinarizer(source));
try{
rawResult = multiFormatReader.decodeWithState(bitmap2);
}catch (Exception e2){
}
}
}
}
}
} finally {

View File

@@ -55,7 +55,7 @@ final class DecodeThread extends Thread {
DecodeThread(Context context,CameraManager cameraManager,
CaptureHandler captureHandler,
Collection<BarcodeFormat> decodeFormats,
Map<DecodeHintType,?> baseHints,
Map<DecodeHintType,Object> baseHints,
String characterSet,
ResultPointCallback resultPointCallback) {

View File

@@ -161,8 +161,6 @@ final class CameraConfigurationManager {
parameters.setZoom(parameters.getMaxZoom() / 10);
}
theCamera.setParameters(parameters);
initializeTorch(parameters, prefs, safeMode);
CameraConfigurationUtils.setFocus(

View File

@@ -198,7 +198,8 @@ public class CodeUtils {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
result = reader.decode(bitmap,hints);
} catch (Exception e) {//解析失败则通过GlobalHistogramBinarizer 再试一次
} catch (Exception e) {
//解析失败则通过GlobalHistogramBinarizer 再试一次
BinaryBitmap bitmap1 = new BinaryBitmap(new GlobalHistogramBinarizer(source));
try {
result = reader.decode(bitmap1);
@@ -232,6 +233,7 @@ public class CodeUtils {
decodeFormats.addAll(DecodeFormatManager.AZTEC_FORMATS);
decodeFormats.addAll(DecodeFormatManager.PDF417_FORMATS);
hints.put(DecodeHintType.TRY_HARDER,Boolean.TRUE);
hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
return parseCode(bitmapPath,hints);
}
@@ -257,7 +259,7 @@ public class CodeUtils {
BinaryBitmap bitmap1 = new BinaryBitmap(new GlobalHistogramBinarizer(source));
try {
result = reader.decodeWithState(bitmap1);
} catch (NotFoundException ne) {
} catch (Exception ne) {
}
} finally {

View File

@@ -29,7 +29,7 @@ versions.espresso = "3.0.2"
//zxing
versions.zxing = "3.3.3"
versions.easypermissions = "1.3.0"
versions.easypermissions = "2.0.1"