Zxing portrait screen switch android
The ZxingDemo downloaded on google is in the landscape status.
In Androidmanifest. xml, I simply change Activity to a vertical screen. android: screenOrientation = "portrait"
Although the screen is positive, the shooting screen displayed on the screen is still horizontal.
I checked some online materials and manually debugged them.
Here is a summary
Steps:
1) In AndroidManifest. xml, modify the screenOrientation of the tag CaptureActivity
android:screenOrientation="portrait"
2) replace the original left right top bottom with getFramingRectInPreview () in the CameraManager. java class.
// 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) In CameraConfigurationManager. java, the void setDesiredCameraParameters (Camera camera) Method
Added before setParameters
camera.setDisplayOrientation(90);
4) In DecodeHandler. java, the decode (byte [] data, int width, int height) method in
PlanarYUVLuminanceSource source = CameraManager. get (). buildLuminanceSource (data, width, height );
Previously added:
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) Replace the initFromCameraParameters (Camera camera) method of CameraConfigurationManager with the following:
This method prevents screen stretching after the portrait Screen
/** * Reads, one time, values from the camera that are needed by the app. */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 = getCameraResolution(parameters, screenResolutionForCamera);//cameraResolution = getCameraResolution(parameters, screenResolution);Log.d(TAG, "Camera resolution: " + screenResolution);}
Now Zxing landscape screen is switched to landscape Screen
If you have any shortcomings, please leave a message below. Thank you.
Hope to be useful to you
Resources
Http://download.csdn.net/detail/aaawqqq/7278133