当我尝试更改 zxing 二维码阅读器的扫描仪矩形的尺寸时,它会更改它,但它会停止扫描二维码。我尝试以下更改矩形的尺寸:
onDraw()
函数ViewFinderView.java
,我调用了frame.set(0,0,300,300)
cameraManager.setManualFramingRect(300,300);
两者都更改了矩形的尺寸,但随后停止扫描代码。请在这里帮助我..提前谢谢..
如果您使用 ZXing 作为库并且无法修改源代码
您可以创建一个自定义类来定义所有相机预览行为,包括删除红线、使其成为正方形、添加自定义徽标、扫描 QR 码。您所要做的就是在您的项目中创建以下类
public class CustomZXingScannerView extends View implements IViewFinder {
private static final String TAG = "ViewFinderView";
private Rect mFramingRect;
private static final float PORTRAIT_WIDTH_RATIO = 6f/8;
private static final float PORTRAIT_WIDTH_HEIGHT_RATIO = 0.75f;
private static final float LANDSCAPE_HEIGHT_RATIO = 5f/8;
private static final float LANDSCAPE_WIDTH_HEIGHT_RATIO = 1.4f;
private static final int MIN_DIMENSION_DIFF = 50;
private static final float SQUARE_DIMENSION_RATIO = 5f/8;
private static final int[] SCANNER_ALPHA = {0, 64, 128, 192, 255, 192, 128, 64};
private int scannerAlpha;
private static final int POINT_SIZE = 10;
private static final long ANIMATION_DELAY = 80l;
private final int mDefaultLaserColor = getResources().getColor(R.color.viewfinder_laser);
private final int mDefaultMaskColor = getResources().getColor(R.color.viewfinder_mask);
private final int mDefaultBorderColor = getResources().getColor(R.color.viewfinder_border);
private final int mDefaultBorderStrokeWidth = getResources().getInteger(R.integer.viewfinder_border_width);
private final int mDefaultBorderLineLength = getResources().getInteger(R.integer.viewfinder_border_length);
protected Paint mLaserPaint;
protected Paint mFinderMaskPaint;
protected Paint mBorderPaint;
protected int mBorderLineLength;
protected boolean mSquareViewFinder;
public CustomZXingScannerView(Context context) {
super(context);
init();
}
public CustomZXingScannerView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
//set up laser paint
mLaserPaint = new Paint();
mLaserPaint.setColor(mDefaultLaserColor);
mLaserPaint.setStyle(Paint.Style.FILL);
//finder mask paint
mFinderMaskPaint = new Paint();
mFinderMaskPaint.setColor(mDefaultMaskColor);
//border paint
mBorderPaint = new Paint();
mBorderPaint.setColor(mDefaultBorderColor);
mBorderPaint.setStyle(Paint.Style.STROKE);
mBorderPaint.setStrokeWidth(mDefaultBorderStrokeWidth);
mBorderLineLength = mDefaultBorderLineLength;
}
public void setLaserColor(int laserColor) {
mLaserPaint.setColor(laserColor);
}
public void setMaskColor(int maskColor) {
mFinderMaskPaint.setColor(maskColor);
}
public void setBorderColor(int borderColor) {
mBorderPaint.setColor(borderColor);
}
public void setBorderStrokeWidth(int borderStrokeWidth) {
mBorderPaint.setStrokeWidth(borderStrokeWidth);
}
public void setBorderLineLength(int borderLineLength) {
mBorderLineLength = borderLineLength;
}
// TODO: Need a better way to configure this. Revisit when working on 2.0
public void setSquareViewFinder(boolean set) {
mSquareViewFinder = set;
}
public void setupViewFinder() {
updateFramingRect();
invalidate();
}
public Rect getFramingRect() {
return mFramingRect;
}
@Override
public void onDraw(Canvas canvas) {
if(getFramingRect() == null) {
return;
}
drawViewFinderMask(canvas);
drawViewFinderBorder(canvas);
drawLaser(canvas);
}
public void drawViewFinderMask(Canvas canvas) {
int width = canvas.getWidth();
int height = canvas.getHeight();
Rect framingRect = getFramingRect();
canvas.drawRect(0, 0, width, framingRect.top, mFinderMaskPaint);
canvas.drawRect(0, framingRect.top, framingRect.left, framingRect.bottom + 1, mFinderMaskPaint);
canvas.drawRect(framingRect.right + 1, framingRect.top, width, framingRect.bottom + 1, mFinderMaskPaint);
canvas.drawRect(0, framingRect.bottom + 1, width, height, mFinderMaskPaint);
}
public void drawViewFinderBorder(Canvas canvas) {
Rect framingRect = getFramingRect();
canvas.drawLine(framingRect.left - 1, framingRect.top - 1, framingRect.left - 1, framingRect.top - 1 + mBorderLineLength, mBorderPaint);
canvas.drawLine(framingRect.left - 1, framingRect.top - 1, framingRect.left - 1 + mBorderLineLength, framingRect.top - 1, mBorderPaint);
canvas.drawLine(framingRect.left - 1, framingRect.bottom + 1, framingRect.left - 1, framingRect.bottom + 1 - mBorderLineLength, mBorderPaint);
canvas.drawLine(framingRect.left - 1, framingRect.bottom + 1, framingRect.left - 1 + mBorderLineLength, framingRect.bottom + 1, mBorderPaint);
canvas.drawLine(framingRect.right + 1, framingRect.top - 1, framingRect.right + 1, framingRect.top - 1 + mBorderLineLength, mBorderPaint);
canvas.drawLine(framingRect.right + 1, framingRect.top - 1, framingRect.right + 1 - mBorderLineLength, framingRect.top - 1, mBorderPaint);
canvas.drawLine(framingRect.right + 1, framingRect.bottom + 1, framingRect.right + 1, framingRect.bottom + 1 - mBorderLineLength, mBorderPaint);
canvas.drawLine(framingRect.right + 1, framingRect.bottom + 1, framingRect.right + 1 - mBorderLineLength, framingRect.bottom + 1, mBorderPaint);
}
public void drawLaser(Canvas canvas) {
Rect framingRect = getFramingRect();
// Draw a red "laser scanner" line through the middle to show decoding is active
mLaserPaint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
int middle = framingRect.height() / 2 + framingRect.top;
canvas.drawRect(framingRect.left + 2, middle - 1, framingRect.right - 1, middle + 2, mLaserPaint);
postInvalidateDelayed(ANIMATION_DELAY,
framingRect.left - POINT_SIZE,
framingRect.top - POINT_SIZE,
framingRect.right + POINT_SIZE,
framingRect.bottom + POINT_SIZE);
}
@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {
updateFramingRect();
}
public synchronized void updateFramingRect() {
Point viewResolution = new Point(getWidth(), getHeight());
int width;
int height;
int orientation = DisplayUtils.getScreenOrientation(getContext());
if(mSquareViewFinder) {
if(orientation != Configuration.ORIENTATION_PORTRAIT) {
height = (int) (getHeight() * SQUARE_DIMENSION_RATIO);
width = height;
} else {
width = (int) (getWidth() * SQUARE_DIMENSION_RATIO);
height = width;
}
} else {
if(orientation != Configuration.ORIENTATION_PORTRAIT) {
height = (int) (getHeight() * LANDSCAPE_HEIGHT_RATIO);
width = (int) (LANDSCAPE_WIDTH_HEIGHT_RATIO * height);
} else {
width = (int) (getWidth() * PORTRAIT_WIDTH_RATIO);
height = (int) (PORTRAIT_WIDTH_HEIGHT_RATIO * width);
}
}
if(width > getWidth()) {
width = getWidth() - MIN_DIMENSION_DIFF;
}
if(height > getHeight()) {
height = getHeight() - MIN_DIMENSION_DIFF;
}
int leftOffset = (viewResolution.x - width) / 2;
int topOffset = (viewResolution.y - height) / 2;
mFramingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);
}
}
您可以根据需要修改任何属性或方法。
之后,在创建 ZXing 活动的活动中,修改 onCreate 方法以使用您刚刚复制粘贴的类:
public class SimpleScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private static final String TAG = "INDIGO";
private ZXingScannerView mScannerView;
public static String SCANNER_GIVE_RESULT = "SCANNER_GIVE_RESULT";
public static String SCANNER_RESULT = "SCANNER_RESULT";
private boolean returnResult;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
mScannerView = new ZXingScannerView(this) {
@Override
protected IViewFinder createViewFinderView(Context context) {
return new CustomZXingScannerView(context);
}
}; // Programmatically initialize the scanner view
List<BarcodeFormat> formats = new ArrayList<>();
formats.add(BarcodeFormat.QR_CODE);
setContentView(mScannerView); // Set the scanner view as the content view
returnResult = getIntent().getBooleanExtra(SCANNER_GIVE_RESULT, false);
}
CameraManager 类有两个常量,分别定义为 MIN_FRAME_WIDTH 和 MIN_FRAME_HEIGHT。您应该根据需要修改它们,并且一切都应该正常工作:
私有静态最终 int MIN_FRAME_WIDTH = 240; //(这里是你想要的值) private static final int MIN_FRAME_HEIGHT = 240; //(您想要的值在这里)
您还可以在创建实际矩形的同一类中修改方法 getFramingRect()。
-- 来自另一篇类似的帖子如何在 zxing 中增加扫描区域大小
晚了,但更好的方法是使用 setScanningRectangle。
IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity());
scanIntegrator.setScanningRectangle(width, height);
scanIntegrator.initiateScan(IntentIntegrator.QR_CODE_TYPES);