在Android Studio中使用ZXING更改QR扫描仪方向

12

我希望你能帮助我。我正在使用Zxing嵌入式库来使用QR扫描仪,但问题是它处于横向模式,我想将其更改为纵向。

我的 Graddle 依赖项中有这个内容。

compile 'com.journeyapps:zxing-android-embedded:2.0.1@aar'
compile 'com.journeyapps:zxing-android-integration:2.0.1@aar'
compile 'com.google.zxing:core:3.0.1'

我在Java类中有以下代码来使用按钮激活扫描器...

and I have this in my java class to activate the scanner with a button...

public void scanQR(View view){
    IntentIntegrator integrator = new IntentIntegrator(this);
    integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
    integrator.setResultDisplayDuration(0);//Text..
    integrator.setPrompt(" Scan a QR Code");
    integrator.setScanningRectangle(450, 450);//size
    integrator.setCameraId(0);  // Use a specific camera of the device
    integrator.initiateScan();

}

谢谢您的帮助!

4个回答

55

不需要扩展类,只需将此添加到清单中:

  <activity
            android:name="com.journeyapps.barcodescanner.CaptureActivity"
            android:screenOrientation="portrait"
            tools:replace="android:screenOrientation"
            android:stateNotNeeded="true"/>

运作得很好


谢谢!希望对你有所帮助! - Ravi Tripathi
5
你应该获得这个奖项。 - Googlian
如果我在一个片段上有它会怎样? - Eduardo Oliveros
如何在同一库中启用 Flash? - Mohan

24

我正在使用

compile 'com.journeyapps:zxing-android-embedded:3.1.0@aar'

这是不同的版本,所以我不知道是否适用于你,但对我来说它是有效的。

关于我的设置,我只编译了

'com.journeyapps:zxing-android-embedded:3.1.0@aar'

'com.google.zxing:core:3.0.1'

而我没有编译

'com.journeyapps:zxing-android-integration:2.0.1@aar'

首先,我创建了一个从CaptureActivity扩展的活动

或者点击此链接查看类https://gist.github.com/TheGratefulDev/21a557c9a96333ec037c

public class CaptureActivityPortrait extends CaptureActivity {
//Nothing in side.
}

其次,将以下代码

integrator.setCaptureActivity(CaptureActivityPortait.class);

添加到您的集成器代码中。

这是我的示例:

CustomIntegrator integrator = new CustomIntegrator(activity);
            integrator.setDesiredBarcodeFormats(CustomIntegrator.PDF_417);
            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();

最后,在AndroidManifest中添加:
   <activity
        android:name=".custom.CaptureActivityPortrait"
        android:screenOrientation="portrait" <---this is the most important line
        android:stateNotNeeded="true"
        android:theme="@style/zxing_CaptureTheme"
        android:windowSoftInputMode="stateAlwaysHidden">
    </activity>

你是如何扩展CaptureActivity的?我得到了一个错误,说无法继承最终的CaptureActivity。 - Shadow
@Shadow,我刚刚检查了我的答案,我忘记了zxing:core3.2.0,请检查build.glade文件,确保您已经添加了这两个库{'com.journeyapps:zxing-android-embedded:3.1.0@aar' 和 'com.google.zxing:core:3.2.0'}。 - NOT_A_PROGRAMMER
@Shadow 这是一个扩展自 CaptureActivity 的类。https://gist.github.com/KaLingCode/21a557c9a96333ec037c - NOT_A_PROGRAMMER
我在Android项目中使用了android:screenOrientation="fullSensor",这意味着我的Activity会切换到横屏模式(这对二维码扫描并不理想)。将其设置为纵向模式可以解决问题。 - Nick Wright
我已经测试并且在com.journeyapps:zxing-android-embedded:3.6.0中工作正常。只需按照步骤进行即可。 - Aldan
显示剩余2条评论

1
我刚刚找到了最简单的方法。我们应该创建另一个CaptureActivity.java类,并在onclick监听器中编写以下代码:
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setPrompt("Scan a barcode");
integrator.setDesiredBarcodeFormats(integrator.ALL_CODE_TYPES);
integrator.setCameraId(0);  
integrator.setOrientationLocked(false);

// Replace with your own java class location here
integrator.setCaptureActivity(com.share.ants.hotelmenu.CaptureActivity.class);
integrator.setBeepEnabled(true);

0

这对我有效:

IntentIntegrator integrator = new IntentIntegrator(YourActivity.this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt(getResources().getString(R.string.scan_a_barcode));
integrator.setCameraId(0);
// Use a specific camera of the device
integrator.setBeepEnabled(true);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接