我在我的应用程序中实现了 Zxing 条码扫描库。我使用了以下库: https://code.google.com/p/barcodefraglibv2/ 我想在扫描时点击按钮打开/关闭闪光灯,但我无法做到这一点。图书馆为此公开了一个功能,但它不工作。
使用的代码是: fragment = (BarcodeFragment)getSupportFragmentManager().findFragmentById(R.id.sample); fragment.getCameraManager().setTorch(true);
给我任何可以打开/关闭手电筒的引用代码。
最佳答案
你应该通过 zxing-android-embedded
库中的示例应用程序,在那里你可以找到 CustomScannerActivity
类,它展示了如何打开和关闭手电筒
以下是链接:
上面链接的代码示例:
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/