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;
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;