Capture Images With Intent without clicking the camera button again - android

I want to capture images through Intent
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)
but the problem is that with this code the camera is started and the user has to click on the camera button to capture the image , but what i want is that the camera sholud start and take picture without any furthur interaction with the user
I want to do this using INTENT

That is the way I did it :
Declare an instance of Camera, and SurfaceHolder.
Create an Object CallBackPicture, and implements the method on PictureTaken (method launched when you want to take a picture)
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Camera.PictureCallback mCall = new Camera.PictureCallback()
{
#Override
public void onPictureTaken(byte[] data, Camera camera)
{
//DO YOUR STUFF
}
};
// Open the instance of camera
mCamera = Camera.open();
try {
// Call the preview (not sure if it is working without this call
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.startPreview();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(mCamera == null) Log.i(TAG, "mCamera is null");
// Will call the onPictureTaken implemented above
// Look at the documentation : public final void takePicture
mCamera.takePicture(null, null, mCall);
mCamera.stopPreview();
mCamera.release();
By modifying this, you should be able to do what you want..
Don't forget to modify the Manifest.XML too, but I think you've already done it!
EDIT : sometimes there is some problem with stoppreview() and release()..
So, the thing i've done is :
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
mCamera = Camera.open();
etc...

This can't be done. There are only two options:
Invoking Camera app via Intent. The user then uses Camera app in normal way - i.e. presses the button when ready.
Use Camera class - this is much more work compared to running Camera app via Intent. But it gives you full control.

When you call the camera intent you basically "run" the camera app (or other app that registered on this intent), so basically, you can't control of how it works.
You can use the Camera API...take a look here

Related

How to keep the flashlight on (programmatically) while the camera preview is ON

So for my Android app, I have created a SurfaceView and assigned it as camera preview and start camera preview using the necessary API's. But now I want to turn the Flashlight ON (kind of act like a Torch) while the preview is working.
Please note that I have seen tons of examples online on how to turn flashlight on and it works as long as I don't call the open camera API. Below is the code -
try
{
CameraManager cameraManager = (CameraManager)context.GetSystemService(Context.CameraService);
if (cameraManager != null)
{
//for the sake of brevity, hardcoded the camera id. 0 is mostly back camera
cameraManager.SetTorchMode(0, true);
}
}
catch (CameraAccessException e)
{
LogUtil.Error("CameraInput", e.ToString());
}
Please note I am testing on Android N and hence the above code works flawlessly. But as soon as I call below line of code, the flash turns off.
Camera camera = Camera.Open(0);
// ...... some code ....//
camera.StartPreview();
When the above 2 lines execute, the flash goes off. Is this a know behaviour like where camera takes exclusive lock over flash hardware and resets it's value to default.
I tried reversing the above code i.e calling the Camera Open API being called first and then setting the flash. On that I get CameraAccessException , camera already in use.
What am i missing ?
Try this while previewing on SurfaceView
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(params);
Remember that If you want to use it as flashlight you can do:
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
If not, to turn flash on which will come out when you take the picture, you use:
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
Happy Coding!
Figured out how to torch on and off while camera is previewing. On button click to on/off the light, use this code:
//at some other function where camera is initialised and start preview
//...
Camera camera = Camera.open();
camera.startPreview();
//...
boolean lightOn = false;
//...
buttonLight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Camera.Parameters p = camera.getParameters();
if (!lightOn) {
lightOn = true;
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
} else {
lightOn = false;
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
}
}
});
Happy Coding! :D

Android Live Wallpaper crashes when tap on camera

I have a live wallpaper that just draws triangles, very simple, and runs smoothly all time. After testing found that when I open the camera to take pictures the phone freezes for about 10 seconds, then the camera opens and the wallpaper crashes and the message "Unfortunately, Live Wallpaper has stopped." appears. Also found that when I open any barcode scanner app the problem raises again because the scanner uses the camera too. Seems the problem raises when the camera app runs. Any ideas what's causing it?
hi #Bullet Camera is just open ones, if it open through any other apps then, you can can not access your camera, Solution is that, where you use camera please release that after no used.
main problem is that, in your app (app in that you are using camera) , so , you release camera after used.
like following :
#Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
}
when you need
private void releaseCameraAndPreview() {
if (camera != null) {
camera.release();
camera = null;
}
}
or
public void stopCamera() {
if (cameraDevice != null) {
cameraDevice.stopPreview();
cameraDevice.setPreviewCallback(null);
cameraDevice.release();
cameraDevice = null;
System.out.println("in to the stop video");
}
}

