【问题标题】:Not able to convert Zxing into portrait mode in android无法在 android 中将 Zxing 转换为纵向模式
【发布时间】:2014-11-07 08:45:43
【问题描述】:

我已按照以下答案执行此操作。 https://stackoverflow.com/a/16252917/2747591

但我没有得到我想做的事情。

当我尝试扫描时,相机拍摄的图像旋转了 90 度。就像您使用相机单击某人的照片一样,然后在我的手机屏幕上显示旋转了 90 度的预览。但这不是我想要的,因为它使条形码扫描难以使用。我想要预览,因为它应该是。 有什么想法吗?

这是我对代码的更改

第 1 步

在 DecodeHandler.java 中,我在 buildLuminanceSource 之前添加了以下代码

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;
PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);

第 2 步

修改了CameraManager.java中的getFramingRectInPreview()

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 步

在 CameraConfigurationManager.java 的 initFromCameraParameters(...) 中禁用横向模式检查

说明是删除

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

但我没有在我的相机配置文件中找到此代码。所以无论如何都应该无所谓

第 4 步

在定义参数后,在 CameraConfigurationManager.java 的 setDesiredCameraParameters(...) 中添加以下行来旋转相机

camera.setDisplayOrientation(90);

第 5 步

像这样在我的应用清单文件中将 CaptureActivity 方向从横向更改为纵向

<activity
           android:name="com.google.zxing.client.android.CaptureActivity"
           android:screenOrientation="portrait"
           android:configChanges="orientation|keyboardHidden"
           android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
           android:windowSoftInputMode="stateAlwaysHidden">
           <intent-filter>
              <action android:name="android.intent.action.MAIN"/>
              <category android:name="android.intent.category.DEFAULT"/>
           </intent-filter>
           <intent-filter>
              <action android:name="com.google.zxing.client.android.SCAN"/>
              <category android:name="android.intent.category.DEFAULT"/>
           </intent-filter>
    </activity>

【问题讨论】:

  • 您知道让客户倒立拍照很困难,那您为什么还要让我们做同样的事情来帮助您呢?没有你的代码,站在我们的头上实际上更容易。 ;)
  • @JeremyMiller 我遵循了与我在顶部提到的上述答案完全相同的程序。这是唯一的代码更改。如果你用过 Zxing,我猜你会明白的。
  • 我有并且它有效,所以让我扭转局面:如果你在他们声称完全一样之前帮助过他们的编码,我猜你会明白的。
  • @JeremyMiller 哈哈哈。是的。让我重新编辑问题。 :P
  • @JeremyMiller 你去 :)

标签: android zxing barcode-scanner


【解决方案1】:

我使用了 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

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

【讨论】:

  • 我已经制作了所有这些通道,但扫描仪从一开始就处于横向模式。
  • @RohitGoyal 检查 1. 版本。 2. 在CaptureActivity 的menifest orientaton 中,它不应该是固定的。 3. 尝试从compyutech.comoj.com/repo/zxing-dynamic.zip 替换类文件 4. 如果不起作用,请尽可能提供代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多