Android open-source QR code recognition project zxing landscape screen changed to vertical screen recognition solution, androidzxing
I found a lot of methods on the Internet, but there was a problem in the end. After debugging for several hours, I finally solved the problem of portrait screen recognition perfectly.
First, you need to have the simplified code of the zxing project.
Using the simplified version saves you a lot of unnecessary code, facilitates study, and better locates core functions.
After successful debugging, you can change it to portrait screen recognition.
Step 2:
In AndroidManifest, modify the screenOrientation attribute of CaptureActivity as follows:
Android: screenOrientation = "portrait"
Step 2:
We want to adjust the camera preview to vertical.
Add the following code to the setDesiredCameraParameters () method in the CameraConfigurationManager class:
// Rotate the camera 90 degrees
SetDisplayOrientation (camera, 90 );
Then add the setDisplayOrientation () method at the end of the CameraConfigurationManager class:
/* Method for changing the Camera imaging orientation */protected void setDisplayOrientation (camera Camera, int angle) {Method downPolymorphic = null;
Try {
DownPolymorphic = camera. getClass (). getMethod ("setDisplayOrientation", new Class [] {int. class });
If (downPolymorphic! = Null)
DownPolymorphic. invoke (camera, new Object [] {angle });
} Catch (NoSuchMethodException e ){
E. printStackTrace ();
} Catch (IllegalArgumentException e ){
E. printStackTrace ();
} Catch (IllegalAccessException e ){
E. printStackTrace ();
} Catch (InvocationTargetException e ){
E. printStackTrace ();}}
Finally, Log of the initFromCameraParameters () method in CameraConfigurationManager. d (TAG, "Screen resolution:" + screenResolution); Add the following code to solve the problem of image stretching after the camera is portrait:
// Add a portrait Screen
Point screenResolutionForCamera = new Point ();
ScreenResolutionForCamera. x = screenResolution. x;
ScreenResolutionForCamera. y = screenResolution. y;
If (screenResolution. x <screenResolution. y ){
ScreenResolutionForCamera. x = screenResolution. y;
ScreenResolutionForCamera. y = screenResolution. x;
} // The second parameter in the following sentence should be modified according to the vertical screen
CameraResolution = getCameraResolution (parameters, screenResolutionForCamera );
Step 2:
In the CameranManager class, the getFramingRectInPreview () method will be:
// The following figure shows the Landscape mode.
Rect. left = rect. left * cameraResolution. x/screenResolution. x;
Rect. right = rect. right * cameraResolution. x/screenResolution. x;
Rect. top = rect. top * cameraResolution. y/screenResolution. y;
Rect. bottom = rect. bottom * cameraResolution. y/screenResolution. y;
Replace:
/// The following figure shows the portrait mode.
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;
Step 2:
The getRow () method in the PlanarYUVLuminanceSource class is used to identify the bar code,
The getMatrix () method is used to identify the QR code.
The renderCroppedGreyscaleBitmap () method is used to generate the obtained code graph.
Run the following command in getRow:
Int offset = (y + top) * dataWidth + left;
In getMatrix:
Int inputOffset = top * dataWidth + left;
InputOffset + = dataWidth;
In renderCroppedGreyscaleBitmap:
Int inputOffset = top * dataWidth + left;
InputOffset + = dataWidth;
Replace all dataWidth with dataHeight in these statements.
In the construction method of PlanarYUVLuminanceSource:
If (left + width> dataWidth | top + height> dataHeight) {throw new IllegalArgumentException ("Crop rectangle does not fit within image data .");}
The interchange between dataWidth and dateHeight is enough.
At this time, there should be no problem with your program portrait identification code diagram. As for the style of the framing box, you can change it to your favorite style in the Custom ViewfinderView.