How to take two consecutive pictures from camera?

When I tried to take a single picture from camera, it works fine. And now I added little bit of modification on my code, and wanna take two consecutive picture from camera.
public void takePicture(final boolean isWithFlash) {
Camera.PictureCallback mCall = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
if(isWithFlash) {
savePhotoToDirectory(data, captureImageFileName);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
} else {
savePhotoToDirectory(data, captureImageFileName);
takePicture(true);
}
}
};
if(mCamera != null) {
if(!isWithFlash) {
Parameters param = mCamera.getParameters();
mCamera.takePicture(null, null, mCall);
} else {
Parameters param = mCamera.getParameters();
param.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(param);
mCamera.takePicture(null, null, mCall);
}
} else {
Log.d("MYLOG", "Camera is null");
}
}
What I'm trying to do is take a picture without flash, and then take an another picture with flash consecutively. However, when I tried my code, it only takes first photo, and second onPictureTaken() function is not being called.
What am I doing wrong here? Or is there any better way to take two consecutive pictures?
Any comments would be really appreciated!
You don't need to call mCamera.stopPreview() after second call. But you need to call mCamera.startPreview() after the first one. I would introduce some delay between two calls to takePicture(), e.g. bu using View.post() to take the second picture. But maybe this post is not necessary, and delay that it causes is too much for your purposes - that's for you to decide.

Android camera freezes after taking one photo

I'm doing one project with camera and after taking one photo camera freezes and u have to finish the activity and recall it again to take another photo, how can I take photo freeze for just 1-2 sec and then surface view to have the camera again. the same for video I am using media recorder, taking video press stop video saves and screen is still alive but I can not take video again I have to restart the activity?
Anybody have a solution?
I found a solution for this: After taking a picture, preview display will have stopped. To take more photos, call camera.startPreview() again first.
after capturing image you should stop the preview and start it back again.
mCamera.stopPreview();
mCamera.startPreview();
it would work fine.
Do any image processing in a background AsyncTask. This will allow your UI Activity to continue on and take another picture.
Edit: I cannot delete an accepted answer so please see stoefin's answer below. Calling camera.startPreview() before taking the next photo works for him.
The camera.startpreview(); answer didn't work for my case but the code below solved that problem for me and hope it helps others too.I used a thread to delay closing and opening of the camera after a photo is captured by 500ms
private void start_camera() {
try {
camera = Camera.open();
// camera.lock();
} catch (RuntimeException e) {
Log.e(tag, "init_camera: " + e);
return;
}
Camera.Parameters param = camera.getParameters();
param = camera.getParameters();
Camera.Size size = param.getSupportedPreviewSizes().get(0);
param.setPreviewSize(size.width, size.height);
camera.setParameters(param);
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewRunning = true;
} catch (Exception e) {
Log.e(tag, "init_camera: " + e);
return;
}}
private void captureImage() {
camera.takePicture(shutterCallback,null,jpegCallback);
Thread restart_preview=new Thread(){public void run(){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
camera.release();
camera=null;
start_camera();
}};
restart_preview.start();}
Instead of using the activities defined by the existing camera app on your phone, you can write your own Activity that uses the Camera API directly to accomplish the functionality you describe. The Camera class is documented here: http://developer.android.com/reference/android/hardware/Camera.html
The camera is freezing, because you are not restarting the preview of the camera, so restart it by calling camera.startpreview()

how to get series of pictures?

i have problems with this, recently i have also problem with just taking 1 photo...
do you know how to solve it? any working code?
i'm doing it like that:
mCamera = Camera.open();
Log.e(TAG, "onCreate");
mCamera.takePicture(null, mPictureCallback, mPictureCallback);
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c)
{
if (imageData != null) {
//process photo
}
}
};
in onDestroy() i release the camera
on simulator it works, on telephone - 1st time runs without problems but no picture, second and next times - 'force close' - looks like camera is not released ...

Categories

Resources