Android 检查设备是否有摄像头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1944117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if device has a camera?
提问by mark
In my app, I'd like to use the camera, if the device has one. Are there any devices running android that do nothave a camera? By including the following into my manifest:
在我的应用程序中,如果设备有相机,我想使用相机。是否有任何运行 android 的设备没有摄像头?通过在我的清单中包含以下内容:
<uses-feature android:name="android.hardware.camera" android:required="false"/>
then it's basically saying "I'll use a camera if one exists, but don't need one to run the app".
那么它基本上是在说“如果有相机,我会使用相机,但不需要相机来运行应用程序”。
How could I check if a camera exists on the device, before attempting to use the Camera class?
在尝试使用 Camera 类之前,如何检查设备上是否存在摄像头?
采纳答案by user197385
I've not tried it, but:
我没试过,但是:
private android.hardware.Camera mCameraDevice;
try {
mCameraDevice = android.hardware.Camera.open();
} catch (RuntimeException e) {
Log.e(TAG, "fail to connect Camera", e);
// Throw exception
}
May be what you need.
可能是你需要的。
回答by dpjanes
This is what I'm using
这就是我正在使用的
import android.content.pm.PackageManager;
PackageManager pm = context.getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
}
All sorts of other fun things to test for are available too - the compass, is location available, is there a front facing camera: http://developer.android.com/reference/android/content/pm/PackageManager.html
各种其他有趣的测试也可用 - 指南针,位置可用,是否有前置摄像头:http: //developer.android.com/reference/android/content/pm/PackageManager.html
回答by Eddy Talvala
To find out how many cameras are available on your device, you can call:
要了解您的设备上有多少个摄像头可用,您可以拨打:
import android.hardware.Camera;
int numCameras = Camera.getNumberOfCameras();
if (numCameras > 0) {
hasCamera = true;
}
Camera.getNumberOfCameras() is static, so it doesn't require actually connecting to a camera. This works since API 9.
Camera.getNumberOfCameras() 是静态的,因此不需要实际连接到相机。这从 API 9 开始有效。
Edit:
编辑:
With the newer camera2 API, you can also call CameraManager.getCameraIdList(), which gives a list of the all the valid camera IDs, instead of just the count.
使用较新的 camera2 API,您还可以调用CameraManager.getCameraIdList(),它提供所有有效相机 ID 的列表,而不仅仅是计数。
回答by Vishwa
you should use this to find camera in your device
你应该用它来在你的设备中找到相机
public static boolean isCameraAvailable(Context context) {
return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
回答by Nurul Akter Towhid
Use the PackageManager.hasSystemFeature()method for checking Camera :
使用PackageManager.hasSystemFeature()方法检查 Camera :
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
Source: https://developer.android.com/guide/topics/media/camera.html#custom-camera
来源:https: //developer.android.com/guide/topics/media/camera.html#custom-camera
回答by Frank
Camera.getNumberOfCameras() is deprecated. You can use:
Camera.getNumberOfCameras() 已弃用。您可以使用:
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public int getNumberOfCameras() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
return ((CameraManager) getSystemService(Context.CAMERA_SERVICE)).getCameraIdList().length;
} catch (CameraAccessException e) {
Log.e("", "", e);
}
}
return Camera.getNumberOfCameras();
}
回答by samaniego
Try this :
尝试这个 :
/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
from : http://developer.android.com/guide/topics/media/camera.html
来自:http: //developer.android.com/guide/topics/media/camera.html
回答by DjP
by following way we can check does device has camera or not.
通过以下方式我们可以检查设备是否有摄像头。
/** Check if this device has a camera */
public static boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA))
{
return true;
}
else if(context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA_FRONT))
{
return true;
}
else {
return false;
}
}
回答by mabeiyi
If you are using Android 2.3, there are some APIs that you can check your camera status, such as the number of cameras (front and back)
如果您使用的是 Android 2.3,则可以使用一些 API 来检查您的相机状态,例如相机数量(正面和背面)
回答by MrG
try this
尝试这个
For front camera
对于前置摄像头
Context context = this;
PackageManager packageManager = context.getPackageManager();
if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {
Utils.makeAlertDialog(context, "Has Front Camera ?", "YES");
} else {
Utils.makeAlertDialog(context, "Has Front Camera ?", "NO");
}
for back camera
用于后置摄像头
Context context = this;
PackageManager packageManager = context.getPackageManager();
if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Utils.makeAlertDialog(context, "Has back Camera ?", "YES");
} else {
Utils.makeAlertDialog(context, "Has back Camera ?", "NO");
}
回答by Srikar Reddy
As per the documentation, you have to use Package Manager to check if Camera is available on the device or not
根据文档,您必须使用包管理器来检查相机是否在设备上可用
In Java:
在 Java 中:
final boolean isCameraAvailable = getPackageManager().hasSystemFeature(FEATURE_CAMERA);
In Kotlin:
在科特林:
val isCameraAvailable = packageManager.hasSystemFeature(FEATURE_CAMERA)