android device overrides flash mode when takePicture is called - android

I have a strange problem with android phone iGet Blackview BV6000s. I need to set flash mode into torch mode during preview and actual photographing. Unfortunately it looks like, something is overriding my code and when takePicture is called, flash goes off and than behaves like regular flash.
Could it be something in that "custom" android, that device has preinstalled?
I made a minimum example beside my actual app and it behaves same.
preview setup:
#Override
protected void onResume() {
super.onResume();
int numCams = Camera.getNumberOfCameras();
if(numCams > 0){
try{
camera = Camera.open(findBackFacingCamera());
Camera.Parameters parameters = camera.getParameters();
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
camera.setDisplayOrientation(90);
camera.setParameters(parameters);
camera.startPreview();
preview.setCamera(camera);
} catch (RuntimeException ex){
Toast.makeText(ctx, getString(R.string.camera_not_found), Toast.LENGTH_LONG).show();
}
}
}
calling takePicture:
preview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
});

Related

onAutoFocus not called in android camera

I have set the focus mode as FOCUS_MODE_AUTO.
camParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
cameraInstance.setParameters(camParameters);
Then I have started the preview.
cameraInstance.startPreview();
After that I am calling the autofocus method.
List<String> focusModes = camParameters.getSupportedFocusModes();
if(focusModes != null && focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
Log.d(TAG, "Phone supports autofocus!");
cameraInstance.autoFocus(autoFocusCallback);
}
Here is my autofocus callback.
private static android.hardware.Camera.AutoFocusCallback autoFocusCallback = new android.hardware.Camera.AutoFocusCallback() {
#Override
public void onAutoFocus (boolean success,
Camera camera) {
if(success) {
Log.d(TAG, "Sharp");
camera.cancelAutoFocus();
} else {
Log.d(TAG, "Not Sharp");
camera.autoFocus(autoFocusCallback);
}
}
};
I found that the onAutoFocus method is not called after I see the camera preview and noted(in logcat) that the following error is continuously spawning.
01-01 08:29:07.135 765-10897/? E/mm-camera: 5052: af_port_handle_set_focus_manual_pos_evt: not in CAM_FOCUS_MODE_OFF(Manual) mode, ignore the settings
01-01 08:29:07.316 765-10891/? E/mm-camera: 739: af_process_update_fv_history: SW Stats missing!Start Waiting
You can try of course, after preview has started. Just call autofocus inside the surfaceCreated and it will do the job when the FOCUS change state
Camera mCamera;
SurfaceView mPreview;
public void surfaceCreated(SurfaceHolder arg0)
{
Camera.Parameters params=mCamera.getParameters();
params.setFocusMode (params.FOCUS_MODE_CONTINUOUS_PICTURE);
mCamera.setParameters(params);
try
{
mCamera.setPreviewDisplay(mPreview.getHolder());
mCamera.setPreviewCallback(this);
mCamera.startPreview();
mCamera.autoFocus(new AutoFocusCallback()
{
public void onAutoFocus(boolean focussed,Camera camera)
{
if(focussed)
Log.d(TAG, "Sharp");
else
Log.d(TAG, "Not Sharp");
}
});
}catch(Exception e){}

How to enable front camera instead of rear camera for android programmatically?

I'm using an old phone it's rear camera is damaged so I want to set my default camera as my front camera. Is it possible to do that everytime when I open camera app it open front camera always?
Try this!
Camera.open(android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);//avoid passing hardcode values
Camera mCamera;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCamera= Camera.open(1);
}
Note:
0 for CAMERA_FACING_BACK
1 for CAMERA_FACING_FRONT
This might helpful. Source URL : this
Button otherCamera = (Button) findViewById(R.id.OtherCamera);
otherCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (inPreview) {
camera.stopPreview();
}
//NB: if you don't release the current camera before switching, you app will crash
camera.release();
//swap the id of the camera to be used
if(currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK){
currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
}
else {
currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
}
camera = Camera.open(currentCameraId);
try {
camera.setPreviewDisplay(previewHolder);
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}

Method Camera.takePicture() crash on Nexus 5, but Xperia mini is OK

At first please excuse my bad English.
I have problem with programmatically taking photo. I wrote an app, that makes collection of photos based on countdown timer and after that, photos are being processed using c++ code.
I'm using dummy SurfaceView, because I don't need preview in UI. The code below is working on my phone Xperia mini - API 15 (so permissions and code would be correct), but I borrowed school Nexus 5 - API 21 and there is problem with preview.
takePicture: camera 0: Cannot take picture without preview enabled
I found a solution, which uses setPreviewTexture (commented below) instead of setPreviewDisplay. It working for the first photo, which is normally saved, but I get the same error after the second call of takePicture().
Thanks for every advice, LS
Camera camera;
#Override
protected void onResume() {
super.onResume();
// is camera on device?
if(!checkCameraHardware()) return;
releaseCamera();
try {
camera.stopPreview();
} catch (Exception e){
Log.d(TAG, "No preview before.");
}
SurfaceView dummy = new SurfaceView(this);
camera = Camera.open();
Camera.Parameters params = camera.getParameters();
params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
camera.setParameters(params);
try {
//camera.setPreviewTexture(new SurfaceTexture(10));
camera.setPreviewDisplay(dummy.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}
SOLUTION:
I needed to refresh preview. The code below is working on Xperie and Nexus too.
Question remains why I have to use setPreviewTexture, because setPreviewDisplay always returns error on Nexus.
camera.takePicture(null, null, new PictureCallback() {
#Override
public void onPictureTaken(final byte[] data, Camera camera) {
// save picture
refreshPreview();
}
});
public void refreshPreview() {
try {
camera.stopPreview();
} catch (Exception e) {}
try {
camera.startPreview();
} catch (Exception e) {}
}
and in function onResume()
try {
camera.setPreviewTexture(new SurfaceTexture(10));
} catch (IOException e) {}
Just add a callback for starting preview on your camera instance. The thing is that after starting preview on camera instance, it needs some time to be able take a picture. Try this:
camera.startPreview();
camera.setOneShotPreviewCallback(new Camera.PreviewCallback() {
#Override
public void onPreviewFrame(byte[] data, Camera camera) {
camera.takePicture(null, null, new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
// do something you want with your picture and stop preview
camera.stopPreview();
}
});
Once the picture is taken, refresh you're surfaceview & stop the preview and releasee camera and restart the process again.
try {
camera.takePicture(null, null, new PictureCallback() {
public void onPictureTaken(final byte[] data, Camera camera) {
//once ur logic done
refreshCamera();
}
});
} catch (Exception e2) {
// Toast.makeText(getApplicationContext(), "Picture not taken", Toast.LENGTH_SHORT).show();
e2.printStackTrace();
}
public void refreshCamera() {
if (dummy.getHolder().getSurface() == null) {
return;
}
try {
camera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
try {
camera.setPreviewDisplay(dummy.getHolder());
camera.startPreview();
} catch (Exception e) {
}
}
Hope this solution may help you.

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

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

Categories

Resources