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.
Related
I'm using Android + Opencv(new to opencv) and I'm currently working with real time object detection (the object stays really close to the android device Camera) , and I noticed that the Android camera's autoFocus keeps modifying my frames (kind of 'zoom in' and 'zoom out' effect) which make it harder for me to keep tracking the object.
I need to turn the "AUTO FOCUS" off because in my case the more blurred image input I have, the better, and I also need to turn the AutoWhiteBalance off as well, or maybe set to a different value.
I would like to know how to do it through my OpenCV CameraBridgeViewBase so I could modify the camera's Focus/WhiteBalance settings.
I've trying to find a way to solve it, and I noticed that many people face the same problems.
Here, at Stack Overflow, would be a great place to find someone who have worked with that and found a good way to overcome these problems.
create your own subclass of javacameraview
public class MyJavaCameraView extends JavaCameraView {
where you can have access to mCamera;
add whatever camera access using method you are interested in
for example
// Setup the camera
public void setFlashMode(boolean flashLightON) {
Camera camera = mCamera;
if (camera != null) {
Camera.Parameters params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
and use this new class as part of the main activity
//force java camera
mOpenCvCameraView = (MyJavaCameraView) findViewById(R.id.activity_surface_view);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.enableView();
I have a camera in my app and I want to make it auto focus continuously in the same way that the phone's camera does it. I found the modes FOCUS_MODE_CONTINUOUS_VIDEO and FOCUS_MODE_CONTINUOUS_PICTURE, but they are not supported by some of the HTC Gingerbread phones I'm testing on.
This is what I'm doing to determine whether I can use these modes:
Camera.Parameters parameters = mCamera.getParameters();
List<String> supportedFocusModes = parameters.getSupportedFocusModes();
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH &&
supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
}
else if (supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
}
else if (supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
// auto focus on request only
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
}
Running on several different Gingerbread HTC phones I don't get the continuous modes back, but I get "auto". This lets me auto focus on demand (when I call mCamera.autoFocus(null), but the camera will not refocus if the user moves the camera.
I cannot set the focus mode to anything the camera does not support, and if I do it shows up blank.
One solution that I tried is to call mCamera.autoFocus(null) on a timer. This causes the camera to refocus continuously, even if it is already in focus.
Is there a way to implement a continuous auto focus experience on these phones? When I look at HTCs camera app on these phones it does have continuous auto focus - as you move around the camera refocuses and does not keep refocusing once the picture is in focus.
We had a requirement to support a very wide range of phones. My solution in the end was to handle each case differently.
For the scenario of phones without continuous auto-focus support I implemented a utility to listen to the accelerometer using SensorManager and trigger mCamera.autoFocus(...) whenever the user moves the camera.
There were also some older tablets that only supported fixed focus (who would use such a thing!), in that case the picture needed to be taken immediately - not on the focus callback.
For most modern phones the code snippet above was fine to use FOCUS_MODE_CONTINUOUS_PICTURE.
I got a similar pb on my samsung S4 and I solved it with:
camera.setParameters(parameters);
camera.autoFocus(null);
This is suggest in the Google doc here.
To make the camera parameters take effect, your application has to call setParameters(Camera.Parameters).
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.
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.