Android flash camera parameter not working - android

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.

Related

Android camera API blurry image on Samsung devices

After implementing the camera2 API for the inApp camera I noticed that on Samsung devices the images appear blurry. After searching about that I found the Sasmung Camera SDK (http://developer.samsung.com/galaxy#camera). So after implementing the SDK on Samsung Galaxy S7 the images are fine now, but on Galaxy S6 they are still blurry. Someone experienced those kind of issues with Samsung devices?
EDIT:
To complement #rcsumners comment. I am setting autofocus by using
mPreviewBuilder.set(SCaptureRequest.CONTROL_AF_TRIGGER, SCaptureRequest.CONTROL_AF_TRIGGER_START);
mSCameraSession.capture(mPreviewBuilder.build(), new SCameraCaptureSession.CaptureCallback() {
#Override
public void onCaptureCompleted(SCameraCaptureSession session, SCaptureRequest request, STotalCaptureResult result) {
isAFTriggered = true;
}
}, mBackgroundHandler);
It is a long exposure image where the use has to take an image of a static non moving object. For this I am using the CONTROL_AF_MODE_MACRO
mCaptureBuilder.set(SCaptureRequest.CONTROL_AF_MODE, SCaptureRequest.CONTROL_AF_MODE_MACRO);
and also I am enabling auto flash if it is available
requestBuilder.set(SCaptureRequest.CONTROL_AE_MODE,
SCaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
I am not really an expert in this API, I mostly followed the SDK example app.
There could be a number of issues causing this problem. One prominent one is the dimensions of your output image
I ran Camera2 API and the preview is clear, but the output was quite blurry
val characteristics: CameraCharacteristics? = cameraManager.getCameraCharacteristics(cameraId)
val size = characteristics?.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)?.getOutputSizes(ImageFormat.JPEG) // The issue
val width = imageDimension.width
val height = imageDimension.height
if (size != null) {
width = size[0].width; height = size[0].height
}
val imageReader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 5)
The code below was returning a dimension about 245*144 which was way to small to be sent to the image reader. Some how the output was stretching the image making it end up been blurry. Therefore I removed this line below.
val size = characteristics?.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)?.getOutputSizes(ImageFormat.JPEG) // this was returning a small
Setting the width and height manually resolved the issue.
You're setting the AF trigger for one frame, but then are you waiting for AF to complete? For AF_MODE_MACRO (are you verifying the device lists support for this AF mode?) you need to wait for AF_STATE_FOCUSED_LOCKED before the image is guaranteed to be stable and sharp. (You may also receive NOT_FOCUSED_LOCKED if the AF algorithm can't reach sharp focus, which could be because the object is just too close for the lens, or the scene is too confusing)
On most modern devices, it's recommended to use CONTINUOUS_PICTURE and not worry about AF triggering unless you really want to lock focus for some time period. In that mode, the device will continuously try to focus to the best of its ability. I'm not sure all that many devices support MACRO, to begin with.

Strange android camera behaviour when torch is on

I have following android code (written here in pseudocode):
mCamera = configAndInitialize(); //all I want to do before taking picture
mCamera.startPreview();
mCamera.torchOn(); //setting parameters with flash mode torch
onClick(){
mCamera.stopPreview();
mCamera.takePicture();
mCamera.torchOff();
}
Sometimes (often when phone was recently restarted and camera wasn't in use until this app) this code ends with error 100 Camera server died. If camera took successfully picture before it usually works.
I was debugging it for great amount of time and I found out it works when I comment out lines with torch. I can see torch working in both cases when taking pictures works or not.
Code of torchOn is following:
if(mCamera != null){
mCamera.stopPreview();
Camera.Parameters p = mCamera.getParameters();
List<String> supported = p.getSupportedFlashModes();
if (supported.contains(Camera.Parameters.FLASH_MODE_TORCH)) {
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
mCamera.setParameters(p);
mCamera.startPreview();
}
Is there any reason why taking picture could not work because of torch? I observed it happend on Motorola Razr and Samsung Galaxy SIII.
I installed on my device two version of this app (with different name and so on). And I do following:
Restart device
Tried app-with-torch
If app-with-torch does work back in point 1.
Tried app-without-torch
Tried app-with-torch
And the results are followings:
App-without-torch works always
App-with-torch in about 80% of tries doesn't work at point 2. (after restart)
App-with-torch works always at point 5. (after app-without-torch was used)
My app start working even if I add torchOff() just before taking picture.
We could call this the " Monty Python Dead Parrot Log.d answer " :-P I wish I had a solution for you - do have some suggestions though. Heisenbugs are difficult to catch.
Does the torch have an isOn() test ?
Similarly (I don't recall), does the camera have an isReady() test ?
Can you tell from the logs if the camera dies before, during, or after mCamera.torchOn() or .torchOff() ?
What would happen if you stretch out the time span between calls ? This wouldn't be useable for the real app, but might help you monitor and catch what's happening. Say something like this in pseudo code:
try {
// Log.d ("cam", "here 1") ;
mCamera = configAndInitialize();
// Log.d ("cam", "here 2");
if ( mCamera.isReady() ) { // or isConfigured / initialized
// Log.d ("cam", "here 2");
Camera.startPreview();
// Log.d ("cam", "here 2");
thisThread.setDelay (200); // millisecs. try even up to 2000 ms !
// Log.d ("cam", "here 4");
mCamera.torchOn();
// Log.d ("cam", "here 5");
thisThread.setDelay (200); // again up to 2000 ms
// Log.d ("cam", "here 6");
}
} catch (Exception e) {
Log.d ("oops!", e.toString() );
}
The other thing to monitor, or make sure of, is that the camera and torch really are ready before that onClick can fire. Sprinkle some delays like the Log.d's all around and see. It might need a call back (mCamera.isReady() then enable onClick ).
Another thing to do is see if you can dig up the camera's source code (or the torch's) and GREP for error 100 - or is that a generic android 100 ?
I'm sure you're well aware of how much stuff happens when that camera gets fired up - seems like hundreds of calls. Since some of these low level items are async (cam is hardware after all), I suspect you're getting a NPE or a insufficiently initialized object. Not all NPE's etc get trapped, so it might just die on one that would not be there if delayed or syncronized sequences are used.
( HTH - I feel your pain, ari, I had to do a lot of camera stuff recently. Debugging on Samsung SIII is prohibitively time consuming. )
[EDIT] you've probably already found this link, but just in case:
How to turn on camera flash light programmatically in Android?
I think this is related to each OEM's implementation of the Android camera HAL (Hardware Abstraction Layer). This problem has also happened to me, I'm not sure but I suspect most camera HALs' torch mode works only in video capture, as that's where it's most used anyways. Try recording a video with the torch on to check it out.
One possible workaround would be to set your camera's flash mode to FLASH_MODE_ON just before taking the picture and then returning to FLASH_MODE_TORCH after the pic is taken if you need it again.

Android: Flashlight working fine in Galaxy 10 inch Tablet but not in Galaxy 7 inch Tablet

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).

Android flash camera turns off just before taking the picture

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);
}
}

Android 4 - Camera White Balancing stops after autoFocus

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.

Categories

Resources