Android : Camera freezes when the flash is turned on - android

I'm working on an app that works with camera but I'm getting an error.
When the camera is active, the camera freezes when the flashlight is turned on or Error "E/Camera: Error 2". Many thanks.
public void FlashOn()
{
cam = Camera.open();
Camera.Parameters params = cam.getParameters();
if (params.getFlashMode().equals(Camera.Parameters.FLASH_MODE_ON))
{
params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
}
else
{
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
cam.setParameters(params);
cam.startPreview();
}
public void FlashOff()
{
Camera.Parameters params = cam.getParameters();
if (params.getFlashMode().equals(Camera.Parameters.FLASH_MODE_ON))
{
params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
}
else
{
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
cam.stopPreview();
cam.release();
}

I don't think that you have to use startPreview again in order to enable the flash. In an older project I did something like this to enable/disable flash:
Camera.Parameters params = camera.getParameters();
if (params.getFlashMode().equals(Camera.Parameters.FLASH_MODE_ON)) {
params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
} else {
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
camera.setParameters(params);
Also, you may want to open/close the camera with AsyncTask because it is a UI blocking operation.
Update: This is how your method should look
public void FlashOn() {
Camera.Parameters params = cam.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
cam.setParameters(params);
}
For FlashOff method, things will be similar.

Related

Cant open Camera Servoce Android

i stuck here with a Problem. Trying to build a torch app. Works fine, but when i switch fragment or go to homescreen and come back the flash light wont work. Error is failed to connect to camera service.
I think the Problem is, that I create a new Camera instance then, and the new cant connect to the camera anymore. But how should i solve it?
public class FlashCameraManager {
private boolean isFlashOn;
private Camera camera;
public Camera.Parameters params;
// getting camera parameters
public void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
camera = null;
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
} else {
camera.release();
camera = null;
}
}
public void FlashOnOff()
{
//Flash Aktivieren oder deaktivieren
if (isFlashOn)
{
//Turn Flash off
if (camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
Log.d("FlashCameraManager", "Turning Flash off");
}
else
{
// Turn Flash on
if (camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
Log.d("FlashCameraManager", "Turning Flash on");
}
}
public boolean isFlashActive()
{
//Prüfen ob Flash an oder aus ist
return isFlashOn;
}}
This is from the MainActivity
final ImageButton flash = (ImageButton) rootView.findViewById(R.id.none_flash);
if(camera == null) {
camera = new FlashCameraManager();
}
camera.getCamera();
flash.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Content
if (camera.isFlashActive())
{
//Turn Flash off
camera.FlashOnOff();
Log.d("NoneFragment", "Turning Flash off");
flash.setActivated(false);
}
else
{
//Turn Flash on
camera.FlashOnOff();
Log.d("NoneFragment", "Turning Flash on");
flash.setActivated(true);
}
}} );
After you are done with the camera (i.e. before exiting the application or launching another activity) make sure that you release the camera resources by calling the method release(), which, per the API Guide, "Disconnects and releases the Camera object resources". The API guide also provides some valueable insight into properly utilizing the class and performing simple operations, such as tasking a picture. The API Guide may be found here:
http://developer.android.com/reference/android/hardware/Camera.html
You might also want to consider taking a glance at the new camera API (android.hardware.camera2), as the current API that you are using is deprecated as of API level 21. The guide for the new API is found here:
http://developer.android.com/reference/android/hardware/camera2/package-summary.html

ImageButton turn on and turn off

