Android如何通过zxing库扫描条形码时打开闪光灯?

时间:2015-11-25 08:53:27

标签: android

我无法在我的应用程序中找到解决方案,当我使用zxing library intent integrator类扫描条形码时如何打开闪光灯。扫描条形码如下。

    public void scanNow() {
    IntentIntegrator integrator = new IntentIntegrator(getActivity());
    integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);//ONE_D_COD E_TYPES
    integrator.setPrompt("Scan a barcode");
    Camera cam = Camera.open();
    Camera.Parameters p = cam.getParameters();
    p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    cam.setParameters(p);
    cam.startPreview();
    integrator.setScanningRectangle(10, 10);
    integrator.setResultDisplayDuration(0);
    integrator.setWide(); // Wide scanning rectangle, may work better for 1D barcodes
    integrator.setCameraId(0);
    ;        // Use a specific camera of the device
    integrator.initiateScan();
    }

请给我解决方案,以便我可以实现这一点。 谢谢

3 个答案:

答案 0 :(得分:0)

您可以在此处找到名为 setTorch(Camera.Parameters参数,布尔值)的方法CameraConfigurationUtils.java

我还没有测试过它。希望有所帮助

答案 1 :(得分:0)

如果您实施Zxing扫描仪(https://github.com/zxing/zxing),那么在MainActivity.cs文件中有以下激活闪光灯的方法:

按钮flashButton;             查看zxingOverlay;

        buttonScanCustomView = this.FindViewById<Button>(Resource.Id.buttonScanCustomView);
        buttonScanCustomView.Click += async delegate {

            //Tell our scanner we want to use a custom overlay instead of the default
            scanner.UseCustomOverlay = true;

            //Inflate our custom overlay from a resource layout
            zxingOverlay = LayoutInflater.FromContext(this).Inflate(Resource.Layout.ZxingOverlay, null);

            //Find the button from our resource layout and wire up the click event
            flashButton = zxingOverlay.FindViewById<Button>(Resource.Id.buttonZxingFlash);
            flashButton.Click += (sender, e) => scanner.ToggleTorch();

            //Set our custom overlay
            scanner.CustomOverlay = zxingOverlay;

            //Start scanning!
            var result = await scanner.Scan();

            HandleScanResult(result);
        };

答案 2 :(得分:0)

我建议你使用CompoundBarcodeView实例。 然后只需调用setTorchOn()和setTorchOff()来打开或关闭flashLight。