我想在Android中以编程方式打开前闪光灯(不是相机预览)。我在谷歌上搜索了一下,但我找到的帮助把我转到了这个页面

有人有链接或示例代码吗?


当前回答

我已经在我的应用程序中通过使用SurfaceView的片段实现了这个功能。这个stackoverflow问题的链接和答案可以在这里找到

希望这对你有所帮助。

其他回答

我得到了自动闪光与以下简单的三步。

我刚刚在Manifest.xml文件中添加了相机和Flash权限

< uses-permission android: name = " android.permission。相机" / > < uses-feature android: name = " android.hardware。相机" / > < uses-permission android: name = " android.permission.FLASHLIGHT " / > < uses-feature android: name = " android.hardware.camera。Flash " android:required="false" />

In your Camera Code do this way. //Open Camera Camera mCamera = Camera.open(); //Get Camera Params for customisation Camera.Parameters parameters = mCamera.getParameters(); //Check Whether device supports AutoFlash, If you YES then set AutoFlash List<String> flashModes = parameters.getSupportedFlashModes(); if (flashModes.contains(android.hardware.Camera.Parameters.FLASH_MODE_AUTO)) { parameters.setFlashMode(Parameters.FLASH_MODE_AUTO); } mCamera.setParameters(parameters); mCamera.startPreview(); Build + Run —> Now Go to Dim light area and Snap photo, you should get auto flash light if device supports.

试试这个。

CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    String cameraId = null; // Usually front camera is at 0 position.
    try {
        cameraId = camManager.getCameraIdList()[0];
        camManager.setTorchMode(cameraId, true);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }

API 23或以上版本(Android M, 6.0)

打开代码

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    String cameraId = null; 
    try {
        cameraId = camManager.getCameraIdList()[0];
        camManager.setTorchMode(cameraId, true);   //Turn ON
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

关闭代码

camManager.setTorchMode(cameraId, false);

和权限

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

额外的编辑

人们仍然在为我的答案投票,所以我决定发布额外的代码 这是我当时对这个问题的解决方案:

public class FlashlightProvider {

private static final String TAG = FlashlightProvider.class.getSimpleName();
private Camera mCamera;
private Camera.Parameters parameters;
private CameraManager camManager;
private Context context;

public FlashlightProvider(Context context) {
    this.context = context;
}

private void turnFlashlightOn() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        try {
            camManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
            String cameraId = null; 
            if (camManager != null) {
                cameraId = camManager.getCameraIdList()[0];
                camManager.setTorchMode(cameraId, true);
            }
        } catch (CameraAccessException e) {
            Log.e(TAG, e.toString());
        }
    } else {
        mCamera = Camera.open();
        parameters = mCamera.getParameters();
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }
}

private void turnFlashlightOff() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        try {
            String cameraId;
            camManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
            if (camManager != null) {
                cameraId = camManager.getCameraIdList()[0]; // Usually front camera is at 0 position.
                camManager.setTorchMode(cameraId, false);
            }
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    } else {
        mCamera = Camera.open();
        parameters = mCamera.getParameters();
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        mCamera.setParameters(parameters);
        mCamera.stopPreview();
    }
}
}

在Marshmallow和上面的例子中,CameraManager的“setTorchMode()”似乎是答案。这对我来说很管用:

 final CameraManager mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
 CameraManager.TorchCallback torchCallback = new CameraManager.TorchCallback() {
     @Override
     public void onTorchModeUnavailable(String cameraId) {
         super.onTorchModeUnavailable(cameraId);
     }

     @Override
     public void onTorchModeChanged(String cameraId, boolean enabled) {
         super.onTorchModeChanged(cameraId, enabled);
         boolean currentTorchState = enabled;
         try {
             mCameraManager.setTorchMode(cameraId, !currentTorchState);
         } catch (CameraAccessException e){}



     }
 };

 mCameraManager.registerTorchCallback(torchCallback, null);//fires onTorchModeChanged upon register
 mCameraManager.unregisterTorchCallback(torchCallback);

Android棒棒糖引入了camera2 API,并弃用了之前的camera API。但是,使用已弃用的API来打开flash仍然有效,并且比使用新的API简单得多。

似乎这个新的API是专门用于全功能的相机应用程序的,它的架构师并没有真正考虑到更简单的用例,比如打开手电筒。现在要做到这一点,必须获得CameraManager,使用虚拟Surface创建CaptureSession,最后创建并启动CaptureRequest。包括异常处理,资源清理和长回调!

要了解如何打开Lollipop和更新的手电筒,请查看AOSP项目中的FlashlightController(尝试找到已修改的旧使用api的最新版本)。不要忘记设置所需的权限。


Android棉花糖最终介绍了一个简单的方法来打开闪光灯setTorchMode。