I am using LG Optimus 3D model, I am able to turn on the torch light when I switch the camera to video recording mode. In normal capture mode, I am only able to turn on flash light.
I have tried several way by adding different parameters into the Camera object, but none of them work. My code is as below:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flash_light);
PackageManager pm = getPackageManager();
if (! pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
Toast.makeText(this, "Your phone does not have flash light support.", Toast.LENGTH_LONG).show();
finish();
}
camera = Camera.open();
cParameters = camera.getParameters();
cParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(cParameters);
}
No point I cannot turn it on programmatically, I have checked as below to ensure my phone support torch mode, in fact, I am really able to turn on for my device.
List<String> abc = cParameters.getSupportedFlashModes();
for (String a : abc){
Log.d("mode: ", a);
}
Please help, anyway to do that?
* The firmware is 2.2
use the following
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
to see if a flash is available or not. It will will return true or false.
More here on how to actually implement the code.
How to turn on the Android Flashlight
Use camera flashlight in Android
How to turn on camera flash light programmatically in Android?
Related
I am developing an app where I want when a user presses a button front flashlight is on. I know how to open a back camera flash light.
But I don't know how to open the front camera flashlight within the same application. Anyone will guide me how to do this?
I don't think there is such thing as "Front Flash Light". The only thing I can think of at this stage is the Notification LED light at the front. Even looking at my Samsung S6 Edge device, I cannot find any front flash light on my camera.
The following code only applies to the rear flash light. I believe there are three things you will need to do.
1 - Check to see if there is availability for flash on the device
2 - Set the corresponding permission in your manifest
3 - Implement the code to operate the flash functions
This code will return a boolean based on the availability for flash feature
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
Next are your permissions in the manifest file
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
The first permission is for the camera, second permission is for the flashlight where you don't have to activate the camera hardware
And finally, the code to turn on the flash
Camera myCamera = Camera.open();
Parameters myParameters = myCamera .getParameters();
myParameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
myCamera.setParameters(p);
myCamera.startPreview();
To turn the service off
myCamera.stopPreview();
myCamera.release();
Additional function to test
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(cameraId, true);
flashLightStatus = true;
imageFlashlight.setImageResource(R.drawable.btn_switch_on);
} catch (CameraAccessException e) {
}
This may assist in getting the front camera accessibility from getCameraIdList function. Usually, the front camera is at the first position.
I'm using Camera Package for developing android app to turn on flash light, like this:
Camera mCamera = Camera.open();
List<String> flashModes = mCamera.getParameters().getSupportedFlashModes();
if(flashModes != null && flashModes.contains(Parameters.FLASH_MODE_TORCH)){
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(parameters);
} else if (flashModes != null && flashModes.contains(Parameters.FLASH_MODE_ON)){
parameters.setFlashMode(Parameters.FLASH_MODE_ON);
mCamera.setParameters(parameters);
}
if(!previewing){
mCamera.startPreview();
previewing = true;
}
It check supporting camera flash modes, and adjust it. It works in Samsung Android devices, and some others, but doesn't works in LG V20, LG V30 and Mi Android. I searched for this and tried:
mCamera.autoFocus(Camera.AutoFocusCallback)
mCamera.setPreviewTexture(SurfaceTexture)
But nothing works. So I used Camera2 package for api>23, like:
CameraManager mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
mCameraManager.setTorchMode("0", true);
And it works, but in Samsung device, an notification show up "Flashlight turned on" and it's very annoying.
So, I need the way to turn on flashlight for all devices with Camera package, or not to show up "Flashlight turned on" notification using Camera2 package.
I look forward to your reply, thanks.
The answer in below link saves my life!
The SurfaceView is essential for some devices.
LED flashlight on Galaxy Nexus controllable by what API?
I added ZXing qr scanner to my android project with compile 'me.dm7.barcodescanner:zxing:1.8.4'.
I am displaying the qrcode scanner inside of a fragment. I can scan and read without any problems. The issue I am having is trying to turn the flash (torch) on while the camera is scanning.
this code scans perfectly
View view = inflater.inflate(R.layout.fragment_qrscan, container, false);
mScannerView = new ZXingScannerView(getActivity());
mScannerView.setResultHandler(this); // Register ourselves as a
mScannerView.startCamera();
return mScannerView;
this code turns on the torch perfectly
camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
return view;
If I try to turn the light on and return mScannerview the scanner works but the light goes off. I can't seem to get the light and the qr scanner to work together successfully. The light does not work or my code will throw an exception depending on the code I try. I have tried to use the first bit of code and turn on the torch in the onResume(). But I am not successful. It is not a permission issue I can turn the light on and off without issues. I can scan no problem. But I can not scan with the light on.
Any help or suggestions would be greatly appreciated.
I have also been trying variations on this.
I saw one solution which involved starting a thread that runs every few seconds, checks if the light is off and (if it is off) turns it on. I couldn't get it working properly.
Another possible solution is to try recoding the project to allow external switching of the light on or off on creation of the page.
I am trying to build a custom camera.
I have a button that starts/stops the flash. I set it to flash mode TORCH when it is on.
On some devices I was told that the flash stops just before the picture is taken. This happens on some android 2.3 devices.
Did anyone else run into this problem? Any ideas why?
Yes I have found the same problem especially on HTC phones.
I have placed a condition to use FLASH_MODE_ON instead of FLASH_MODE_TORCH
List<String> pList = mCamera.getParameters().getSupportedFlashModes();
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
if (pList.contains(Parameters.FLASH_MODE_TORCH) && (!ManufacturerName.contains("htc"))) {
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
}
else
if (pList.contains(Parameters.FLASH_MODE_ON)) {
parameters.setFlashMode(Parameters.FLASH_MODE_ON);
}
}
I am writing an app that sets the flash mode to torch. I have been testing the application on my Droid X, and the LED light does not come on. I tried it on a Droid Incredible and it worked fine. I can't figure out what the problem is. Here is part of my code for turning on torch mode.
Camera mCamera = Camera.open();
Camera.Parameters params = mCamera.getParameters();
if(params.getFlashMode() != null){
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
mCamera.setParameters(params);
I have added mCamera.startPreview(); because I read that should make a difference, but it doesn't. I also made a list of available flash modes and displayed them to the screen to make sure that my Droid X does have torch mode, and it was in the list. I even created a new application from code I found online that turns the LED flash on and off with a button. Again it worked fine on the Droid Incredible but not the Droid X. Is there something I am missing to get this to run on the Droid X, or could it be something with Gingerbread? The Droid X is running Gingerbread and the Droid Incredible is running FroYo.
There are quite a few quirks when setting FLASH_MODE_TORCH.
Often you need to start a camera preview:
Camera mCamera = Camera.open();
mCamera.startPreview();
Camera.Parameters params = mCamera.getParameters();
if(params.getFlashMode() != null){
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
mCamera.setParameters(params);
That may resolve it on some phones, other phones also require the preview to be drawn to a SurfaceView. This can be done by implementing SurfaceHolder.Callback interface in your activity.
See an example here.
It could be that the Droid X doesn't support Torch Mode. Try something like this:
List<String> pList = camera.getParameters().getSupportedFlashModes();
if (pList.contains(Parameters.FLASH_MODE_TORCH))
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
Refer to Issue 191453:
SurfaceTexture mDummy = new SurfaceTexture(1); // any int argument will do
camera.setPreviewTexture(mDummy);
camera.startPreview();
The only thing I found that works on the Droid X is the code presented by Siddhpura Amit part way down the page in this answer Use camera flashlight in Android. He checks the manufacturer and checks to see if it contains the string "motorola." If so, he has special code that can switch the camera Flash LED on or off. I can verify that it does work as I have a Motorola Droid X.