简体   繁体   English

无法在Android中将Zxing转换为人像模式

[英]Not able to convert Zxing into portrait mode in android

I have followed the following answer to do this. 我已按照以下答案进行操作。 https://stackoverflow.com/a/16252917/2747591 https://stackoverflow.com/a/16252917/2747591

But I am not getting what i want to do. 但是我没有得到我想要做的。

The image captured by camera is rotated by 90 degree while i am trying to scan. 当我尝试扫描时,相机捕获的图像旋转了90度。 Like if you are clicking a photo of a person using the camera, then in my phone screen it is showing the preview rotated by 90 degree. 就像您用相机单击某人的照片一样,在我的手机屏幕上,它显示的预览旋转了90度。 But that is not what i want as it is making bar code scanning difficult to use. 但这不是我想要的,因为它使条形码扫描变得难以使用。 I want preview as it should be. 我想要预览。 Any ideas? 有任何想法吗?

Here are my changes in the code 这是我对代码的更改

Step 1 第1步

In DecodeHandler.java I have added the following code just before buildLuminanceSource 在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);

Step 2 第2步

Modified getFramingRectInPreview() in CameraManager.java 在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;

Step 3 : 第三步

Disable the check for Landscape Mode in initFromCameraParameters(...) in CameraConfigurationManager.java 在CameraConfigurationManager.java的initFromCameraParameters(...)中禁用横向模式检查

The instructions is to Remove 说明是删除

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

But I didn't find this code in my Cameraconfiguration file. 但是我在Cameraconfiguration文件中找不到此代码。 so it should not matter anyways 所以反正没关系

Step 4 第四步

Added following line to rotate camera in setDesiredCameraParameters(...) in CameraConfigurationManager.java just after defining parametres 在定义参数后,在CameraConfigurationManager.java的setDesiredCameraParameters(...)中添加了以下行以旋转摄像机

camera.setDisplayOrientation(90);

Step 5 第5步

Changed the CaptureActivity orientation from landscape to portrait in my app's manifest file like this 像这样将我的应用清单文件中的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>

I have used zxing zxing 2.3 and below solution worked for me. 我使用过zxing zxing 2.3及以下解决方案为我工作。

1 In CameraConfigurationManager class, setDesiredCameraParameters Method add below code below pointed line 1在CameraConfigurationManager类中,使用setDesiredCameraParameters方法在尖线下方添加以下代码

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

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

2 In CameraManager class, getFramingRect Method replace code as below 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 In CameraManager class, getFramingRectInPreview Method replace code as below 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 In DecodeHandler class, decode Method add below code below pointed line 4在DecodeHandler类中,解码方法在尖线下方添加以下代码

-> Result rawResult = null; ->结果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;

  }

Please find my working code 请找到我的工作代码

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

Hope this will help you.... 希望这个能对您有所帮助....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM