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);
}
}
Related
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 am using android's Flash Light to turn on the flashlight from within my app,
its working fine for
1.Sony Xperia
2.Galaxy Tab 10 inches
But, accidentally the same code is not working for Samsung Galaxy Tab 7 inches.
The method i have written is :
private void toggleFlashlight() {
Parameters p = mCamera.getParameters();
if (!mToogleFlashlight) {
mToogleFlashlight = true;
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
} else {
mToogleFlashlight = false;
p.setFlashMode(Parameters.FLASH_MODE_OFF);
}
if (mCamera != null) {
mCamera.setParameters(p);
}
}
Along with this I have also included the permissions and feature list I had to, in the AndroidManifest.xml
Only if there was some problem in the Manifest(either some tag was missing) it wouldnt work for the other devices as well.
I mean I can not track this what kind of mind boggling issue this is.
Please help me with some fix, have deadlines to meet and already screwed in this issue.
I think this solves your problem.
Maybe you should also test the behavior with this app. It has an debug screen pressing the "Volume down"-button (originally posted here).
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?
In my current application, I've got a class holding an instance of a Camera object and trying to do the following:
1) Wait for a specified time, e.g. nothing (this is done via a TimerTask)
2) Request to focus via autoFocus
3) In autoFocus callback, request OneShotPreviewCallback
4) In preview callback, save image
5) Repeat
While the white balancing is working fine prior to the first autoFocus, it stops after the first focussing has been done. Well, of course I looked up the API, and there is one interesting statement in the autoFocus description.
But auto-focus routine may stop auto-exposure and auto-white balance transiently during focusing.
But it seems it is non stopped only transiently, but permantly. Funny enough, with the subsequent call of autoFocus, the camera tries to ajust the whitening again, but the correct value is mostly only with the second or third autoFocus.
I also tried to set the white balancing in code, but it didn't change anything.
setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
Does anyone else know this issue, or am I missing some point ? I know that I could permanently call autoFocus to force the white balancing, but that doesn't seem the optimal way for me, because prior to the first call auf autoFocus, it works perfectly fine.
P.S.: I'm testing on a Samsung Galaxy S2 with Android 4.0.3.
I have ran into similar issue on Samsung Galaxy 2 Duos 2. In this case, the auto exposure settings have stopped working instead of the WB. I tried to cycle (on/off) the auto exposure param and it worked for me.
mCamera.autoFocus(new Camera.AutoFocusCallback() {
#Override
public void onAutoFocus(boolean b, Camera camera) {
Camera.Parameters params = camera.getParameters();
if (params.isAutoExposureLockSupported()) {
params.setAutoExposureLock(true);
camera.setParameters(params);
params = camera.getParameters();
params.setAutoExposureLock(false);
camera.setParameters(params);
}
}
});
I've got the similar problem on Samsung Galaxy Ace - after first autofocus, camera white balancing turns off and does not turn on again, no matter how much I do autofocus after.
As there are no API methods to tell camera to resume white balancing, and resetting the camera parameters in autofocus callback doesn't do the trick, my guess is that it is a bug in camera driver in Samsung phones - I've tried my application with different phones and only on this Samsung Galaxy Ace (GT-S5830; updated to Android 2.3.3), camera white balancing does not resume after autofocusing.
Maybe we should issue a bug ticket on developer.samsung.com?
It seems that
mCamera.stopPreview();
mCamera.startPreview();
in AutoFocusCallback can enable auto exposure again, but bringing a very short pause on the preview as side effect.
I've been searching for a few days but I can't find a way to set the flash mode of the camera in an HTC Wildfire. The same code works for the Nexus S. This is the code I am currently using.
//Code block to toggle flash setting between on and off
Camera.Parameters param = mCameraDevice.getParameters();
flashModes = param.getSupportedFlashModes();
if (flashModes != null) {
currentFlashMode = param.getFlashMode();
if (currentFlashMode.equals(Parameters.FLASH_MODE_OFF)) {
currentFlashMode = Parameters.FLASH_MODE_ON;
}
else {
currentFlashMode = Parameters.FLASH_MODE_OFF;
}
param.setFlashMode(currentFlashMode);
mCameraDevice.setParameters(param);
}
I have verified that even in the HTC Wildfire the if conditions are satisfied and set parameters gets called. Unfortunately it seems to have no effect and the default flash setting of the camera is always used.
The flash parameter is not set in any other part of the code. I've seen some apps successfully set the flash mode on the Wildfire, so I;m sure I'm doing some thing wrong. Any help would be greatly appreciated.
I know that HTC devices are using a different trick. Have a look at the following piece of code - http://www.java2s.com/Open-Source/Android/Tools/quick-settings/com/bwx/bequick/flashlight/HtcLedFlashlight.java.htm
It's taken from the quick-settings app.