How to turn ON and OFF Device flash light? - android

I want to turn on only camera flash light (not with camera preview) programmatically in Android. I googled for it but the help I found was not working on my Samsung Galaxy ACE.
By using this code able to turn on for few seconds after that it goes to off automatically with out doing anything on the UI. If I try to turn on again getting force close.
private void turnOn() {
clicked = true;
mCamera = Camera.open();
Parameters params = mCamera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
mCamera.setParameters(params);
mCamera.startPreview();
mCamera.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
}
});
}
For turn OFF
private void turnOff() {
clicked = false;
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
}
}
Declared these manifest permissions
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />
Can any body tell me How to turn on the flash light for a long time upto the use click on OFF button. Is it requires any third party libraries. I saw this app in the Google play Tiny flash how they did this app..
From my after turn on it goes to off in 2 sec I did not get what's the problem in this.

Try this
//Turn on
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
//Turn off
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();

#RajaReddy PolamReddy,
I am using Samsung S3 LTE (4G). All you need change is to change Parameters.FLASH_MODE_ON to Parameters.FLASH_MODE_TORCH.
It works for me.

Bad news:
According to documentation android camera needs intialized surface view to start preview
Good news:
it must not be full screen or visible
My expirience so far that starting preview is necessary to activate flash ( but I can not speak for all devices )
If you need some examples how to activate preview and possibly hide it behind overlay,
look into javaocr library (see demos - there are 2 android apps, and also camera management library in separate project - you can just grab it)
https://sourceforge.net/projects/javaocr/

Related

Front Flash light in Android

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.

NFC unavailable when camera is open

This is my Torch app:
final Camera.Parameters p;
Camera camera=Camera.open();
camera.setPreviewTexture(new SurfaceTexture(0));
p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
When my app is running, some devices cannot detect NFC tags. I noticed this happens with the Nexus 5X, specifically.
It appears certain devices can't detect NFC when the camera is running.
Can this problem be solved programmatically?
Unfortunately, no, but I sincerely hope Nexus 5X is the only device on which you will ever encounter this.
The reason is that pretty late in the development cycle of the 5X, it was found that the NFC controller polling introduced noise in the camera sensor. The only feasible fix at that time was to turn off NFC when the camera is opened :(
To solve this problem you can add this little code to your onStop, in the activity that uses the camera. If you need NFC, in some devices you need to release the camera.
#Override
protected void onStop() {
super.onStop();
try
{
android.hardware.Camera mCamera = android.hardware.Camera.open();
mCamera.release();
mCamera = null;
}
catch(RuntimeException e)
{
Log.e(TAG, "init_camera: " + e);
return;
}
}

How to turn off Flash light of Camera through intents in Android Programatically?

How to turn off Flash light of Camera through intents in Android Programatically?
I use the below code for activating Camera application and then taking the picture and get back to my application.
String packageName = takePictureIntent.resolveActivity(packageManager).getPackageName();
if (mPhotoFileUri != null)
getActivity().grantUriPermission(packageName, mPhotoFileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoFileUri);
startActivityForResult(takePictureIntent, TAKE_PICTURE);
I want to turn off Camera flash when its open by default.How its possible?
Suggestions will be appreciated:-)
to turn on/off the phone camera led or flashlight in Android, you can use following :
Camera camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
And, not forgot to put following permission on AndroidManifest.xml.
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
Edit-1:
Unfortunately, when using the camera with Intent, the only extra parameter you can set is
MediaStore.EXTRA_OUTPUT
I'm guess you can't change via Intent. Reference from this question
For this you should do like :
1) Check whether flash light is available or not ?
2) If yes then Turn Off/On
3) If no then you can do whatever according to your app. needs
For Checking availability of flash in device:
You can use the following
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
which will return true if a flash is available, false if not.
See link for more information.
For turning on/off flashlight :
I googled out and got this about android.permission.FLASHLIGHT. Android manifests' permission looks promising:
<!-- Allows access to the flashlight -->
<permission android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal"
android:label="#string/permlab_flashlight"
android:description="#string/permdesc_flashlight" />
Then make use of Camera and set Camera.Parameters. The main parameter used here is FLASH_MODE_TORCH.
eg.
Code Snippet to turn on camera flash light.
Camera cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
Code snippet to turn off camera led light.
cam.stopPreview();
cam.release();
Issues :
There are also some problems while turning On/Off flashlight. eg. for the devices not having FLASH_MODE_TORCH or even if it has, then flashlight doesnot turn ON etc.
Typically Samsung creates alot of problems.
You can refer about problems in the given below list:
1) Use camera flashlight in Android
2) Turn ON/OFF Camera LED/flash light in Samsung Galaxy Ace 2.2.1 & Galaxy Tab
REFERENCE LINK :
How to turn on camera flash light programmatically in Android?

