ZXing: Finding the bounding rectangle of Barcode

梦想与她 提交于 2019-12-04 07:43:41

Via ResultPoints you have the sides of the barcode. Via WhiteRectangleDetector (WRDet) you have the y coordinate of the top and the height of the barcode. Putting all this information together will get you the exact coordinates!

Breaking it down explicitly:

  • left-top: y value of top from WRDet, x value via the most left ResultPoint
  • right-top: y value of top from WRDet, x value via the most right ResultPoint
  • left-bottom: y value of top from WRDet + height from WRDet, x value via the most left ResultPoint
  • right-bottom: y value of top from WRDet + height from WRDet, x value via the most right ResultPoint

It might seem overkill to call for the ResultPoints and to call the WRDet, but the WRDet algorithm is very fast if the initial search center is inside the barcode. The initial search center can be modified using following constructor:

public WhiteRectangleDetector(BitMatrix image, int initSize, int x, int y)

You know x should lie between the left and right ResultPoint, and for the y value you can choose the y value of one of the ResultPoints.


As an aside, here is the short explanation as to why WhiteRectangleDetector only captures a horizontal fraction. There is an initial rectangle that is expanded along its four sides until no more black points lie on it. The top and bottom are correct, while in a 1D barcode the white bars prevent the algorithm from searching any further.

WhiteRectangleDetector works better for 2D codes (no vertical white bars the entire height of the code), given that you know where to put the initial search center of course.

I'm looking into this myself, and perhaps you can modify the following parameter in a different constructor to overcome your problem: the initSize parameter in following constructor in the WhiteRectangleDetector.java enlarges the initial search area and could lead to detecting the entire 1D barcode.

public WhiteRectangleDetector(BitMatrix image, int initSize, int x, int y) throws NotFoundException {...}

What I want to ask you: does WhiteRectangleDetector work for 1D and/or for QR codes? Have you tried MonochromeRectangleDetector yet?

For QR codes: After studying the source code of ZXING I've found a simple solution for the problem. The resultsPoints of each qr code contain the following points in the following order:

index 0: bottomLeft index 1: topLeft index 2: topRight

So you can for example create your own class with the resultPoints as a constructor parameter to create objects that hold the qr code coordinates within an image. The following code is written in C# as we use Xamarin but the java code would look similar:

public class QRCodeCoordinates
{
    public float X1 { get; set; }
    public float X2 { get; set; }
    public float Y1 { get; set; }
    public float Y2 { get; set; }

    public QRCodeCoordinates(ZXing.ResultPoint[] resultPoints)
    {
        this.X1 = resultPoints[0].X; // index 0: bottom left
        this.X2 = resultPoints[2].X; // index 2: top right
        this.Y1 = resultPoints[2].Y; // index 2: top right
        this.Y2 = resultPoints[0].Y; // index 0: bottom left            
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!