在 zxing 片段库中打开/关闭手电筒 [英] Turn ON/OFF flashlight in zxing fragment lib

查看:71
本文介绍了在 zxing 片段库中打开/关闭手电筒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have implemented Zxing barcode scanning library in my application. I have used following library: https://code.google.com/p/barcodefraglibv2/ I want to turn ON/OFF the flash light on click of a button when scanning but I am not able to do this. Library has exposed one function for the same but it is not working.

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

Code used is: fragment = (BarcodeFragment)getSupportFragmentManager().findFragmentById(R.id.sample); fragment.getCameraManager().setTorch(true);

请提供我可以打开/关闭手电筒的任何参考代码.

Provide me any refrence code by which I can turn ON/OFF flashlight.

推荐答案

你应该通过 zxing-android-embedded 库中的示例应用程序,在那里你可以找到 CustomScannerActivity 显示如何打开和关闭闪光灯的课程以下是链接:

You should go through the sample app in zxing-android-embedded library, there you can find CustomScannerActivity class which shows how to switch ON and OFF flash light below is the link:

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

来自上面链接的代码示例:

Code sample from above link:

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

这篇关于在 zxing 片段库中打开/关闭手电筒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