Skip to content

Commit

Permalink
支持黑边二维码识别
Browse files Browse the repository at this point in the history
  • Loading branch information
ailiwean committed Jan 11, 2021
1 parent 31c5556 commit bddd23b
Show file tree
Hide file tree
Showing 15 changed files with 701 additions and 260 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ android {
applicationId "com.android.NBZxing"
minSdkVersion 17
targetSdkVersion 30
versionCode 21
versionName "1.30"
versionCode 22
versionName "1.40"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
signingConfig signingConfigs.release
compileOptions {
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/com/android/NBZxing/CusScanView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CusScanView @JvmOverloads constructor(context: Context, attributeSet: Attr
* 4 ScanTypeConfig.TWO_DIMENSION 所有二维码格式
* 5 ScanTypeConfig.ONE_DIMENSION 所有一维码格式
*/
override fun getScanType(): ScanTypeConfig {
override fun configScanType(): ScanTypeConfig {
return ScanTypeConfig.HIGH_FREQUENCY
}

Expand All @@ -47,4 +47,12 @@ class CusScanView @JvmOverloads constructor(context: Context, attributeSet: Attr
Toast.makeText(context, "未扫描到内容", Toast.LENGTH_SHORT).show()
else Toast.makeText(context, content, Toast.LENGTH_SHORT).show()
}

/***
* 是否支持黑边二维码扫描
*/
override fun isSupportBlackEdgeQrScan(): Boolean {
return true
}

}
2 changes: 1 addition & 1 deletion app/src/main/java/com/android/NBZxing/CusScanView2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CusScanView2 @JvmOverloads constructor(context: Context, attributeSet: Att
* 4 ScanTypeConfig.TWO_DIMENSION 所有二维码格式
* 5 ScanTypeConfig.ONE_DIMENSION 所有一维码格式
*/
override fun getScanType(): ScanTypeConfig {
override fun configScanType(): ScanTypeConfig {
return ScanTypeConfig.HIGH_FREQUENCY
}

Expand Down
11 changes: 10 additions & 1 deletion module_camera/src/main/java/com/ailiwean/core/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Config {
//自动缩放
public static final int AUTO_ZOOM = 2;

//实时位置
//实时二维码探测点位置
public static final int RT_LOCATION = 3;

//###############################################
Expand Down Expand Up @@ -63,6 +63,9 @@ public static boolean is270() {
return displayOrientation == 270;
}

/*###############################################*/


//灰度算法类路径
public static String GARY_SCALE_PATH = "com.ailiwean.module_grayscale.GrayScaleDispatch";

Expand All @@ -74,4 +77,10 @@ public static boolean hasDepencidesScale() {
}
return true;
}

/*###############################################*/

//是否支持黑边二维码识别
public static boolean isSupportBlackEdge = true;

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class AbleManager private constructor(handler: Handler) : PixsValuesAble(handler

fun loadAbility() {
ableList.clear()
// ableList.add(XQRScanCrudeAble(handlerHolder.get()))
ableList.add(XQRScanCrudeAble(handlerHolder.get()))
ableList.add(XQRScanZoomAble(handlerHolder.get()))
// ableList.add(XQRScanAbleRotate(handlerHolder.get()))
ableList.add(XQRScanAbleRotate(handlerHolder.get()))
ableList.add(LighSolveAble(handlerHolder.get()))
// ableList.add(XQRScanAble(handlerHolder.get()))
// ableList.add(GrayscaleStrengAble(handlerHolder.get()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected void needParseDeploy(PlanarYUVLuminanceSource source, boolean isNative
if (result != null && result.getText() != null)
return;
result = toLaunchParse(source.getHybridBinary());
if (result != null && !"".equals(result.getText()) && result.getText() != null) {
if (result != null && result.getText() != null && !"".equals(result.getText())) {
sendMessage(Config.RT_LOCATION,
ScanHelper.rotatePoint(result.getResultPoints()));
sendMessage(Config.SCAN_RESULT, covertResult(result));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class XQRScanAbleRotate extends PixsValuesAble {
@Override
protected void needParseDeploy(PlanarYUVLuminanceSource source, boolean isNative) {
result = toLaunchParse(new HybridBinarizer(source.onlyCopyWarpRotate()));
if (result != null && !"".equals(result.getText()) && result.getText() != null) {
if (result != null && result.getText() != null && !"".equals(result.getText())) {
sendMessage(Config.SCAN_RESULT, covertResultRotate(result));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected void needParseDeploy(PlanarYUVLuminanceSource source, boolean isNative
if (result != null && result.getText() != null)
return;
result = toLaunchParse(source.getHybridBinaryCurde());
if (result != null && !"".equals(result.getText()) && result.getText() != null) {
if (result != null && result.getText() != null && !"".equals(result.getText())) {
sendMessage(Config.SCAN_RESULT, covertResult(result));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ protected void needParseDeploy(PlanarYUVLuminanceSource source, boolean isNative
} else if (lenght < Config.scanRect.getPreX() / 3 * 2) {
Config.currentZoom += 0.07;
}

zoomTime = System.currentTimeMillis();
lastLenght = lenght;

// sendMessage(Config.AUTO_ZOOM, Config.currentZoom + "");
sendMessage(Config.AUTO_ZOOM, Config.currentZoom + "");
}
}
61 changes: 36 additions & 25 deletions module_camera/src/main/java/com/ailiwean/core/view/FreeZxingView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ package com.ailiwean.core.view


import android.content.Context
import android.graphics.*
import android.graphics.Bitmap
import android.os.Handler
import android.os.HandlerThread
import android.os.Looper
import android.os.Message
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.Toast
import com.ailiwean.core.Config.*
import com.ailiwean.core.Result
import com.ailiwean.core.able.AbleManager
Expand Down Expand Up @@ -43,6 +41,7 @@ abstract class FreeZxingView @JvmOverloads constructor(context: Context, attribu

//配置扫码类型
initScanType()

}

/***
Expand Down Expand Up @@ -107,34 +106,37 @@ abstract class FreeZxingView @JvmOverloads constructor(context: Context, attribu
setZoom(message.obj.toString().toFloat())
}

//实时位置
//实时探测点位置
RT_LOCATION -> {
poinF = message.obj as Array<PointF>
invalidate()
//数组长度为3
// poinF = message.obj as Array<PointF>
// invalidate()
}
}
}

return true
}

var poinF: Array<PointF>? = null

val paint by lazy {
val paint = Paint()
paint.color = Color.RED
paint.style = Paint.Style.FILL
paint
}


override fun draw(canvas: Canvas?) {
super.draw(canvas)
if (poinF != null)
poinF?.forEach {
canvas?.drawCircle(it.x, it.y, 5f, paint)
}
}
// var poinF: Array<PointF>? = null

// val paint by lazy {
// val paint = Paint()
// paint.color = Color.RED
// paint.textSize = 15f
// paint.style = Paint.Style.FILL
// paint
// }
//
//
// override fun draw(canvas: Canvas?) {
// super.draw(canvas)
// if (poinF != null)
// poinF?.forEachIndexed { index, it ->
//// canvas?.drawText("$index", pointF.x, pointF.y, paint)
// canvas?.drawCircle(it.x, it.y, 5f, paint)
// }
// }

/***
* 相机采集数据实时回调
Expand Down Expand Up @@ -269,7 +271,8 @@ abstract class FreeZxingView @JvmOverloads constructor(context: Context, attribu
* 配置扫码类型
*/
private fun initScanType() {
scanTypeConfig = getScanType()
scanTypeConfig = configScanType()
isSupportBlackEdge = isSupportBlackEdgeQrScan()
}

/***
Expand Down Expand Up @@ -317,10 +320,18 @@ abstract class FreeZxingView @JvmOverloads constructor(context: Context, attribu
/***
* 提供扫码类型
*/
open fun getScanType(): ScanTypeConfig {
open fun configScanType(): ScanTypeConfig {
return ScanTypeConfig.HIGH_FREQUENCY
}

/***
* 是否支持黑边二维码识别-会导致缩放变得灵敏
* 默认支持
*/
open fun isSupportBlackEdgeQrScan(): Boolean {
return true
}

/***
* 业务Handler
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.ailiwean.core.zxing.core.qrcode;

import com.ailiwean.core.Config;
import com.ailiwean.core.zxing.core.BarcodeFormat;
import com.ailiwean.core.zxing.core.BinaryBitmap;
import com.ailiwean.core.zxing.core.ChecksumException;
Expand All @@ -32,6 +33,7 @@
import com.ailiwean.core.zxing.core.qrcode.decoder.Decoder;
import com.ailiwean.core.zxing.core.qrcode.decoder.QRCodeDecoderMetaData;
import com.ailiwean.core.zxing.core.qrcode.detector.Detector;
import com.ailiwean.core.zxing.core.qrcode.detector.Detector2;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -83,7 +85,14 @@ public final Result decode(BinaryBitmap image, Map<DecodeHintType, ?> hints)
try {
detectorResult = new Detector(image.getBlackMatrix()).detect(hints);
} catch (NotFoundException e) {
return null;
if (Config.isSupportBlackEdge) {
try {
detectorResult = new Detector2(image.getBlackMatrix()).detect(hints);
} catch (NotFoundException notFoundException) {
return null;
}
} else
return null;
}
points = detectorResult.getPoints();
try {
Expand Down Expand Up @@ -207,7 +216,8 @@ private static BitMatrix extractPureBits(BitMatrix image) throws NotFoundExcepti
return bits;
}

private static float moduleSize(int[] leftTopBlack, BitMatrix image) throws NotFoundException {
private static float moduleSize(int[] leftTopBlack, BitMatrix image) throws
NotFoundException {
int height = image.getHeight();
int width = image.getWidth();
int x = leftTopBlack[0];
Expand Down
Loading

0 comments on commit bddd23b

Please sign in to comment.