How I can turn on and turn off camera light on ImageButton..
This is my code
public void liBtn(View v)
{
int tur=0;
if (tur==0)
{
Camera.Parameters p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
int tur=1;
}
if (tur==1)
{
Camera.Parameters p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.startPreview();
int tur=0;
}
}
Any ideas?
// Create a class member variable
private boolean isOn = false;
// And use it in your method
public void liBtn(View v) {
Camera.Parameters p = camera.getParameters();
if (!isOn) {
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
} else {
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
camera.setParameters(p);
camera.startPreview();
isOn = !isOn;
}
put int tur=0; outside your method.
create a global boolean variable isFlashOn and do some thing like this
public void liBtn(View v){
if (!isFlashOn)
{
Camera.Parameters p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isFlashOn =true;
}else{
Camera.Parameters p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.startPreview();
isFlashOn =false;
}
}

Android flash mode as torch doesnt works

I am using the camera API and flash mode as torch but after taking one picture the flash is turning off.How can i turn on the flash again.?I am using android 2.3,How can I use flash mode as torch
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // <15>
camera = Camera.open();
params = camera.getParameters();
List<Size> sizes = params.getSupportedPictureSizes();
Camera.Size size = sizes.get(0);
params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
params.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
params.setSceneMode(Camera.Parameters.SCENE_MODE_LANDSCAPE);
params.setFlashMode("torch");
params.setJpegThumbnailQuality(100);
params.setExposureCompensation(0);
params.setJpegQuality(100);
// params.setPreviewSize(PreviewSizeWidth,PreviewSizeHeight);
//now that you have the list of supported sizes, pick one and set it back to the parameters...
//int w=0,h=0;
for(int i=0;i<sizes.size();i++)
{
if(sizes.get(i).width > size.width)
size = sizes.get(i);
}
params.setPictureSize(size.width, size.height);
Toast.makeText(getContext(), size.width+"and"+size.height,Toast.LENGTH_SHORT).show();
camera.setParameters(params);
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
{
params.set("orientation", "portrait");
camera.setDisplayOrientation(90);
}
camera.startPreview();
try
{
camera.setPreviewDisplay(holder);
}
catch (IOException exception)
{
camera.release();
camera = null;
}
}
}
Check this
private Camera camera;
if (camera == null) {
} else {
// Set the torch flash mode
Parameters param = camera.getParameters();
param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
try {
camera.setParameters(param);
camera.startPreview();
} catch (Exception e) {
}
}
What do you do with the photo after you take it? Do you move to a different activity/fragment and then come back into the take photo fragment? Do you simply store the photo without ever leaving the current activity/fragment?
If you don't leave the activity/fragment, I'd suggest trying to restart the torch at the end of your last Camera.PictureCallback. Remember, the camera preview stops after taking a photo so you have to restart the preview. Perhaps something like:
mPictureCallback = new Camera.PictureCallback(){
#Override
public void onPictureTaken(byte[] data, Camera camera) {
// ... process your byte data ...
if(mCamera != null){
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
try{
mCamera.startPreview();
}catch(Exception e){
e.printStackTrace();
}
}
}
};
you need to call mCamera.stopPreview() before calling mCamera.setParameters(params); like below.
mCamera.stopPreview();
mCamera.setParameters(cameraParameters);
mCamera.startPreview();

Android Camera LED light goes to Off after 2 seconds?

I am using the camera flash light in my application, I was done coding for that, it's working on/off the light.
but after 2 seconds it goes to off. If I press the on button again it was giving force close.
This is the code i am using for this, please help me.
I want this like if user presses the on button light On, upto user press Off button.
private void processOffClick() {
//togglebutton.setButtonDrawable(R.drawable.offbutton);
System.out.println("in off state");
if( cam != null ){
cam.stopPreview();
cam.release();
}
}
private void processOnClick() {
//togglebutton.setButtonDrawable(R.drawable.onbutton);
System.out.println("in on state");
cam = Camera.open();
Parameters params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
cam.setParameters(params);
cam.startPreview();
cam.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
}
});
}
put the lines:
Parameters params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
cam.setParameters(params);
in processOffClick instead of putting it in processOnClick
like that:
boolean clicked = false;
private void processOffClick() {
//togglebutton.setButtonDrawable(R.drawable.offbutton);
clicked = false;
System.out.println("in off state");
if( cam != null ){
cam.stopPreview();
cam.release();
}
}
private void processOnClick() {
clicked = true;
//togglebutton.setButtonDrawable(R.drawable.onbutton);
System.out.println("in on state");
cam = Camera.open();
Parameters params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
while(clicked) {
cam.setParameters(params);
cam.startPreview();
cam.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
}
});
}
}
It might work, i didn't check the code
I added a while loop so it would hold the flash and the focus until its unclicked.
My experience says, that flash mode ought to be "TORCH" ( if supported ) and it is started only when you start preview. However, cameras behave very differently on different devices and not always as advertised in their capabilities descrioptors
1.Turn on
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
2. Turn off
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
And, put following permission on AndroidManifest.xml.
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
see this one http://www.mkyong.com/android/how-to-turn-onoff-camera-ledflashlight-in-android/

