[Turn] Modify ZXing QR code scanning to portrait mode
Modify ZXing For Android to portrait mode. After practice, the feasible methods are as follows:
1. Modify the AndroidManifest file and set CaptureActivity to portrait: The
code is: android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
2. The camera preview scene is adjusted to the vertical
orientation , as follows: 1) Add the following code to the setDesiredCameraParameters(Camera camera) method in the CameraConfigurationManager class:
//Rotate the camera 90 degrees
setDisplayOrientation(camera, 90);
2) Then add it to the CameraConfigurationManager class The setDisplayOrientation(Camera camera, int angle) method, the specific code of the method is as follows:
3) Finally, in order to prevent the problem of image stretching after the camera is turned upright, you need to replace the initFromCameraParameters(Camera camera) method in the CameraConfigurationManager class with the following code
3. Modify the code in the getFramingRectInPreview() method in the CameraManager class as follows:
change: //Horizontal screen mode
Replace with:
4. In the decode(byte[] data, int width, int height) method in the DecodeHandler class, add the following code before the call to buildLuminanceSource:
1. Modify the AndroidManifest file and set CaptureActivity to portrait: The
code is: android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
2. The camera preview scene is adjusted to the vertical
orientation , as follows: 1) Add the following code to the setDesiredCameraParameters(Camera camera) method in the CameraConfigurationManager class:
//Rotate the camera 90 degrees
setDisplayOrientation(camera, 90);
2) Then add it to the CameraConfigurationManager class The setDisplayOrientation(Camera camera, int angle) method, the specific code of the method is as follows:
protected void setDisplayOrientation(Camera camera, int angle) {
Method downPolymorphic;
try {
downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });
if (downPolymorphic != null)
downPolymorphic.invoke(camera, new Object[] { angle });
} catch (Exception e1) {
e1.printStackTrace();
}
}
3) Finally, in order to prevent the problem of image stretching after the camera is turned upright, you need to replace the initFromCameraParameters(Camera camera) method in the CameraConfigurationManager class with the following code
void initFromCameraParameters(Camera camera)
{
Camera.Parameters parameters = camera.getParameters();
previewFormat = parameters.getPreviewFormat();
previewFormatString = parameters.get("preview-format");
Log.d(TAG, "Default preview format: " + previewFormat + '/'
+ previewFormatString); WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
screenResolution = new Point(display.getWidth(), display.getHeight());
Log.d(TAG, "Screen resolution: " + screenResolution);
Point screenResolutionForCamera = new Point();
screenResolutionForCamera.x = screenResolution.x;
screenResolutionForCamera.y = screenResolution.y;
// preview size is always something like 480*320, other 320*480
if (screenResolution.x < screenResolution.y)
{
screenResolutionForCamera.x = screenResolution.y;
screenResolutionForCamera.y = screenResolution.x; }
cameraResolution =findBestPreviewSizeValue(parameters, screenResolutionForCamera);
// cameraResolution = findBestPreviewSizeValue(parameters, screenResolution);
Log.d(TAG, "Camera resolution: " + screenResolution);
}
}
change: //Horizontal screen 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 with:
//竖屏模式
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;
4. In the decode(byte[] data, int width, int height) method in the DecodeHandler class, add the following code before the call to buildLuminanceSource:
//竖屏
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;
At this point, run the program and the portrait mode is completed.
Original address: http://blog.sina.com.cn/s/blog_705dc23f0101a78x.html ;
and http://blog.csdn.net/aaawqqq/article/details/24804939
Original address: http://blog.sina.com.cn/s/blog_705dc23f0101a78x.html ;
and http://blog.csdn.net/aaawqqq/article/details/24804939