在使用 ZXing 库中的 IntentIntegrator 时,我可以通过 Intent 向条码扫描器添加一个闪光按钮吗?

While using the IntentIntegrator from the ZXing library, can I add a flash button to the barcode scanner via the Intent?

我正在使用 ZXing Library and its port of the Android Application 通过 Intent 从 Android 应用扫描条形码和二维码。我在 Gradle 依赖项中添加了以下两行以使用 android-集成代码而不进行修改:

compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
compile 'com.google.zxing:core:3.2.1'

我在 Activity 中使用 IntentIntegrator 扫描 onCreate() 中的条形码,如下所示:

integrator = new IntentIntegrator(this);
integrator.setOrientationLocked(false);
integrator.setPrompt(getString(R.string.scanner_text)); // Set the text of the scanner
integrator.setCameraId(0);  // Use a specific camera of the device
integrator.setBeepEnabled(true); // Enable beep in the scanner
integrator.setBarcodeImageEnabled(false); // Do not fetch image from the camera
integrator.initiateScan();

一切正常,我得到了正确的扫描结果,但我想要在扫描仪的右下角有一个闪光按钮,如下所示:

我已经可以使用音量增大和减小键控制闪光灯,因为我覆盖了 CaptureActivity。 条码扫描器中是否已经有一个像上面那样的闪光灯按钮,可以在自动、打开和关闭模式之间切换?如果有,可以用IntentIntegratoraddExtra()方法激活吗?还是实现这个的唯一方法是根据我的需要修改整个代码?

CompoundBarcodeView 中有一个 setTorchOn 方法,因此您可以查看该方法并尝试根据您的需要实现它。希望对你有帮助。

我忽略了 this page on Embedding BarcodeView and these sample activities which show how to customise the Barcode Scanner according to your needs. The example activity that helped me was CustomScannerActivity

IntentIntegrator class 中没有用于本地实现 Flash 按钮的选项。相反,我应该为条形码扫描器制作自定义布局,在自定义 activity 中使用它,并从 IntentIntegrator.

中调用此 activity

我有两个活动。一个是 ScannerActivity,另一个是 CallingActivity。一个让我困惑了一段时间的错误是我在 ScannerActivityonCreate() 方法中创建了一个 IntentIntegrator 的实例。它应该在 CallingActivity 中。

在给定的示例中使用了 Button 并且 Button 的文本根据 flash 进行了更改。我创建了一个名为 activity_custom_scanner 的新 Android 布局,其中我用 ToggleButton 替换了 Button,并使用按钮的图像来获取我想要的 Flash On/Off Button。

所以我的 ScannerActivity 看起来像这样:

public class CustomScannerActivity extends Activity implements
        CompoundBarcodeView.TorchListener {

    private static final int BarCodeScannerViewControllerUserCanceledErrorCode = 99991;

    private static final String TAG = CustomScannerActivity.class.getSimpleName();

    private CaptureManager capture;
    private CompoundBarcodeView barcodeScannerView;
    private ToggleButton switchFlashlightButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_scanner);

        barcodeScannerView = (CompoundBarcodeView)findViewById(R.id.zxing_barcode_scanner);
        barcodeScannerView.setTorchListener(this);

        switchFlashlightButton = (ToggleButton)findViewById(R.id.switch_flashlight);

        switchFlashlightButton.setText(null);
        switchFlashlightButton.setTextOn(null);
        switchFlashlightButton.setTextOff(null);

        // if the device does not have flashlight in its camera,
        // then remove the switch flashlight button...
        if (!hasFlash()) {
            switchFlashlightButton.setVisibility(View.GONE);
        }

        switchFlashlightButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // Save the state here
                if (isChecked) {
                    barcodeScannerView.setTorchOn();
                } else {
                    barcodeScannerView.setTorchOff();
                }
            }
        });

        capture = new CaptureManager(this, barcodeScannerView);
        capture.initializeFromIntent(getIntent(), savedInstanceState);
        capture.decode();
    }

    @Override
    protected void onResume() {
        super.onResume();
        capture.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        capture.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        capture.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        capture.onSaveInstanceState(outState);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }

    /**
     * Check if the device's camera has a Flashlight.
     * @return true if there is Flashlight, otherwise false.
     */
    private boolean hasFlash() {
        return getApplicationContext().getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    }

    @Override
    public void onTorchOn() {
        // necessary override..
    }


    @Override
    public void onTorchOff() {
        // necessary override..
    }

}

CallingActivity 看起来像这样:

public class CallingActivity extends Activity {

    private static final String TAG = CallingActivity.class.getSimpleName();

    private static final int BarCodeScannerViewControllerUserCanceledErrorCode = 99991;

    String uuid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        uuid = getIntent().getStringExtra("uuid");
        new IntentIntegrator(this).setOrientationLocked(false).setCaptureActivity(CustomScannerActivity.class).initiateScan();
    }



    public void onActivityResult(int requestCode, int resultCode, Intent intent) {

        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

        if (resultCode == RESULT_OK) {
            if (scanResult != null) {

                // handle scan result
                Log.i(TAG, "Text from Barcode Scanner: " + scanResult.getContents());
                getIntent().putExtra("data", scanResult.getContents());
                getIntent().putExtra("uuid", uuid);
            }
        }
        else if (resultCode == RESULT_CANCELED) {
            getIntent().putExtra("error", "User canceled");
            getIntent().putExtra("error_code", BarCodeScannerViewControllerUserCanceledErrorCode);
        }
        else
        {
            getIntent().putExtra("error", getString(R.string.scanner_error));
            getIntent().putExtra("error_code", BarCodeScannerViewControllerUserCanceledErrorCode);
        }

        setResult(resultCode, this.getIntent());

        this.finish();
    }

}

我不确定这是否是完美的方式,但我就是这样做的。

希望对大家有所帮助!

这个问题可能太老了,但是您可以在扫描时使用音量按钮 on/off 转动手电筒。