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;
}
}
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 have some troubles with displaying camera viewfinder on Galaxy S2 (android 2.3) and LG P500 (android 2.3). I have some noise (interference) on SurfaceView. Nevertheless, result photo hasn't such defects.
Other devices (include devices with 2.3 and Galaxy S2 with android 4) working fine.
Unfortunatly, I haven't such devices, so I can't test it after every changing in source code. Maybe someone already had this problem?
Also, I tried to test this app using Samsung Remote Test Lab, but, unfortunatly, Galaxy S2 with android 2.3 was placed on the table and his camera takes blackness (splash do nothing). In this case there are no any white strips.
onCreate():
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mSurfaceHolder.addCallback(this);
...
mSurfaceView.setLayoutParams(lp);
onResume():
mCamera = Camera.open();
if (mCamera == null) {
Toast.makeText(this, R.string.t_no_camera, Toast.LENGTH_SHORT).show();
finish();
} else {
mCamera.setDisplayOrientation(90);
Parameters p = mCamera.getParameters();
p.setFocusMode(Parameters.FOCUS_MODE_AUTO);
prepareFlash();
if (prepareResolution()) {
Camera.Size size = getBestPreviewSize(mDisplay.getWidth(), mDisplay.getHeight(), p);
if (size != null) {
p.setPreviewSize(size.width, size.height);
}
}
mCamera.setParameters(p);
try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
surfaceCreated(SurfaceHolder holder):
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
Any idea? What could it be? I will be grateful for any hint.
You start camera preview in Portrait mode; bugs with such preview occur on different devices. Sometimes, these bugs get fixed with software upgrade from the manufacturer, sometimes, they are not. Sometimes, crazy workarounds can resolve the problem.
To be on the safe side, stick with Landscape mode. Maybe your app can keep a black list of devices/build versions where the preview activity will not allow vertical (portrait) orientation.
Note that you can usually simulate portrait orientation by using rotated icons, bitmaps, and even text labels.
I searched on the web to turn on the flash using this mode but no solution work so far... I want to use this mode because some devices doesn't support the FLASH_MODE_TORCH, so I want to force the focus or another thing to turn on the flash continuously. I tried to restart the "autofocus" when the flash turns off automatically but it takes too much time (the period between two "flashing" is quite long).
So I ask you if you know some way to do it, thanks ! :)
Actually I use :
public static Camera Cam;
public static Parameters CamParams;
Cam = Camera.open();
CamParams = Cam.getParameters();
CamParams.setFlashMod(Parameters.FLASH_MODE_ON);
Cam.setParameters(CamParams);
Cam.startPreview();
Cam.autoFocus(new AutoFocusCallback()
{
public void onAutoFocus(boolean success, Camera camera)
{}
});
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/
I have a small app I have been working on that uses the front camera. The way I have been obtaining use of the front camera seems to work on most phones, but users have been reporting trouble on the S3 and various other new devices. The way I have been accessing the front camera is like so:
// Find the ID of the front camera
CameraInfo cameraInfo = new CameraInfo();
for(int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if(cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
defaultCameraId = i;
mCameraFound = true;
}
}
if(!mCameraFound)
displayDialog(8);
From some of the error reporting I've added into the app, I've noticed the S3 actually finds the front camera, but users report it only shows a blank screen? I have only been able to test on the devices I have (GNex and N7). I was hoping someone here may have some experience with this or may be able to help me solve this issue. If you want to try the app out on your S3, check the link below. Thanks in advance.
https://play.google.com/store/apps/details?id=com.wckd_dev.mirror
EDIT: I created a MirrorView object which contains a TextureView used for the preview. The MirrorView object implements a SurfaceTextureListener. Within the onSurfaceTextureAvailable() method is where the preview is started. I also created a method for restarting the preview after the app has gone from hidden back to visible.
So this is called when the app is first started:
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
try {
if (mCamera != null) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mPreviewSize.height, mPreviewSize.width);
requestLayout();
mCamera.setParameters(parameters);
mCamera.setPreviewTexture(surface);
mCamera.startPreview();
}
}
catch(RuntimeException e) {
// Log.e(TAG, "RuntimeException caused by setPreviewTexture()", exception);
}
catch (IOException e) {
// Log.e(TAG, "IOException caused by setPreviewTexture()", exception);
}
}
The restartPreview call is to an identical (but separate) method. From some of the debug data I've been collecting through users, I've noticed that the app finds two camera on the S III and selects the id matching CAMERA_FACING_FRONT. Also, this issue doesn't seem to be happening on all S III. I have users who have feedback reporting as much. The latest report from a user experiencing this issue was an AT&T S III user. Any help would be appreciated!
Got some face time with an S3 tonight that was experiencing this issue with my app. Here what was going on. The TextureView relies on 2d hardware acceleration which is supposed to on by default (from what I understood) on 4.0+ devices. It wasn't turning on (for my app at least) on his phone. The fix was as simple as adding a single line in the manifest (under application).
android:hardwareAcceleration = "true"