无法在Android中将Zxing转换为纵向模式

时间:2014-11-07 08:45:43

标签: android zxing barcode-scanner

我已按照以下答案执行此操作。 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;
}

但我在Cameraconfiguration文件中找不到此代码。所以反正无所谓

第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>

1 个答案:

答案 0 :(得分:0)

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

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

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

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