I am trying to write an app that requires the LED flash to go into torch mode. The problem is, Android 2.1 does not support this mode and therefore I cannot support the platform yet. Wouldn't be an issue, but I am writing it for my fiance and her Epic 4G only has 2.1 right now. I found some code samples that use some undocumented API calls and therefore work on the Motorola Droid and such, but they do not work on the Epic. Does anyone have some suggestions on where to look to find code that should help me get this working?
I'm finding that torch mode is generally working fine on 2.1 but I had the same problem with the Samsung Epic and found a hack around it.
Looking at the params returned by Camera.getParameters() when run on the Samsung Epic, I noticed that the flash-modes it claims to support are: flash-mode-values=off,on,auto;
torch-mode is not listed, implying it's not supported.
However, I found that this model would still accept that mode and WOULD turn the LED on! The bad news was that when later setting the flash-mode back to auto or off left the LED still lit! It will not turn off until you call Camera.release().
I guess that's why Samsung dont include it in the list of supported!?!
So...the method I use to toggle torch in a CameraHelper class is...
/***
* Attempts to set camera flash torch/flashlight mode on/off
* #param isOn true = on, false = off
* #return boolean whether or not we were able to set it
*/
public boolean setFlashlight(boolean isOn)
{
if (mCamera == null)
{
return false;
}
Camera.Parameters params = mCamera.getParameters();
String value;
if (isOn) // we are being ask to turn it on
{
value = Camera.Parameters.FLASH_MODE_TORCH;
}
else // we are being asked to turn it off
{
value = Camera.Parameters.FLASH_MODE_AUTO;
}
try{
params.setFlashMode(value);
mCamera.setParameters(params);
String nowMode = mCamera.getParameters().getFlashMode();
if (isOn && nowMode.equals(Camera.Parameters.FLASH_MODE_TORCH))
{
return true;
}
if (! isOn && nowMode.equals(Camera.Parameters.FLASH_MODE_AUTO))
{
return true;
}
return false;
}
catch (Exception ex)
{
MyLog.e(mLOG_TAG, this.getClass().getSimpleName() + " error setting flash mode to: "+ value + " " + ex.toString());
}
}
The activities that use this call it as follows...
private void toggleFlashLight()
{
mIsFlashlightOn = ! mIsFlashlightOn;
/**
* hack to fix an issue where the Samsung Galaxy will turn torch on,
* even though it says it doesnt support torch mode,
* but then will NOT turn it off via this param.
*/
if (! mIsFlashlightOn && Build.MANUFACTURER.equalsIgnoreCase("Samsung"))
{
this.releaseCameraResources();
this.initCamera();
}
else
{
boolean result = mCamHelper.setFlashlight(mIsFlashlightOn);
if (! result)
{
alertFlashlightNotSupported();
}
}
}
The magic that makes this work in releaseCameraResources() is that it calls Camera.release()....and then I have to reinitialize all my camera stuff for Samsung devices.
Not pretty but seems to be working for plenty of users.
Note that I do have a report of torch mode not working at all with this code on Nexus one but have been able to dig into it. It definitely works on HTC EVO and Samsung Epic.
Hope this helps.
In my case for Samsung devices I needed to set focus mode to infinity and it started to work
params.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
mCamera.setParameters(params);
mCamera.startPreview();
Related
This is something I've been struggling with for quite some time. A lot of Chinese Android manufacturers create Android TV boxes that are controlled using a remote with keypad controls.
I'm looking for a definitive way of detecting if the device uses navigational controls or if it is in fact using touch screen input. Certain devices might emulate touch screen input as mouse input as well, so it's kind of tricky.
Any ideas?
Please read about input controls in the following articles:
http://developer.android.com/training/keyboard-input/index.html
http://developer.android.com/training/keyboard-input/navigation.html
http://developer.android.com/training/tv/optimizing-navigation-tv.html
To see at run-time what types of navigation a user has available, use the Configuration class.
Configuration configuration = context.getResources().getConfiguration();
if (Configuration.NAVIGATION_NONAV == configuration.navigation) {
// Device has no navigation facility other than using the touchscreen.
} else if (Configuration.NAVIGATION_DPAD == configuration.navigation) {
// Device has a directional-pad (d-pad) for navigation.
} else if (Configuration.NAVIGATION_TRACKBALL == configuration.navigation) {
// Device has a trackball for navigation.
} // ... etc
Based on Jozua's answer, I created this simple method which can be used to determine if a device uses navigation controls from a number of factors. The code is written in a way that attempts to fail early.
/**
* Determines if the device uses navigation controls as the primary navigation from a number of factors.
* #param context Application Context
* #return True if the device uses navigation controls, false otherwise.
*/
public static boolean usesNavigationControl(Context context) {
Configuration configuration = context.getResources().getConfiguration();
if (configuration.navigation == Configuration.NAVIGATION_NONAV) {
return false;
} else if (configuration.touchscreen == Configuration.TOUCHSCREEN_FINGER) {
return false;
} else if (configuration.navigation == Configuration.NAVIGATION_DPAD) {
return true;
} else if (configuration.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH) {
return true;
} else if (configuration.touchscreen == Configuration.TOUCHSCREEN_UNDEFINED) {
return true;
} else if (configuration.navigationHidden == Configuration.NAVIGATIONHIDDEN_YES) {
return true;
} else if (configuration.uiMode == Configuration.UI_MODE_TYPE_TELEVISION) {
return true;
}
return false;
}
I have tested this on numerous phones, tablets, emulator configurations and Google TV. A number of devices are controlled using a remote control and a USB mouse. I haven't yet tested whether it works as expected on such devices.
I have a application which uses camera functionality in it but part of its functionality can also run without camera feature. SO I have put this in my manifest.
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false"/>
and in my code I check whether the device has camera or not using this
final boolean deviceHasCameraFlag = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
Now I am testing my code on a tablet which runs Android 4.0(ICS) and has no camera. But still I get True value for the deviceHasCameraFlag. Is this weird or am I missing something.
I tried different things and even tried the same thing on Bluetooth feature as Tablet even doesn't have Bluetooth feature. It works fine for Bluetooth but gives me true for camera.
Which device is it? The answer you get is a bug, and 4.0 is very old nowadays. Many tablets that still run this version were not crafted correctly, both hardware and software featuring multiple problems.
Regardless, you should always be prepared to handle failure on Camera.open() or Camera.open(0): for example, in some cases other software on your device will not release the camera gracefully.
So, in your case you have a false positive, you try to open the camera, it fails, and you continue as if there is no camera on the device, even if PackageManager thinks that PackageManager.FEATURE_CAMERA is availabe.
Though I have accepted Alex's answer I still want to put this one collectively as what can be the best solution in such condition.
What I found was in case of some low standard android devices
pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)
returns true even if camera doesn't exist and that seems to be a device bug for me which in unchecked.
So whenever there is scenario that you need to check if camera exists for a device or not best practice is something that I am putting below (best practice as per my knowledge if there is something more interesting and best solution that this you are welcome to put it here on this post)
int numberOfCameras = Camera.getNumberOfCameras();
context = this;
PackageManager pm = context.getPackageManager();
final boolean deviceHasCameraFlag = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
if( !deviceHasCameraFlag || numberOfCameras==0 )
{
Log.e(TAG, "Device has no camera" + numberOfCameras);
Toast.makeText(getApplicationContext(), "Device has no camera", Toast.LENGTH_SHORT).show();
captureButton.setEnabled(false);
}
else
{
Log.e(TAG, "Device has camera" + deviceHasCameraFlag + numberOfCameras);
}
In this I am checking both number of cameras as well as device has camera feature Boolean , so in any case it would not fail my condition.
In my case I had this code:
public boolean hasCameraSupport() {
boolean hasSupport = false;
if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) { //<- this constant caused problems
hasSupport = true;
}
return hasSupport;
}
and it kept returning false on a Genymotion device running Android 4.1.1 (API 16). Once I changed the constant PackageManager.FEATURE_CAMERA_ANY to PackageManager.FEATURE_CAMERA, my problems went away. I am guessing that not all devices/API levels support PackageManager.FEATURE_CAMERA_ANY.
I got it you will try this one definitely it will work....
import android.hardware.Camera;
int numCameras = Camera.getNumberOfCameras();
if (numCameras > 0) {
System.out.println("camera");
} else {
System.out.println("No Camera");
}
For CameraX, if the FEATURE_CAMERA_ANY method is still returning true when there is no Camera on device, you can add the below method. So whether FEATURE_CAMERA_ANY returns true or false when CameraX is getting initialized, Below method will make sure to do what you want if a camera is actually not available on device.
private CameraSelector cameraSelector;
private ProcessCameraProvider cameraAvailableCheck;
private ListenableFuture<ProcessCameraProvider> cameraAvailableCheckFuture;
private void checkIfAnyCameraExist()
{
cameraAvailableCheckFuture = ProcessCameraProvider.getInstance(context);
cameraAvailableCheckFuture.addListener(new Runnable() {
#Override
public void run() {
try {
cameraAvailableCheck = cameraAvailableCheckFuture.get();
if ((cameraAvailableCheck.hasCamera(cameraSelector.DEFAULT_BACK_CAMERA) || cameraAvailableCheck.hasCamera(cameraSelector.DEFAULT_FRONT_CAMERA) ))
{
//Do what you want if at least back OR front camera exist
}
else
{
//Do what you want if any camera does not exist
}
}
catch (ExecutionException | InterruptedException | CameraInfoUnavailableException e)
{
// No errors need to be handled for this Future.
// This should never be reached.
}
}
}, ContextCompat.getMainExecutor(this));
}
Please try this code:
private boolean isDeviceSupportCamera() {
if (getApplicationContext().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
Still it does't work then please let me know
I want to check if a device has any cameras before trying to open a qr code reader.
I have the following code:
public boolean checkDeviceCompatibility() {
PackageManager pm = context.getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
return true;
} else {
// use front camera
Toast.makeText(
context,
"This device does not have a back camera so it has automatically reverted to the front camera",
Toast.LENGTH_SHORT).show();
return true;
}
} else {
util.displayErrorDialog(
context,
"This device does not have any cameras and therefore cannot make use of the QR Code feature.");
return false;
}
}
But now if I run this code in debug mode on my galaxy S3 with two cameras. the first if statement is returned false.
Why could this be?
FEATURE_CAMERA_ANY was added in Android 4.2. hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY) should return false for any pre-4.2 device. If your S3 is still on 4.1, that would explain your problem.
To make it clear.
FEATURE_CAMERA_ANY was added to Android 4.2 ( API-17): Android - developers.
code:
public static boolean hasCamera(Context context) {
return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
Note that using this code will return false if device under version 4.2:
Then you should know that there is a bug with emulator when use FEATURE_CAMERA_ANY feature (with android 4.2 and above). see: Emulator does not honour Camera support flag
Thats why Im still using old way even its deprecated:
public static boolean hasCamera() {
return android.hardware.Camera.getNumberOfCameras() > 0;
}
I know is possible to detect if camera has flash integrated, using a method like this:
/**
* #return true if a flash is available, false if not
*/
public static boolean isFlashAvailable(Context context) {
return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}
but if the device has 2 cameras how can I test for each of them if has flash available?
For example on a Samsung S2 device, on native camera application when using the front camera the flash button is disabled, meaning is not available.
Thanks.
Paul's answer didn't work for me.
The front camera on a Galaxy Nexus has a valid flash-mode of FLASH_MODE_OFF, but it is the only supported option. This method will work in all situations:
private boolean hasFlash(){
Parameters params = mCamera.getParameters();
List<String> flashModes = params.getSupportedFlashModes();
if(flashModes == null) {
return false;
}
for(String flashMode : flashModes) {
if(Parameters.FLASH_MODE_ON.equals(flashMode)) {
return true;
}
}
return false;
}
If your app supports more than just FLASH_MODE_OFF and FLASH_MODE_ON, you'll need to tweak the if-check inside the loop.
-- Update --
Also you can add torch for flash in if condition if you really need to use torch.
if (Camera.Parameters.FLASH_MODE_ON.equals(flashMode) || Camera.Parameters.FLASH_MODE_TORCH.equals(flashMode)) {
I figured this by myself and I post here the solution, which is actually very simple:
/**
* Check if Hardware Device Camera can use Flash
* #return true if can use flash, false otherwise
*/
public static boolean hasCameraFlash(Camera camera) {
Camera.Parameters p = camera.getParameters();
return p.getFlashMode() == null ? false : true;
}
The above method is different by this one:
/**
* Checking availability of flash in device.
* Obs.: If device has 2 cameras, this method doesn't ensure both cameras can use flash.
* #return true if a flash is available in device, false if not
*/
public static boolean isFlashAvailable(Context context) {
return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}
I need to detect if the phone has a front facing camera, and if so, I need to calculate the megapixels. The same thing goes for a rear facing camera.
I know how to get the megapixels of a "Camera" object, but I don't know how to check for the other things.
P.s.: I would also be nice if you know a way to check if the Camera has flash or not, and other cool statistics about the camera
I always try to create helpers
check if you have a front Camera:
public static boolean checkCameraFront(Context context) {
if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {
return true;
} else {
return false;
}
}
Check if you have Camera in your device
public static boolean checkCameraRear() {
int numCamera = Camera.getNumberOfCameras();
if(numCamera > 0) {
return true;
} else {
return false;
}
}
http://developer.android.com/reference/android/hardware/Camera.html#getNumberOfCameras() , introduced in API lvl 9. This gets you the number of cameras
http://developer.android.com/reference/android/hardware/Camera.CameraInfo.html contains information of its facing direction.
http://developer.android.com/reference/android/hardware/Camera.Parameters.html#getPictureSize() is megapixels, if counted
http://developer.android.com/reference/android/hardware/Camera.Parameters.html#getFlashMode() returns null if no flash..
many other parameters can be gotten from the camera object too
http://developer.android.com/reference/android/hardware/Camera.html has step by step instructions for using camera. You can follow these instructions if you understand any object oriented language.