我想在Android中以编程方式打开前闪光灯(不是相机预览)。我在谷歌上搜索了一下,但我找到的帮助把我转到了这个页面
有人有链接或示例代码吗?
我想在Android中以编程方式打开前闪光灯(不是相机预览)。我在谷歌上搜索了一下,但我找到的帮助把我转到了这个页面
有人有链接或示例代码吗?
当前回答
你也可以使用下面的代码来关闭闪光灯。
Camera.Parameters params = mCamera.getParameters()
p.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
其他回答
根据我的经验,如果你的应用程序设计为纵向和横向工作,你需要声明变量凸轮为静态。否则,onDestroy()在切换方向时被调用,它会破坏它,但不会释放Camera,因此不可能再次重新打开它。
package com.example.flashlight;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
public static Camera cam = null;// has to be static, otherwise onDestroy() destroys it
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void flashLightOn(View view) {
try {
if (getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA_FLASH)) {
cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "Exception flashLightOn()",
Toast.LENGTH_SHORT).show();
}
}
public void flashLightOff(View view) {
try {
if (getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA_FLASH)) {
cam.stopPreview();
cam.release();
cam = null;
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "Exception flashLightOff",
Toast.LENGTH_SHORT).show();
}
}
}
为了证明我必须画这条线
<uses-permission android:name="android.permission.CAMERA" />
从http://developer.android.com/reference/android/hardware/Camera.html
上面建议的台词对我不起作用。
你也可以使用下面的代码来关闭闪光灯。
Camera.Parameters params = mCamera.getParameters()
p.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
我得到了自动闪光与以下简单的三步。
我刚刚在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.
在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);
2022年Kotlin版本:
fun Context.isFlashLightAvailable() = packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)
val Context.camaraManager: CameraManager get() = getSystemService(CameraManager::class.java)
fun Context.toggleFlashLight(on: Boolean) {
camaraManager.run {
val firstCameraWithFlash = cameraIdList.find { camera ->
getCameraCharacteristics(camera).keys.any { it == FLASH_INFO_AVAILABLE }
}
firstCameraWithFlash?.let {
runCatching { setTorchMode(it, on) }.onFailure { Timber.e(it, "Error setTorchMode") }
} ?: Timber.e(Throwable("toggleFlashLight"), "Camera with flash not found")
}
}
或者用一个返回结果的挂起函数
suspend fun Context.toggleFlashLightWithResult(on: Boolean): Boolean {
return suspendCancellableCoroutine { cont ->
runCatching {
camaraManager.run {
val firstCameraWithFlash = cameraIdList.find { camera ->
getCameraCharacteristics(camera).keys.any { it == FLASH_INFO_AVAILABLE }
}
val callback = object : CameraManager.TorchCallback() {
override fun onTorchModeChanged(cameraId: String, enabled: Boolean) {
super.onTorchModeChanged(cameraId, enabled)
if (cont.isActive) cont.resume(enabled)
}
}
if (firstCameraWithFlash == null) {
Timber.e(Throwable("toggleFlashLight"), "Camera with flash not found")
cont.resume(false)
} else {
Timber.tag("~!").d("firstCameraWithFlash: $firstCameraWithFlash")
setTorchMode(firstCameraWithFlash, on)
registerTorchCallback(mainExecutor, callback)
}
cont.invokeOnCancellation { unregisterTorchCallback(callback) }
}
}
.onFailure { Timber.e(it, "Error toggleFlashLight") }
}
}