其他分享
首页 > 其他分享> > 安卓 授权打开手电筒

安卓 授权打开手电筒

作者:互联网

打开手电筒

大家好,我自己的项目是 写一个 扫一扫 ,同时开启 手电筒的功能,扫一扫 我用到了 Zxing ,然后呢 我网上找了手电筒的方法,加上之后 发现 额 不行 ,一直报这个错 android.hardware.camera2.CameraAccessException: CAMERA_IN_USE (4): setTorchMode:1702: Torch for camera “0” is not available due to an existing camera user
显示了 相机被占用, 问题还在解决中,
在此 我发一些 授权打开手电筒的代码吧。
如下:
第一步 要添加权限那些;

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-feature android:name="android.hardware.camera.flash" />

第二步 添加布局,我就不发了,一个Button按钮就行

第三步就是关键代码了:
private void light() {

    /**
     * 检测 设备是否支持闪光灯
     */
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
        screenLight();
        return;
    }

    if (Build.VERSION.SDK_INT <Build.VERSION_CODES.M) {
        switchTorch(0);
    }else {
        //检查权限是否授予
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED) {
            //权限请求
            requestPermissions(new String[]{Manifest.permission.CAMERA},CAMERA_REQUEST_CODE);

        }else {
            switchTorch(1);
        }
    }

}


/**
 * 屏幕亮度
 */
private void screenLight() {
    Window window=getWindow();
    WindowManager.LayoutParams layoutParams=window.getAttributes();
    if (!isOpen ) {
        btn.setText("关闭手电筒");
        layoutParams.screenBrightness=BRIGHTNESS_OVERRIDE_FULL;
        isOpen=true;
    }else {
        btn.setText("打开手电筒");
        layoutParams.screenBrightness=BRIGHTNESS_OVERRIDE_NONE;

        isOpen=false;
    }
    window.setAttributes(layoutParams);
}

private void switchTorch(int flag) {

    if (!isOpen) {
        openTorch(flag);
        isOpen=true;
    }else {
        closeTorch(flag);
        isOpen=false;
    }
}

private void closeTorch(int flag) {
    btn.setText("打开手电筒");
    switch (flag){
        case 0:
            if (camera1 == null) {
                return;
            }
            camera1.stopPreview();
            camera1.release();
            break;
        case 1:
            if (cameraManager1 == null) {
                return;
            }
            if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.M) {
                try {
                    cameraManager1.setTorchMode("0",false);
                } catch (Exception e) {
                    //CameraAccessException 会在6.0一下报错 这里设置为Exception
                    e.printStackTrace();
                }
            }
            break;
    }
}

private void openTorch(int flag) {
    btn.setText("关闭手电筒");
    switch (flag){
        case 0:
            Log.d("info","关闭 手电筒");
            camera1= Camera.open();
            Camera.Parameters parameters=camera1.getParameters();
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            camera1.setParameters(parameters);
            camera1.startPreview();
            break;
        case 1:
            Log.d("info","打开 手电筒");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                cameraManager1= (android.hardware.camera2.CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
                Log.d("info","打开 手电筒 1");
            }
            if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.M) {
                try {
                    cameraManager1.setTorchMode("0",true);
                    Log.d("info","打开 手电筒 2");
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d("info","异常=="+ e.toString());
                }
            }
            break;
    }
}



@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (CAMERA_REQUEST_CODE==requestCode){
        if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED) {
            openTorch(1);
        }else {
            //权限拒绝
            Toast.makeText(this, "权限拒绝", Toast.LENGTH_SHORT).show();
        }
    }
}

好吧,就这样了, 大家应该可以看懂, 至于 一开始那个 相机被占用问题 ,我解决好了 ,再发布吧,哈哈哈

标签:info,Log,安卓,手电筒,grantResults,VERSION,Build,授权
来源: https://blog.csdn.net/mawlAndroid/article/details/117129085