Skip to content

Commit

Permalink
优化定位
Browse files Browse the repository at this point in the history
  • Loading branch information
ailiwean committed May 3, 2020
1 parent 4231d48 commit 4171428
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 57 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/android/NBZxing/CusZxingView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

/***
Expand Down
12 changes: 12 additions & 0 deletions module_camera/src/main/java/com/ailiwean/core/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class Result {

PointF pointF;

boolean isRotate;

public String getText() {
return text;
}
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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<ResultPoint> 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) {
Expand All @@ -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_;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -52,21 +52,23 @@ 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;
float preX = Config.scanRect.getPreX();
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
Expand All @@ -77,20 +79,24 @@ 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;
float preX = Config.scanRect.getPreX();
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.
Expand All @@ -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);
}

/***
Expand All @@ -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();
}
}
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/***
* 实际预览区域/总预览区域
Expand All @@ -44,7 +45,6 @@ public ScanRect setScanR(Rect scanR) {
return this;
}


public Rect getScanRR() {
return scanRR;
}
Expand Down

0 comments on commit 4171428

Please sign in to comment.