android - 在 zxing fragment 库中打开/关闭手电筒

标签 android android-fragments zxing barcode-scanner

我在我的应用程序中实现了 Zxing 条码扫描库。我使用了以下库: https://code.google.com/p/barcodefraglibv2/ 我想在扫描时点击按钮打开/关闭闪光灯,但我无法做到这一点。图书馆为此公开了一个功能,但它不工作。

使用的代码是: fragment = (BarcodeFragment)getSupportFragmentManager().findFragmentById(R.id.sample); fragment.getCameraManager().setTorch(true);

给我任何可以打开/关闭手电筒的引用代码。

最佳答案

你应该通过 zxing-android-embedded 库中的示例应用程序,在那里你可以找到 CustomScannerActivity 类,它展示了如何打开和关闭手电筒 以下是链接:

https://github.com/journeyapps/zxing-android-embedded/blob/master/sample/src/main/java/example/zxing/CustomScannerActivity.java

上面链接的代码示例:

public class CustomScannerActivity extends Activity implements
    DecoratedBarcodeView.TorchListener {

private CaptureManager capture;
private DecoratedBarcodeView barcodeScannerView;
private Button switchFlashlightButton;

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

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

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

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

    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);
}

public void switchFlashlight(View view) {
    if (getString(R.string.turn_on_flashlight).equals(switchFlashlightButton.getText())) {
        barcodeScannerView.setTorchOn();
    } else {
        barcodeScannerView.setTorchOff();
    }
}

@Override
public void onTorchOn() {
    switchFlashlightButton.setText(R.string.turn_off_flashlight);
}

@Override
public void onTorchOff() {
    switchFlashlightButton.setText(R.string.turn_on_flashlight);
} 
}

关于android - 在 zxing fragment 库中打开/关闭手电筒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32731869/

相关文章:

android - @BindingAdapter 不适用于 Android Kotlin 库

Android - 获取当前时间而不依赖于设备的时钟

android - 在fragment中添加ProgressBar Webview

java - 使用java中的zxing库读取数据矩阵

android - ZXing 2.0 二维码扫描后启动 fragment

android - 对于以前没有编程经验的人来说,学习 Android 的最佳方法是什么?

android - 获取 UI 线程的延迟

java - 如何在 viewpager 中同时显示 2 个 fragment ?

android - 如何在不调用 api 的情况下显示数据?

ios - ZXingObjC 无法解码从 UIImagePickerController 获取的图像