【发布时间】:2020-11-03 15:59:35
【问题描述】:
目前 zxing 库仅支持横向模式。对于我的应用,我需要在纵向模式下使用
【问题讨论】:
-
这是您可以使用 zxing 2.1 执行的操作。 stackoverflow.com/questions/16252791/…
-
从 Zing 2.2.0 开始,您可以为方向设置属性,请参阅answer
标签: android
目前 zxing 库仅支持横向模式。对于我的应用,我需要在纵向模式下使用
【问题讨论】:
标签: android
这是纵向模式扫描的解决方案
首先在您的应用级 gradle 文件中声明这两行
implementation 'com.journeyapps:zxing-android-embedded:3.0.1@aar'
implementation 'com.google.zxing:core:3.2.0'
在您的 xml 文件中定义一个按钮,并在按钮的 Onclick 侦听器中在 MainActivity java 文件中编写以下代码
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setPrompt("Scan a barcode");
integrator.setCameraId(0); // Use a specific camera of the device
integrator.setOrientationLocked(true);
integrator.setBeepEnabled(true);
integrator.setCaptureActivity(CaptureActivityPortrait.class);
integrator.initiateScan();
在 onCreate() 方法之后在 MainActivity java 文件中编写以下代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
st_scanned_result = result.getContents();
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
}
}
然后创建一个名为 CaptureActivityPortrait 的类来扩展 CaptureActivity。该类如下所示
package soAndSo(Your PackageName);
import com.journeyapps.barcodescanner.CaptureActivity;
public class CaptureActivityPortrait extends CaptureActivity {
}
最重要的是在清单文件中声明您的 CaptureActivityPortrait,如下所示
<activity android:name=".CaptureActivityPortrait"
android:screenOrientation="sensorPortrait"
android:stateNotNeeded="true"
android:theme="@style/zxing_CaptureTheme"
android:windowSoftInputMode="stateAlwaysHidden"></activity>
【讨论】:
只需查看在纵向模式下使用 Zxing 的问题。
【讨论】:
我想在纵向模式下使用条形码阅读器。我找到了solution here 正如该线程前面发布的评论中所述。我想把它作为一个答案,这样更容易为有同样问题的人找到解决方案。
要在纵向模式下使用扫描仪,您需要在AndroidManifest.xml 中添加以下活动。
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="portrait"
tools:replace="screenOrientation" />
就是这样。
查看链接了解更多详情。
【讨论】:
setDisplayOrientation(int)不影响PreviewCallback.onPreviewFrame传入的字节数组的顺序。 (有关更多信息,请参阅 JavaDoc)
表示需要旋转previewCallback返回的数据,但这是yuv数据,需要转成rgb数据再旋转。可以纵向模式扫描条码,但需要更长的时间,因为需要更多时间来处理从 yuv 到 rgb 的数据并旋转 rgb 数据。 所以这里的问题是我们如何从 previewcallback 获取纵向模式的 yuv 数据?当我们为相机参数设置 setPreviewSize 时,如果不支持 previewsize 则会出现异常。这是这个问题的问题。如果相机驱动程序不支持纵向模式的 previewSize(高度 > 宽度),则无法在纵向模式下获取 yuv 数据。其余的取决于您,您可以在纵向模式下扫描条形码,但需要更长的时间,或者您必须将屏幕方向更改为横向才能更快地获得结果。
【讨论】:
请检查如下(Official Documentation)
将此活动添加到清单文件
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="fullSensor"
tools:replace="screenOrientation" />
将积分器 OrientationLocked 设置为 false
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setOrientationLocked(false);
integrator.initiateScan();
希望对你有帮助
【讨论】:
为了使屏幕纵向工作,为活动设置纵向方向(例如在清单中),然后配置相机:在 CameraConfigurationManager.setDesiredCameraParameters(Camera camera) 中使用 camera.setDisplayOrientation(90)。但请注意:
因为预览帧总是“横向”,我们需要旋转它们。我使用了comment #11 提供的顺时针旋转。不要忘记在旋转后交换宽度和高度参数。 decodeHandler.java,在decode(byte[] data, int width, int height)中buildLuminanceSource之前旋转数据
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;
我还按照#c11 的建议修改了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;
我没有修改 getCameraResolution()。这是与#c11 的第二个不同之处。
因此,我已经完成了 UPC 和其他一维码纵向扫描工作。
附:您也可以调整FramingRect的大小(即扫描时屏幕上可见的矩形),FramingRectInPreview会自动调整。
【讨论】:
只需将这些代码添加到项目的 AndroidManifest.xml 中
<activity android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="sensorPortrait"
android:stateNotNeeded="true"
android:theme="@style/zxing_CaptureTheme"
android:windowSoftInputMode="stateAlwaysHidden"
tools:replace="android:screenOrientation" />
【讨论】:
使用这个android库https://github.com/SudarAbisheck/ZXing-Orient
它支持纵向和横向。
【讨论】:
将此 android:screenOrientation="sensorPortrait" 添加到您的清单中
<activity android:name=".CaptureActivity"
android:screenOrientation="sensorPortrait"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:theme="@style/CaptureTheme"
android:windowSoftInputMode="stateAlwaysHidden"
tools:replace="android:theme"/>
【讨论】: