Zxing portrait screen switch android

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.