Turn ON/OFF Camera LED/flash light in Samsung Galaxy Ace 2.2.1 & Galaxy Tab

I am turning ON Camera LED light using FLASH_MODE_ON.
Samsung Galaxy Ace have only three flash modes : on, off and auto.
FLASH_MODE_TORCH not working in Samsung Galaxy Tab & Samsung Galaxy Ace 2.2.1
Here is my code how i am turning ON my Camera LED
Camera cam;
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) {
}
});
And turning it off by using :
cam.stopPreview();
cam.release();
Code Reference : Use camera flashlight in Android
But the problem is LED Light remains on just for 5sec. It just then turns OFF automatically.
Can anyone please tell where can be the problem. OR any way to turn ON the LED light continuously till its requested to Stop.
I will soon released a new version of my app to support to galaxy ace.
You can download here: https://play.google.com/store/apps/details?id=droid.pr.coolflashlightfree
In order to solve your problem you should do this:
this._camera = Camera.open();
this._camera.startPreview();
this._camera.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
}
});
Parameters params = this._camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
this._camera.setParameters(params);
params = this._camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
this._camera.setParameters(params);
don't worry about FLASH_MODE_OFF because this will keep the light on, strange but it's true
to turn off the led just release the camera

How to use Front Facing Camera on Samsung Galaxy S

I've tried several answer I've found across the web, such as:
Camera.Parameters parameters = mCamera.getParameters();
parameters.set("camera-id", 2);
mCamera.setParameters(parameters);
or
mMediaRecorder.setVideoSource(2);
But it doesn't work. I've also set permissions on the manifest file:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
Am i missing out on something? I've searched StackOverflow and I know this has been asked before but there seem to be no confirmed solution on this, any kind of help would be appreciated.
Note: I'm using Galaxy S on the 2.1 platform
Anyway after a few trials and error, I figured it out how to do it:
Camera.Parameters parameters = mCamera.getParameters();
parameters.set("camera-id", 2);
parameters.setPreviewSize(640, 480); // or (800,480) this is supported front camera preview size # Samsung Galaxy S
mCamera.setParameters(parameters);
Or, if you need to use it with MediaRecorder:
MediaRecorder mMediaRecorder = new MediaRecorder();
Camera mCamera = Camere.open();
Camera.Parameters parameters = mCamera.getParameters();
parameters.set("camera-id", 2);
parameters.setPreviewSize(640, 480); // or (800,480)
mCamera.setParameters(parameters);
mCamera.unlock(); // unlock, to give other process to access it otherwise it can't be used later
mMediaRecorder.setCamera(mCamera);
// continue with mMediaRecorder standard routines here
If you need to have a smaller preview size, you could set/scale down your SurfaceView size instead.
There currently isn't a standard API for front-facing cameras; you will need to rely on whatever (if any) documentation the hardware manufacturer has for using their extensions to access the front-facing camera. This will of course only work on those specific devices.
Note writing raw strings like "camera-id" is often a good sign you have wandered into the woods outside of the official SDK. :)
I think this is as good a place as any to add some details I've worked out.
In case you're using the front facing camera in portrait mode and the resulting file comes out cut up into squares with green blocks thrown in try inverting the width and height (both preview and recorder) and setting the encoder to H263...
This solved the problem on my Samsung Galaxy S on 2.3.3+...

Categories

Resources