Zxing在纵向模式下工作不正确

时间:2015-01-15 09:05:21

标签: android barcode qr-code zxing

我在Android Studio中创建了一个项目,并添加了zxing库2.3。我想只在纵向模式下扫描和解码。我已阅读这些手册Zxing Camera in Portrait mode on Android,但现在我有纵向模式,无法解码任何条形码。 我还尝试通过在manifest中添加纵向模式并将此代码添加到CameraConfigurationManager.java来以另一种方式解决此问题:

parameters.set("orientation", "portrait");
  parameters.setRotation(90);
  if (camera != null)
      try {
          camera.setDisplayOrientation(90);
      } catch (NoSuchMethodError ex) {
      }

程序以纵向模式启动,但没有解码任何条形码,当我将手机旋转90度,程序解码条形码,当拍摄和旋转90度时,qr代码图像,当我旋转我的手机,程序解码条形码。

1 个答案:

答案 0 :(得分:0)

我使用了zxing zxing 2.3及以下解决方案为我工作。

1在CameraConfigurationManager类中,setDesiredCameraParameters方法在下面的代码中添加以下代码

- > Camera.Parameters 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类中,解码方法在下面的代码行中添加以下代码

- &GT;结果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

您可以删除所有条件,如果您只想使用纵向模式。

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