How to turn on the Android Flashlight

Update
Check out my answer
Original
I'm trying to turn on the camera flashlight on the LG Revolution within my program. I use the torch mode method which works on most phones but not on LG phone. Does anyone know how to get it to work on LG's or specifically the Revolution?
Here's my manifest:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
Here's my current code:
public Camera camera = Camera.open();
public Camera.Parameters Flash = camera.getParameters();
With my on create:
Flash.setFlashMode("torch");
Parameters p = camera.getParameters();
camera.setParameters(Flash);
camera.startPreview();
I've seen people use an auto focus but i don't know if that would work.
I thought I would update this with some bullet prof code that works on almost all 4.0+ devices.
public void turnOn() {
camera = Camera.open();
try {
Parameters parameters = camera.getParameters();
parameters.setFlashMode(getFlashOnParameter());
camera.setParameters(parameters);
camera.setPreviewTexture(new SurfaceTexture(0));
camera.startPreview();
camera.autoFocus(this);
} catch (Exception e) {
// We are expecting this to happen on devices that don't support autofocus.
}
}
private String getFlashOnParameter() {
List<String> flashModes = camera.getParameters().getSupportedFlashModes();
if (flashModes.contains(FLASH_MODE_TORCH)) {
return FLASH_MODE_TORCH;
} else if (flashModes.contains(FLASH_MODE_ON)) {
return FLASH_MODE_ON;
} else if (flashModes.contains(FLASH_MODE_AUTO)) {
return FLASH_MODE_AUTO;
}
throw new RuntimeException();
}
The real key is setting that fake SurfaceTexture so that the preview will actually start. Turning it off is very easy as well
public void turnOff() {
try {
camera.stopPreview();
camera.release();
camera = null;
} catch (Exception e) {
// This will happen if the camera fails to turn on.
}
}
It seems like the developer of the Tiny Flashlight + LED app on the Android Market figured out how to make the flashlight work on LG Revolution.
Maybe you can contact him and ask?
You can also check the permissions he is using in his app to try to make your app work!
Good luck!
Test this :
if(camera == null){
camera = Camera.open();
parameters = camera.getParameters();
List<String> flashModes = parameters.getSupportedFlashModes();
if(flashModes != null && flashModes.contains(Parameters.FLASH_MODE_TORCH)){
//appareil supportant le mode torch
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
} else if (flashModes != null && flashModes.contains(Parameters.FLASH_MODE_ON)){
//spécial samsung
parameters.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(parameters);
camera.startPreview();
camera.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) { }
});
} else {
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
}
parameters.setFlashMode( Parameters.FLASH_MODE_OFF );
camera.setParameters(parameters);
camera.release();
camera = null;
} catch (RuntimeException e) {}
}//if
This worked well for LG Nexus:
camera = Camera.open();
camera.setPreviewTexture(new SurfaceTexture(0));
camera.setParameters(p);
camera.startPreview();
/*TESTED LG G4 */
public void flashOnOff(){
List<String> flashModes = parameter001.getSupportedFlashModes();
if(flashModes != null && flashModes.contains(Parameters.FLASH_MODE_TORCH)){
//appareil supportant le mode torch
parameter001.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(parameter001);
} else if (flashModes != null && flashModes.contains(Parameters.FLASH_MODE_ON)){
//spécial samsung
parameter001.setFlashMode(Parameters.FLASH_MODE_ON);
mCamera.setParameters(parameter001);
mCamera.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) { }
});
} else {
parameter001.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(parameter001);
}
if (!isFlashOn) {
if (mCamera == null || parameter001 == null) {
return;
}
parameter001 = mCamera.getParameters();
parameter001.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(parameter001);
try {
mCamera.setPreviewTexture(new SurfaceTexture(0));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mCamera.startPreview();
isFlashOn = true;
// changing button/switch image
}else if (isFlashOn) {
if (mCamera == null || parameter001 == null) {
return;
}
parameter001 = mCamera.getParameters();
parameter001.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(parameter001);
mCamera.stopPreview();
isFlashOn = false;
}
}

Categories

Resources