Skip to content

Commit

Permalink
支持识别反色码(#66,#69,#72
Browse files Browse the repository at this point in the history
  • Loading branch information
jenly1314 committed Dec 27, 2019
1 parent 0a136a6 commit 42a5cc9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 25 deletions.
12 changes: 8 additions & 4 deletions lib/src/main/java/com/king/zxing/CaptureActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


public class CaptureActivity extends Activity implements OnCaptureCallback{
/**
* @author <a href="mailto:[email protected]">Jenly</a>
*/
public class CaptureActivity extends AppCompatActivity implements OnCaptureCallback{

public static final String KEY_RESULT = Intents.Scan.RESULT;

Expand Down Expand Up @@ -114,9 +117,10 @@ public CaptureHelper getCaptureHelper(){
}

/**
* Get {@link CameraManager}
* Get {@link CameraManager} use {@link #getCaptureHelper()#getCameraManager()}
* @return {@link #mCaptureHelper#getCameraManager()}
*/
@Deprecated
public CameraManager getCameraManager(){
return mCaptureHelper.getCameraManager();
}
Expand Down Expand Up @@ -154,4 +158,4 @@ public boolean onTouchEvent(MotionEvent event) {
public boolean onResultCallback(String result) {
return false;
}
}
}
5 changes: 3 additions & 2 deletions lib/src/main/java/com/king/zxing/CaptureFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class CaptureFragment extends Fragment implements OnCaptureCallback {
public static CaptureFragment newInstance() {

Bundle args = new Bundle();

CaptureFragment fragment = new CaptureFragment();
fragment.setArguments(args);
return fragment;
Expand Down Expand Up @@ -127,9 +127,10 @@ public CaptureHelper getCaptureHelper(){
}

/**
* Get {@link CameraManager}
* Get {@link CameraManager} use {@link #getCaptureHelper()#getCameraManager()}
* @return {@link #mCaptureHelper#getCameraManager()}
*/
@Deprecated
public CameraManager getCameraManager(){
return mCaptureHelper.getCameraManager();
}
Expand Down
13 changes: 13 additions & 0 deletions lib/src/main/java/com/king/zxing/CaptureHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public class CaptureHandler extends Handler implements ResultPointCallback {
*/
private boolean isSupportAutoZoom;

/**
*
*/
private boolean isSupportLuminanceInvert;


private enum State {
PREVIEW,
Expand Down Expand Up @@ -198,4 +203,12 @@ public boolean isSupportAutoZoom() {
public void setSupportAutoZoom(boolean supportAutoZoom) {
isSupportAutoZoom = supportAutoZoom;
}

public boolean isSupportLuminanceInvert() {
return isSupportLuminanceInvert;
}

public void setSupportLuminanceInvert(boolean supportLuminanceInvert) {
isSupportLuminanceInvert = supportLuminanceInvert;
}
}
21 changes: 20 additions & 1 deletion lib/src/main/java/com/king/zxing/CaptureHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
*/
private boolean isSupportAutoZoom = true;

/**
* 是否支持识别颜色反转色的码,黑白颜色反转,默认不支持
*/
private boolean isSupportLuminanceInvert = false;

/**
* 是否支持连扫,默认不支持
*/
Expand Down Expand Up @@ -334,6 +339,7 @@ private void initCamera(SurfaceHolder surfaceHolder) {
captureHandler.setSupportVerticalCode(isSupportVerticalCode);
captureHandler.setReturnBitmap(isReturnBitmap);
captureHandler.setSupportAutoZoom(isSupportAutoZoom);
captureHandler.setSupportLuminanceInvert(isSupportLuminanceInvert);
}
} catch (IOException ioe) {
Log.w(TAG, ioe);
Expand Down Expand Up @@ -718,9 +724,22 @@ public CaptureHelper supportAutoZoom(boolean supportAutoZoom) {
return this;
}

/**
* 是否支持识别反色码,黑白颜色反转
* @param supportLuminanceInvert 默认为false,当返回true时表示支持,会增加识别率,但相应的也会增加性能消耗。
* @return
*/
public CaptureHelper supportLuminanceInvert(boolean supportLuminanceInvert) {
isSupportLuminanceInvert = supportLuminanceInvert;
if(captureHandler!=null){
captureHandler.setSupportLuminanceInvert(isSupportLuminanceInvert);
}
return this;
}

/**
* 设置是否支持全屏扫码识别
* @param fullScreenScan
* @param fullScreenScan 默认为false
* @return
*/
public CaptureHelper fullScreenScan(boolean fullScreenScan) {
Expand Down
54 changes: 36 additions & 18 deletions lib/src/main/java/com/king/zxing/DecodeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,50 @@ private void decode(byte[] data, int width, int height,boolean isScreenPortrait,
PlanarYUVLuminanceSource source = buildPlanarYUVLuminanceSource(data,width,height,isScreenPortrait);

if (source != null) {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

boolean isReDecode;
try {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
rawResult = multiFormatReader.decodeWithState(bitmap);
isReDecode = false;
} catch (Exception e) {
BinaryBitmap bitmap1 = new BinaryBitmap(new GlobalHistogramBinarizer(source));
isReDecode = true;
}

if(isReDecode && handler.isSupportLuminanceInvert()){
try {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source.invert()));
rawResult = multiFormatReader.decodeWithState(bitmap);
isReDecode = false;
} catch (Exception e) {
isReDecode = true;
}
}

if(isReDecode){
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){

}
}
BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
rawResult = multiFormatReader.decodeWithState(bitmap);
isReDecode = false;
}catch (Exception e){
isReDecode = true;
}
}

if(isReDecode && isSupportVerticalCode){
source = buildPlanarYUVLuminanceSource(data,width,height,!isScreenPortrait);
if(source!=null){
try{
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
rawResult = multiFormatReader.decodeWithState(bitmap);
}catch (Exception e){

}
}

} finally {
multiFormatReader.reset();
}

multiFormatReader.reset();
}

if (rawResult != null) {
Expand Down Expand Up @@ -157,7 +176,6 @@ private void decode(byte[] data, int width, int height,boolean isScreenPortrait,
}
}


}

if (handler != null) {
Expand Down Expand Up @@ -211,7 +229,7 @@ private boolean handleAutoZoom(int length,int width){
return true;
}

if(length<width/5){
if(length < width/ 5){

Camera camera = cameraManager.getOpenCamera().getCamera();
if(camera!=null){
Expand Down

0 comments on commit 42a5cc9

Please sign in to comment.