【问题标题】:zxing camera portrait mode and landscape on androidandroid上的zxing相机人像模式和风景
【发布时间】:2014-05-01 07:20:05
【问题描述】:

我已经在我的应用程序中集成了 zxing 库。现在我希望 zxing 相机可以在横向模式和纵向模式下工作。现在它只在一种模式下工作。有人可以帮我看看怎么做吗?

【问题讨论】:

    标签: android eclipse zxing barcode-scanner portrait


    【解决方案1】:

    我使用了 compyutech 的答案,但缺少一些东西。所以,我把所有需要的东西都放在这里了。

    1.相机配置管理器:initFromCameraParameters 方法内:

    if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        camera.setDisplayOrientation(90);
    }
    

    也从相同的方法中删除此代码:

        if (width < height) {
            Log.i(TAG, "Display reports portrait orientation; assuming this is incorrect");
            int temp = width;
            width = height;
            height = temp;
        }
    

    2。相机管理器: class variables

    private static final int MIN_FRAME_WIDTH = 340;
    private static final int MIN_FRAME_HEIGHT = 240;
    private static final int MAX_FRAME_WIDTH = 1200;
    private static final int MAX_FRAME_HEIGHT = 675;
    

    getFramingRect 方法:

    public synchronized Rect getFramingRect() {
        if (framingRect == null) {
          if (camera == null) {
            return null;
          }
          Point screenResolution = configManager.getScreenResolution();
          if (screenResolution == null) {
            // Called early, before init even finished
            return null;
          }
    
            // Code added to enable portrait mode
            int width = MIN_FRAME_WIDTH;
            int height = MIN_FRAME_HEIGHT;
            if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                int tmp = 7 * screenResolution.x / 8; 
                width = (tmp) < MIN_FRAME_WIDTH ? MIN_FRAME_WIDTH : (tmp);
    
                tmp = 1 * screenResolution.y / 3;
                height = (tmp) < MIN_FRAME_WIDTH ? MIN_FRAME_WIDTH : ((tmp) > MAX_FRAME_HEIGHT ? MAX_FRAME_HEIGHT : (tmp));
                Log.d(TAG, "Customized code for portrait mode in getFramingRect executed (Piyush Merja) ");
            }else{
                // Original Code
                width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
                height = findDesiredDimensionInRange(screenResolution.y, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
            }
            // End
    
          int leftOffset = (screenResolution.x - width) / 2;
          int topOffset = (screenResolution.y - height) / 2;
          framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);
          Log.d(TAG, "Calculated framing rect: " + framingRect);
        }
        return framingRect;
      }
    
      private static int findDesiredDimensionInRange(int resolution, int hardMin, int hardMax) {
        int dim = 5 * resolution / 8; // Target 5/8 of each dimension
        if (dim < hardMin) {
          return hardMin;
        }
        if (dim > hardMax) {
          return hardMax;
        }
        return dim;
      }
    

    getFramingRectInPreview 方法:

    public synchronized Rect getFramingRectInPreview() {
        if (framingRectInPreview == null) {
          Rect framingRect = getFramingRect();
          if (framingRect == null) {
            return null;
          }
          Rect rect = new Rect(framingRect);
          Point cameraResolution = configManager.getCameraResolution();
          Point screenResolution = configManager.getScreenResolution();
          if (cameraResolution == null || screenResolution == null) {
            // Called early, before init even finished
            return null;
          }
    
          // Code added to enable portrait mode
            if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                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;
                Log.d(TAG, "Customized code for portrait mode in getFramingRectInPreview executed (Piyush Merja) ");
            }else{
                // Original code commented
                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;
            }
            // End
            framingRectInPreview = rect;//this was missing in compyutech's answer
        }
        return framingRectInPreview;
      }
    

    3.解码处理程序:

    decode 方法内:

        long start = System.currentTimeMillis();
        Result rawResult = null;
    
        // Code added to enable portrait mode
        if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            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];
            }
            data = rotatedData;
            int tmp = width;
            width = height;
            height = tmp;
            Log.d(TAG, "Customized code for portrait mode in decode executed (Piyush Merja) ");
    
        }//end
    
        PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);
    

    4. AndroidManifest.xml:

    删除CaptureActivity 的方向设置。

    【讨论】:

      【解决方案2】:

      谷歌这个问题。

      我使用了 zxing zxing 2.3 及以下的解决方案。

      1 在CameraConfigurationManager 类中,setDesiredCameraParameters 方法在尖线下方添加以下代码

      -> Camera.Parameters 参数 = camera.getParameters();

       if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
              camera.setDisplayOrientation(90);
       }
      

      2在CameraManager类中,getFramingRect方法替换代码如下

      int width = MIN_FRAME_WIDTH; int height = MIN_FRAME_HEIGHT;
      if (context.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) {
         int tmp = 7 * screenResolution.x / 8; 
         width = (tmp) < MIN_FRAME_WIDTH ? MIN_FRAME_WIDTH : (tmp);                   
         tmp = 1 * screenResolution.y / 3;
         height = (tmp) < MIN_FRAME_WIDTH ? MIN_FRAME_WIDTH : ((tmp) > MAX_FRAME_HEIGHT ?  MAX_FRAME_HEIGHT : (tmp));
      }else{
         // Original Code
         width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, > MAX_FRAME_WIDTH);
         height = findDesiredDimensionInRange(screenResolution.y, MIN_FRAME_HEIGHT,  MAX_FRAME_HEIGHT); 
      }
      

      3 在CameraManager类中,getFramingRectInPreview方法替换代码如下

      if (context.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) {
         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;
      }else{
         // Original code commented
         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;
      }
      

      4 在 DecodeHandler 类中, decode 方法在指定线下方添加以下代码

      -> 结果 rawResult = null;

       if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
              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];
              }
              data = rotatedData;
              int tmp = width;
              width = height;
              height = tmp;
      
        }
      

      请找到我的工作代码

      http://www.compyutech.co.in/repo/zxing-dynamic.zip

      希望这对你有帮助....

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-16
        • 1970-01-01
        • 1970-01-01
        • 2013-01-08
        • 2016-09-05
        • 2018-04-29
        相关资源
        最近更新 更多