How to use Zxing in portrait mode?

前端 未结 9 965
离开以前
离开以前 2020-12-01 07:48

Currently zxing library supports only on landscape mode.For my app i need to use in portrait mode

9条回答
  •  情话喂你
    2020-12-01 08:36

    1. In order to make screen work in portrait, set portrait orientation for the activity (e.g. in manifest) and then config the camera: Use camera.setDisplayOrientation(90) in CameraConfigurationManager.setDesiredCameraParameters(Camera camera). But be aware that:

      • setDisplayOrientation(int) requires Android 2.2
      • setDisplayOrientation(int) does not affect the order of byte array passed in PreviewCallback.onPreviewFrame. (Refer to JavaDoc for additional info)
    2. Because preview frames are always in "landscape", we need to rotate them. I used clockwise rotation offered by comment #11. Do not forget to swap width and height parameters after rotation. DecodeHandler.java, rotate data before buildLuminanceSource in decode(byte[] data, int width, int height)

      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;
      
    3. I also modified CameraManager.java, getFramingRectInPreview(), as recommended by #c11:

      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. I did not modify getCameraResolution(). That's the second difference to #c11.

    As a result, I've got UPC and other 1D codes scanning work in portrait.

    P.S. Also you may adjust the size of FramingRect (that's the rectangle visible on the screen during scanning), and FramingRectInPreview will be adjusted automatically.

提交回复
热议问题