Android Zxing 将方向更改为纵向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10216943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Zxing change orientation to portrait
提问by Udi Idan
I'm trying to rotate Zxing display after reading a few questions and posts about the issue. After following the instructions, the display did rotate, but the rectangle of the scanner is not positioned as it should (as can be seen on the image attached).
在阅读了一些有关该问题的问题和帖子后,我正在尝试旋转 Zxing 显示。按照说明操作后,显示屏确实旋转了,但扫描仪的矩形未按其应有的位置定位(如所附图像所示)。
This is what I have done:
这就是我所做的:
in CameraConfigurationManager:
camera.setDisplayOrientation(90);
in DecodeHandler.java
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; width = height; height = tmp;
in CameraManager.java:
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;
在 CameraConfigurationManager 中:
camera.setDisplayOrientation(90);
在 DecodeHandler.java 中
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; width = height; height = tmp;
在 CameraManager.java 中:
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;
采纳答案by Udi Idan
After a lot of struggling, I found the problem, and I hope it will help someone in the future.
经过一番苦苦挣扎,我找到了问题所在,希望对以后的人有所帮助。
On initFromCameraParameters
method in CameraConfigurationManager
there is an assumption that the scan is ALWAYS in landscape mode
, and therefor a fix when width < height
.
If You follow the steps in the question and remove this check, it works fine.
在initFromCameraParameters
方法中CameraConfigurationManager
,假设扫描是ALWAYS in landscape mode
,因此在 时修复width < height
。如果您按照问题中的步骤操作并删除此检查,则它可以正常工作。
回答by Roberto
Thank you for your answer!! it really helped me, one thing that I noticed is that at least on zxing 2.1 you need to pass "rotatedData" to buildLuminanceSource instead of just "data", the line end up like this:
谢谢您的回答!!它真的帮助了我,我注意到的一件事是,至少在 zxing 2.1 上,您需要将“rotatedData”传递给 buildLuminanceSource 而不仅仅是“data”,该行最终是这样的:
PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(rotatedData, width, height);
Hopefully this helps someone else!
希望这对其他人有帮助!
回答by Anup
As of zxing library:2.2.0 support for orientation change is inherent
从 zxing 库开始:2.2.0 对方向变化的支持是固有的
Add/edit the following in manifest:
在清单中添加/编辑以下内容:
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="fullSensor"
tools:replace="screenOrientation" />
Set additional property at call to scanner :
在调用扫描仪时设置附加属性:
IntentIntegrator integrator = new IntentIntegrator(this);
//allows portrait/landscape mode
integrator.setOrientationLocked(false);//"additional property"
integrator.initiateScan();
Reference link : https://github.com/journeyapps/zxing-android-embedded#changing-the-orientation
参考链接:https: //github.com/journeyapps/zxing-android-embedded#changed-the-orientation
回答by Yousuf Qureshi
Well I made a small change in ProjectLibrary (xzing project) and able to change orientation landscape to portrait
好吧,我在 ProjectLibrary(xzing 项目)中做了一个小改动,并且能够将方向横向更改为纵向
In setDesiredCameraParameters method of class CameraConfigurationManager
added
在setDesiredCameraParameters method of class CameraConfigurationManager
添加
camera.setDisplayOrientation(90);
camera.setDisplayOrientation(90);
.. in my original project's AndroidManifest.xml
file. I set screenOrientation = portrait
and Its working fine on my ICS 4.0.3
.. 在我原来的项目AndroidManifest.xml
文件中。我screenOrientation = portrait
在我的 ICS 4.0.3 上设置和它工作正常
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
android:configChanges="orientation|keyboardHidden"
android:exported="false"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" >
<intent-filter>
<action android:name="com.phonegap.plugins.barcodescanner.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
回答by Dayanand Waghmare
In
CameraConfigurationManager
:camera.setDisplayOrientation(90);
In
DecodeHandler.java
: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; width = height; height = tmp;
In
CameraManager.java
: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;
In
CameraConfigurationManager
:if (width > height) { Log.i(TAG, "Display reports portrait orientation; assuming this is incorrect"); int temp = width; width = height; height = temp; }
Change
android:screenOrientation="portrait"
forCaptureActivity
in manifest.
在
CameraConfigurationManager
:camera.setDisplayOrientation(90);
在
DecodeHandler.java
: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; width = height; height = tmp;
在
CameraManager.java
: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;
在
CameraConfigurationManager
:if (width > height) { Log.i(TAG, "Display reports portrait orientation; assuming this is incorrect"); int temp = width; width = height; height = temp; }
在清单中更改
android:screenOrientation="portrait"
为CaptureActivity
。
回答by Sean Owen
I'm a developer of Barcode Scanner. Yes, it takes a lot more than this to make it scan in portrait mode. You have to "rotate" the image data, and account for the orientation of the device, its default orientation, and its sensor's orientation.
我是 Barcode Scanner 的开发人员。是的,要使其以纵向模式进行扫描需要的远不止这些。您必须“旋转”图像数据,并考虑设备的方向、默认方向和传感器的方向。
Barcode Scanner+scans in portrait mode, and you can integrate with it via Intent in exactly the same way that you integrate with Barcode Scanner. (However it's a for-pay app.)
Barcode Scanner+以纵向模式扫描,您可以通过 Intent 以与与 Barcode Scanner 集成完全相同的方式与它集成。(但它是一个付费应用程序。)
回答by Loi Nguyen
I've tried various patches, suggested in other answers, but barcode recognition remained unreliable.
我尝试了其他答案中建议的各种补丁,但条形码识别仍然不可靠。
I highly recommend using repository below in portrait mode. Try it, it's fast and stable. I used it in my hybrid app.
我强烈建议在纵向模式下使用下面的存储库。试试吧,它又快又稳定。我在我的混合应用程序中使用了它。
回答by rizujikeda
try this : add android:screenOrientation="sensorPortrait"
试试这个:添加 android:screenOrientation="sensorPortrait"
<activity android:name=".CaptureActivity"
android:screenOrientation="sensorPortrait"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:theme="@style/CaptureTheme"
android:windowSoftInputMode="stateAlwaysHidden"
回答by Syed Danish Haider
In your library go to manifest file change the below line under activity tag
在您的库中,转到清单文件更改活动标签下的以下行
android:screenOrientation="portrait"