我已经在我的应用中实现了Zxing条形码扫描库。我已经使用了下面的库:https://code.google.com/p/barcodefraglibv2/我想在扫描时点击一个按钮打开/关闭闪光灯,但我不能这样做。库已经公开了一个相同的函数,但它不工作。
使用的代码是: fragment = (BarcodeFragment)getSupportFragmentManager().findFragmentById(R.id.sample);fragment.getCameraManager().setTorch(true);
提供任何参考代码,我可以通过它打开/关闭手电筒。
发布于 2017-05-10 16:06:26
你应该通过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);
}
}
发布于 2018-02-11 19:07:34
您可以使用以下命令:
public void flashON(View v) {
mScannerView.setFlash(true);
}
public void flashOFF(View v){
mScannerView.setFlash(false);
}
其中flashON和flashOFF是2 imageButtons。这是xml:
<me.dm7.barcodescanner.zxing.ZXingScannerView
android:id="@+id/zxscan"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</me.dm7.barcodescanner.zxing.ZXingScannerView>
<ImageButton
android:id="@+id/FlashON"
android:onClick="flashON"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/FlashOFF"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/FlashOFF"
android:layout_marginEnd="60dp"
android:layout_marginRight="60dp"
android:src="@drawable/flashon" />
<ImageButton
android:id="@+id/FlashOFF"
android:onClick="flashOFF"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/NumeEchip"
android:layout_marginEnd="13dp"
android:layout_marginRight="13dp"
android:src="@drawable/flashoff" />
发布于 2015-09-23 13:52:20
为了控制闪存,您需要将以下权限和功能添加到您的清单中。
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
这是一个示例代码-
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ToggleButton;
/**
* @author Prabu
* July 30 2013
* @version 1.0
*
*/
public class FlashlightActivity extends Activity {
private Camera camera;
private ToggleButton button;
private final Context context = this;
/**
* @see android.app.Activity#onStop()
*/
@Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
}
}
/**
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.flashlight);
button = (ToggleButton) findViewById(R.id.togglebutton);
final PackageManager pm = context.getPackageManager();
if(!isCameraSupported(pm)){
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("No Camera");
alertDialog.setMessage("The device's doesn't support camera.");
alertDialog.setButton(RESULT_OK, "OK", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int which) {
Log.e("err", "The device's doesn't support camera.");
}
});
alertDialog.show();
}
camera = Camera.open();
}
public void onToggleClicked(View view) {
PackageManager pm=context.getPackageManager();
final Parameters p = camera.getParameters();
if(isFlashSupported(pm)){
boolean on = ((ToggleButton) view).isChecked();
if (on) {
Log.i("info", "torch is turn on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
} else {
Log.i("info", "torch is turn off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
}
}else{
button.setChecked(false);
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("No Camera Flash");
alertDialog.setMessage("The device's camera doesn't support flash.");
alertDialog.setButton(RESULT_OK, "OK", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int which) {
Log.e("err", "The device's camera doesn't support flash.");
}
});
alertDialog.show();
}
}
/**
* @param packageManager
* @return true <b>if the device support camera flash</b><br/>
* false <b>if the device doesn't support camera flash</b>
*/
private boolean isFlashSupported(PackageManager packageManager){
// if device support camera flash?
if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
return true;
}
return false;
}
/**
* @param packageManager
* @return true <b>if the device support camera</b><br/>
* false <b>if the device doesn't support camera</b>
*/
private boolean isCameraSupported(PackageManager packageManager){
// if device support camera?
if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
return true;
}
return false;
}
}
对于完整的源代码-> Visit Here
https://stackoverflow.com/questions/32731869
复制相似问题