diff --git a/app/src/main/java/com/android/NBZxing/CusZxingView.kt b/app/src/main/java/com/android/NBZxing/CusZxingView.kt index e163b94..0b53add 100644 --- a/app/src/main/java/com/android/NBZxing/CusZxingView.kt +++ b/app/src/main/java/com/android/NBZxing/CusZxingView.kt @@ -17,7 +17,7 @@ import com.ailiwean.core.zxing.ScanTypeConfig class CusZxingView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null, def: Int = 0) : ZxingCameraView(context, attributeSet, def) { override fun resultBack(content: String) { - Toast.makeText(context, content, Toast.LENGTH_SHORT).show() + // Toast.makeText(context, content, Toast.LENGTH_SHORT).show() } /*** diff --git a/module_camera/src/main/java/com/ailiwean/core/Result.java b/module_camera/src/main/java/com/ailiwean/core/Result.java index 8642209..d086737 100644 --- a/module_camera/src/main/java/com/ailiwean/core/Result.java +++ b/module_camera/src/main/java/com/ailiwean/core/Result.java @@ -17,6 +17,8 @@ public class Result { PointF pointF; + boolean isRotate; + public String getText() { return text; } @@ -35,6 +37,16 @@ public Result setPointF(PointF pointF) { return this; } + + public boolean isRotate() { + return isRotate; + } + + public Result setRotate(boolean rotate) { + isRotate = rotate; + return this; + } + @NonNull @Override public String toString() { diff --git a/module_camera/src/main/java/com/ailiwean/core/able/XQRScanAbleRotate.java b/module_camera/src/main/java/com/ailiwean/core/able/XQRScanAbleRotate.java index 0710749..b87c028 100644 --- a/module_camera/src/main/java/com/ailiwean/core/able/XQRScanAbleRotate.java +++ b/module_camera/src/main/java/com/ailiwean/core/able/XQRScanAbleRotate.java @@ -5,11 +5,9 @@ import com.ailiwean.core.Config; import com.ailiwean.core.helper.ScanHelper; +import com.ailiwean.core.zxing.CustomMultiFormatReader; +import com.google.zxing.BinaryBitmap; import com.google.zxing.Result; -import com.google.zxing.ResultPoint; - -import java.util.ArrayList; -import java.util.List; /** * @Package: com.ailiwean.core.able @@ -18,7 +16,11 @@ * @Author: SWY * @CreateDate: 2020/5/3 2:32 PM */ -public class XQRScanAbleRotate extends XQRScanAble { +public class XQRScanAbleRotate extends PixsValuesAble { + + CustomMultiFormatReader reader = CustomMultiFormatReader.getInstance(); + protected Result result; + BinaryBitmap binaryBitmap; XQRScanAbleRotate(Handler handler) { super(handler); @@ -32,27 +34,13 @@ public void cusAction(byte[] data, int dataWidth, int dataHeight) { dataWidth += dataHeight; dataHeight = dataWidth - dataHeight; dataWidth -= dataHeight; + //先生产扫码需要的BinaryBitmap binaryBitmap = ScanHelper.byteToBinaryBitmap(data, dataWidth, dataHeight); result = reader.decode(binaryBitmap); if (result != null) { - result = rotateResultPoint(result); Message.obtain(handler, Config.SCAN_RESULT, covertResult(result)).sendToTarget(); } - - } - - private Result rotateResultPoint(Result result) { - List resultPoints = new ArrayList<>(); - for (ResultPoint point : result.getResultPoints()) { - resultPoints.add(new ResultPoint(point.getY(), point.getX())); - } - return new Result(result.getText(), result.getRawBytes(), - result.getNumBits(), - resultPoints.toArray(new ResultPoint[]{}), - result.getBarcodeFormat(), - result.getTimestamp() - ); } private byte[] rotateByte(byte[] data, int dataWidth, int dataHeight) { @@ -65,10 +53,11 @@ private byte[] rotateByte(byte[] data, int dataWidth, int dataHeight) { return rotatedData; } - protected com.ailiwean.core.Result covertResult(Result result) { + private com.ailiwean.core.Result covertResult(Result result) { com.ailiwean.core.Result result_ = new com.ailiwean.core.Result(); result_.setText(result.getText()); result_.setPointF(ScanHelper.rotatePointR(result.getResultPoints())); + result_.setRotate(true); return result_; } } diff --git a/module_camera/src/main/java/com/ailiwean/core/able/XQRScanZoomAble.java b/module_camera/src/main/java/com/ailiwean/core/able/XQRScanZoomAble.java index 61dd782..a9d0fa5 100644 --- a/module_camera/src/main/java/com/ailiwean/core/able/XQRScanZoomAble.java +++ b/module_camera/src/main/java/com/ailiwean/core/able/XQRScanZoomAble.java @@ -2,11 +2,9 @@ import android.os.Handler; import android.os.Message; -import android.util.Log; import com.ailiwean.core.Config; import com.ailiwean.core.helper.ScanHelper; -import com.google.zxing.BinaryBitmap; import com.google.zxing.FormatException; import com.google.zxing.NotFoundException; import com.google.zxing.ResultPoint; diff --git a/module_camera/src/main/java/com/ailiwean/core/helper/ScanHelper.java b/module_camera/src/main/java/com/ailiwean/core/helper/ScanHelper.java index 0356a44..5bbed3a 100644 --- a/module_camera/src/main/java/com/ailiwean/core/helper/ScanHelper.java +++ b/module_camera/src/main/java/com/ailiwean/core/helper/ScanHelper.java @@ -1,8 +1,8 @@ package com.ailiwean.core.helper; -import android.graphics.Point; import android.graphics.PointF; import android.graphics.Rect; +import android.util.Log; import com.ailiwean.core.Config; import com.ailiwean.core.zxing.PlanarYUVLuminanceSource; @@ -52,10 +52,13 @@ public static PointF rotatePoint(ResultPoint[] point) { if (point == null || point.length == 0) return new PointF(0, 0); + if (Config.scanRect.getScanR() == null) + return new PointF(0, 0); + PointF avargPoint = new PointF(); for (ResultPoint item : point) { - avargPoint.x += item.getX(); - avargPoint.y += item.getY(); + avargPoint.x += Math.abs(item.getX()); + avargPoint.y += Math.abs(item.getY()); } avargPoint.x /= point.length; avargPoint.y /= point.length; @@ -63,10 +66,9 @@ public static PointF rotatePoint(ResultPoint[] point) { float preY = Config.scanRect.getPreY(); float aspX = preX / (float) Config.scanRect.getScanR().height(); float aspY = preY / (float) Config.scanRect.getScanR().width(); - return new PointF(preX - aspX * avargPoint.y, preY - aspY * avargPoint.x); + return new PointF(preX - aspX * avargPoint.y, aspY * avargPoint.x); } - /*** * 二维码坐标转换屏幕坐标 * @param point @@ -77,10 +79,13 @@ public static PointF rotatePointR(ResultPoint[] point) { if (point == null || point.length == 0) return new PointF(0, 0); + if (Config.scanRect.getScanRR() == null) + return new PointF(0, 0); + PointF avargPoint = new PointF(); for (ResultPoint item : point) { - avargPoint.x += item.getX(); - avargPoint.y += item.getY(); + avargPoint.x += Math.abs(item.getX()); + avargPoint.y += Math.abs(item.getY()); } avargPoint.x /= point.length; avargPoint.y /= point.length; @@ -88,9 +93,10 @@ public static PointF rotatePointR(ResultPoint[] point) { float preY = Config.scanRect.getPreY(); float aspX = preX / (float) Config.scanRect.getScanRR().width(); float aspY = preY / (float) Config.scanRect.getScanRR().height(); - return new PointF(preX - aspX * avargPoint.y, preY - aspY * avargPoint.x); + return new PointF(aspX * avargPoint.x, aspY * avargPoint.y); } + /** * A factory method to build the appropriate LuminanceSource object based on the format * of the preview buffers, as described by Camera.Parameters. @@ -109,7 +115,7 @@ private static PlanarYUVLuminanceSource buildLuminanceSource(byte[] data, int wi return null; } return new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top, - rect.width(), rect.height(), true); + rect.width(), rect.height(), false); } /*** @@ -118,24 +124,21 @@ private static PlanarYUVLuminanceSource buildLuminanceSource(byte[] data, int wi */ public static Rect getScanByteRect(int dataWidth, int dataHeight) { if (dataWidth > dataHeight) { - if (Config.scanRect.getScanR() == null) { + if (Config.scanRect.getScanR() == null) Config.scanRect.setScanR(new Rect()); - Config.scanRect.getScanR().left = (int) (Config.scanRect.getRect().left * dataHeight); - Config.scanRect.getScanR().top = (int) (Config.scanRect.getRect().top * dataWidth); - Config.scanRect.getScanR().right = (int) (Config.scanRect.getRect().right * dataHeight); - Config.scanRect.getScanR().bottom = (int) (Config.scanRect.getRect().bottom * dataWidth); - Config.scanRect.setScanR(rotateRect(Config.scanRect.getScanR())); - } + Config.scanRect.getScanR().left = (int) (Config.scanRect.getRect().left * dataHeight); + Config.scanRect.getScanR().top = (int) (Config.scanRect.getRect().top * dataWidth); + Config.scanRect.getScanR().right = (int) (Config.scanRect.getRect().right * dataHeight); + Config.scanRect.getScanR().bottom = (int) (Config.scanRect.getRect().bottom * dataWidth); + Config.scanRect.setScanR(rotateRect(Config.scanRect.getScanR())); return Config.scanRect.getScanR(); } else { - if (Config.scanRect.getScanRR() == null) { + if (Config.scanRect.getScanRR() == null) Config.scanRect.setScanRR(new Rect()); - Config.scanRect.getScanRR().left = (int) (Config.scanRect.getRect().left * dataWidth); - Config.scanRect.getScanRR().top = (int) (Config.scanRect.getRect().top * dataHeight); - Config.scanRect.getScanRR().right = (int) (Config.scanRect.getRect().right * dataWidth); - Config.scanRect.getScanRR().bottom = (int) (Config.scanRect.getRect().bottom * dataHeight); - Config.scanRect.setScanRR(Config.scanRect.getScanRR()); - } + Config.scanRect.getScanRR().left = (int) (Config.scanRect.getRect().left * dataWidth); + Config.scanRect.getScanRR().top = (int) (Config.scanRect.getRect().top * dataHeight); + Config.scanRect.getScanRR().right = (int) (Config.scanRect.getRect().right * dataWidth); + Config.scanRect.getScanRR().bottom = (int) (Config.scanRect.getRect().bottom * dataHeight); return Config.scanRect.getScanRR(); } } @@ -144,6 +147,10 @@ public static Rect getScanByteRect(int dataWidth, int dataHeight) { * 计算探测器获取二维码大小 */ public static int getQrLenght(ResultPoint[] point) { + + if (Config.scanRect.getScanR() == null) + return 0; + //计算中心点坐标 PointF avargPoint = new PointF(); for (ResultPoint item : point) { diff --git a/module_camera/src/main/java/com/ailiwean/core/view/ZxingCameraView.kt b/module_camera/src/main/java/com/ailiwean/core/view/ZxingCameraView.kt index 6caf40b..fafc6f6 100644 --- a/module_camera/src/main/java/com/ailiwean/core/view/ZxingCameraView.kt +++ b/module_camera/src/main/java/com/ailiwean/core/view/ZxingCameraView.kt @@ -11,6 +11,7 @@ import android.util.AttributeSet import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.Toast import com.ailiwean.core.Config.* import com.ailiwean.core.Result import com.ailiwean.core.Utils @@ -51,8 +52,8 @@ abstract class ZxingCameraView @JvmOverloads constructor(context: Context, attri SCAN_RESULT -> { scanSucHelper() if (it.obj is Result) { - showQRLoc((it.obj as Result).pointF, it.obj.toString() - ) + showQRLoc((it.obj as Result).pointF, it.obj.toString()) + Toast.makeText(context, (it.obj as Result).isRotate.toString(), Toast.LENGTH_SHORT).show() } } LIGHT_CHANGE -> { diff --git a/module_camera/src/main/java/com/ailiwean/core/zxing/ScanRect.java b/module_camera/src/main/java/com/ailiwean/core/zxing/ScanRect.java index 41727f9..b296177 100644 --- a/module_camera/src/main/java/com/ailiwean/core/zxing/ScanRect.java +++ b/module_camera/src/main/java/com/ailiwean/core/zxing/ScanRect.java @@ -12,15 +12,16 @@ */ public class ScanRect { - int dataX; - int dataY; + private int dataX; + private int dataY; - int preX; - int preY; + private int preX; + private int preY; + + private RectF r = new RectF(); + private Rect scanR = new Rect(); + private Rect scanRR = new Rect(); - RectF r; - Rect scanR; - Rect scanRR; /*** * 实际预览区域/总预览区域 @@ -44,7 +45,6 @@ public ScanRect setScanR(Rect scanR) { return this; } - public Rect getScanRR() { return scanRR; }