在zxing片段lib中打开/关闭手电筒

时间:2015-09-23 05:46:52

标签: android android-fragments zxing barcode-scanner

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

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

为我提供任何可以打开/关闭手电筒的参考码。

6 个答案:

答案 0 :(得分:2)

您需要将以下权限和功能添加到您的清单中才能控制闪存。

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

完整来源 - &gt; Visit Here

答案 1 :(得分:2)

您应该浏览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);
} 
}

答案 2 :(得分:2)

您可以使用:

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" />

答案 3 :(得分:1)

从zxing源代码打开CameraManager活动,并在startPreview()中粘贴以下代码。在camera.startPreview()

之前
script.bat

答案 4 :(得分:0)

https://github.com/zxing/zxing/blob/0cf3b9be71680f50c90a71ca26ce0d33664b0dd6/android/src/com/google/zxing/client/android/CaptureActivity.java中,您可以看到:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
      case KeyEvent.KEYCODE_VOLUME_DOWN:
        cameraManager.setTorch(false);
        return true;
      case KeyEvent.KEYCODE_VOLUME_UP:
        cameraManager.setTorch(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
  }

答案 5 :(得分:0)

如果使用手电筒然后要打开相机,则手电筒将被禁用并出现错误。因为现在您必须使用相机的手电筒功能。因此,您可以关闭手电筒并打开相机的手电筒。但只有您只想使用相机的手电筒,这很简单。

ZXingScannerView mScannerView;
qrcode_btn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
      mScannerView = new ZXingScannerView(ctx);
      contentFrame.addView(mScannerView);
      mScannerView.setResultHandler(ActivityMain.this);
      mScannerView.startCamera();
      mScannerView.setFlash(true);
   }
}

如果在ZXing之前打开手电筒,则必须在ZXing之前关闭手电筒。

flashLightOff();
public void flashLightOff() {
   CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
   try {
      boolean flashAvailable = cameraManager.getCameraCharacteristics("0").get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
   } catch (CameraAccessException e) {
      e.printStackTrace();
   }

   try {
      String cameraId = cameraManager.getCameraIdList()[0];
      cameraManager.setTorchMode(cameraId, false);
      flashLightStatus = false;
      flashlight.setColorFilter(ContextCompat.getColor(context, R.color.grey_dark));
   } catch (CameraAccessException e) {
   }
}