我想用zxing libarary扫描qrcode时打开手电筒

时间:2016-11-12 06:56:23

标签: android

public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
    private ZXingScannerView mScannerView;
    Result rawResult;
    Camera cam;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    mScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view
    setContentView(mScannerView);
    mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
    mScannerView.startCamera();
    cam = Camera.open();
    Camera.Parameters p = cam.getParameters();
    p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    cam.setParameters(p);
    cam.startPreview();

只有打开手电筒没有打开ScannerView如何修复它,我需要ScannerView&中的闪光灯图标。如果需要,手电筒是打开的。

1 个答案:

答案 0 :(得分:0)

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
                String cameraId = null; // Usually back camera is at 0 position.
                try {
                    cameraId = camManager.getCameraIdList()[0];
                    camManager.setTorchMode(cameraId, false);  

     // TODO  Turn ON  Flash Light On
                    mScannerView.setFlash(true);    
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }


mScannerView.setFlash(true);     // Turn on Flash Light
mScannerView.setFlash(false);    // Turn  Flash Light