I am using CameraManager and CameraCharacteristics
I would like to know how to check if the flashlight is turned on
Try this bit of code
public boolean FlashStatus() {
Camera.Parameters parameters = camera.getParameters();
return parameters.getFlashMode() == "FLASH_MODE_TORCH";
}
Related
I am getting following error
Exception java.lang.RuntimeException: setParameters failed
android.hardware.Camera.native_setParameters (Camera.java)
android.hardware.Camera.setParameters (Camera.java:1946)
in code below. I have no clue what wrong i am doing below.
Camera mCamera = Camera.open();
Parameters params = mCamera.getParameters();
if (params.getFlashMode() != null)
params.setFlashMode(Parameters.FLASH_MODE_OFF);
if (nightMode && params.getSceneMode() != null)
params.setSceneMode(Parameters.SCENE_MODE_NIGHT);
if (params.getSupportedFocusModes().contains(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
params.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
} else if (params.getSupportedFocusModes().contains(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
params.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
} else if (params.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_INFINITY)) {
params.setFocusMode(Parameters.FOCUS_MODE_INFINITY);
}
mCamera.setParameters(params);
this error is occurring in some devices like samsung mostly.
Requesting for help.Thanks in advance.
Your params could be not supported by device. You can detect available focus modes with getSupportedFocusModes method of Camera.Parameters class. If some mode doesn't contain in this list, you can't set it to your camera.
Edit
As Alex said in comment you can see error message in logcat.
you must check if the focus mode supported by the user device, not all devices has camera focus mode, you do it like this:
public boolean support_focus(Camera camera){
Camera.Parameters parameters = camera.getParameters();
List<String> focusModes = parameters.getSupportedFocusModes();
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO))
return true;
else
return false;
}
this check if device support FOCUS_MODE_AUTO, change that with your desired Parameter.
I am creating a camera in android using android.hardware.camera2...
I referred to this link
But, I do not know how to switch front camera and back camera.
Please give a advice!
First of all get the list of camera Ids from device
We can use CameraManager to iterate all the cameras that are available in the system, each with a designated cameraId. Using the cameraId, we can get the properties of the specified camera device. Those properties are represented by class CameraCharacteristics. Things like "is it front or back camera", "output resolutions supported" can be queried there.
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
try {
return manager.getCameraIdList();
} catch (CameraAccessException e) {
return null;
}
Now if you want to open Front camera
CameraCharacteristics characteristics
= manager.getCameraCharacteristics(cameraId);
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
//Do your code here (open Camera with Camera ID)
}
This is another method directly which return directly CameraId .
String getFrontFacingCameraId(CameraManager cManager){
for(final String cameraId : cManager.getCameraIdList()){
CameraCharacteristics characteristics = cManager.getCameraCharacteristics(cameraId);
int cOrientation = characteristics.get(CameraCharacteristics.LENS_FACING);
if(cOrientation == CameraCharacteristics.LENS_FACING_FRONT) return cameraId;
}
return null;
}
For More Information about Camera2 Api you can see Here
I am using following code to turn LED flashlight on and off:
public Flashlight(SurfaceView preview, Context context){
this.preview = preview;
this.context = context;
mHolder = preview.getHolder(); //mHolder is surfaceHolder
mHolder.addCallback(this);
try {
mCamera = Camera.open();
params = mCamera.getParameters();
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
//AUTOFOCUS LASER FIX ON LG G3
List<String> focusModes = params.getSupportedFocusModes();
if (focusModes.contains(params.FOCUS_MODE_INFINITY)) {
params.setFocusMode(params.FOCUS_MODE_INFINITY);
}
else{
if (focusModes.contains(params.FOCUS_MODE_FIXED))
params.setFocusMode(params.FOCUS_MODE_FIXED);
}
mCamera.setParameters(params);
cameraOpened = true;
}catch (Exception e){
cameraOpened = false;
e.printStackTrace();
}
}
private void turnOnFlashlight(){
flashlightOn = true;
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
}
private void turnOffFlashlight(){
flashlightOn = false;
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
}
It works great on most phones but I cant get it working on xperia Z5. I dont have Z5 for testing so I only know it from user response. So I would like to ask if there is any other (preferably working) way to turn on flashlight on Xperia Z5.
Thanks in forward
As already mentioned in the comment, I found there to be 3 steps of making the flash appear (seems to be working on all devices so far)
cam.setParameters(p); // will trigger flash on most devices
// Needed for some devices.
cam.setPreviewTexture(new SurfaceTexture(0));
// Needed for some more devices.
cam.startPreview();
Since you did 2 of those, try adding the PreviewTexture and it should work. The whole code of a working flashlight can be found here at Flashlight Widget
Camera cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
The above dose not work on Lollipop, Because Camera is deprecated in Lollipop. I cant able to find any other way to turn on flash programmatically in Lollipop. How can I achieve this. Thanks in advance.
Camera class is now deprecated.
For LOLLIPOP above you need to use camera2 Api
so nickkadrov's solution doesent work for 6.0 & above device,best way to on/off flash light is try code below
public static void toggleFlashLight(){
toggle=!toggle;
try {
CameraManager cameraManager = (CameraManager) getApplicationContext().getSystemService(Context.CAMERA_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
for (String id : cameraManager.getCameraIdList()) {
// Turn on the flash if camera has one
if (cameraManager.getCameraCharacteristics(id).get(CameraCharacteristics.FLASH_INFO_AVAILABLE)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
cameraManager.setTorchMode(id, true);
}
}
}
}
} catch (Exception e2) {
Toast.makeText(getApplicationContext(), "Torch Failed: " + e2.getMessage(), Toast.LENGTH_SHORT).show();
}
}
where toggle is class level static Boolean variable whose default value is false
static boolean toggle=false;
mCam = Camera.open();
Camera.Parameters p = mCam.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCam.setParameters(p);
mPreviewTexture = new SurfaceTexture(0);
try {
mCam.setPreviewTexture(mPreviewTexture);
} catch (IOException ex) {
// Ignore
}
mCam.startPreview();
It works for me on Android 5.0.x. And don't forget to add permission in manifest for camera usage.
<uses-permission android:name="android.permission.CAMERA" />
Your code should actually work. Please check if you added the permission for using the camera properly:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
This should be added to your AndroidManifest above of your other specifications.
Additionally, there is an interesting discussion about different devices and an example which should work on every device here: Flashlight in Android
If you dont want to use the deprecated API, you can check out:
Package Summary of Camera2
Camera device specification on the new api
Unfortunately I can not give you an example for using the new API, because I did not use it myself yet.
How I check the flash light available on device?also want to know how can I On/Off the flash light?
I have put the code but not working right now?
I search out this
http://gitorious.org/rowboat/frameworks-base/commit/eb9cbb8fdddf4c887004b20b504083035d57a15f
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/com/android/server/LightsService.java#LightsService
Please can tell which I should use?
Thank You.
You can use the following
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
which will return true if a flash is available, false if not.
See http://developer.android.com/reference/android/content/pm/PackageManager.html for more information.
boolean hasFlash =this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
or
public boolean hasFlash() {
if (camera == null) {
return false;
}
Camera.Parameters parameters = camera.getParameters();
if (parameters.getFlashMode() == null) {
return false;
}
List<String> supportedFlashModes = parameters.getSupportedFlashModes();
if (supportedFlashModes == null || supportedFlashModes.isEmpty() || supportedFlashModes.size() == 1 && supportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)) {
return false;
}
return true;
}
First you get supported flash modes:
camera = Camera.open(i); // Introduced in API level 9
parameters = camera.getParameters();
String[] flashModes = parameters.getSupportedFlashModes();
And then you check if this array contains the correct constants like: "auto", "on", "off".
More info in:
http://developer.android.com/reference/android/hardware/Camera.Parameters.html#FLASH_MODE_AUTO
This can help full to turn on/off flash light of device. This is give me satisfaction, Hope be useful to you also.
Turn On camera Flash
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
Turn Off camera Flash
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
Put this Permission in manifest file
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
For more detail you can go HERE.