Android open source library using ZXing open source library QR code - achieve vertical screen and high recognition rate

The ZXing open source library defaults to horizontal display. After changing to vertical display, manually set the width and height of the scanning box, you will find that when scanning the QR code at close range, the scanning cannot be successful. You need to scan a little farther to scan successfully. Success, the analysis should be that after setting the width and height of the scan, there is a problem with the actual scanning area calculation. Articles 1 to 4 learn from the blog to basically solve the problem. I also thank the original blogger, but there is still a little problem, that is, when scanning When the QR code is complex, the recognition rate is too low. The solution to this problem has been given in Article 5;

step:

1) Modify the screenOrientation of the <Activity /> tag CaptureActivity in AndroidManifest.xml to

android:screenOrientation="portrait"


2) Replace the original left right top bottom with getFramingRectInPreview() in the CameraManager.java class

// vertical screen  
rect.left = rect.left * cameraResolution.y / screenResolution.x;   
rect.right = rect.right * cameraResolution.y / screenResolution.x;   
rect.top = rect.top * cameraResolution.x / screenResolution.y;   
rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;   

3) void setDesiredCameraParameters(Camera camera) method in CameraConfigurationManager.java

increase before setParameters

camera.setDisplayOrientation(90);  

  co

4) The decode(byte[] data, int width, int height) method in DecodeHandler.java is in

PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);

Added before:

byte[] rotatedData = new byte[data.length];   
 for (int y = 0; y < height; y++) {   
 for (int x = 0; x < width; x++)   
 rotatedData[x * height + height - y - 1] = data[x + y * width];   
 }   
 int tmp = width; // Here we are swapping, that's the difference to #11   
 width = height;   
 height = tmp;   
 data = rotatedData;  


5) The above steps have basically solved the problem of long-distance scanning of the horizontal screen to vertical screen. The general QR code is scanned very fast, and the recognition rate is very good, but when scanning complex QR codes, the recognition rate is found. Very low, sometimes it can't be recognized for a long time, but using the original ZXing Demo to scan, although the recognition speed is slower, but the impact is not large, the recognition rate is still very high, which makes people wonder; take the time to read the source code carefully , and debugged for n times, and finally found the problem; the resolution of the camera selected in ZXing Demo is 1920 * 1080, but the resolution of the camera I modified is only 640 * 480, so that the resolution of the real scanned image is too low, When scanning complex QR codes, the low-resolution ones are already indistinguishable from the text, so how can they be identified? Modification method:
Modify the method findBestPreviewSizeValue under the CameraConfigurationUtils class
double screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y;
This code is:
 
 
double screenAspectRatio;
if(screenResolution.x > screenResolution.y){
  screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y;
}else{
  screenAspectRatio = (double) screenResolution.y / (double) screenResolution.x;
}
In this way, whether it is a horizontal screen or a vertical screen, the most matching maximum resolution can be correctly found, and the recognition rate is greatly improved;
 
 






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325901527&siteId=291194637