Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take a photo automatically without user interaction

I used this code to capture an image from the camera.

package android.takeowneship;


import java.io.File;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ImageView;
import android.widget.Toast;


public class camera extends Activity{


    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

    Uri imageUri;
    private ImageView imageView;

     @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

          //define the file-name to save photo taken by Camera activity
            String fileName = "new-photo-name.jpg";
            //create parameters for Intent with filename
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, fileName);
            values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
            //imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
            imageUri = getContentResolver().insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            //create new Intent
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

        }

     protected void onActivityResult(int requestCode, int resultCode, Intent data) {

         if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
             if (resultCode == RESULT_OK) {
                 //use imageUri here to access the image
                 Toast.makeText(this, "picture has been taken"+ imageUri, Toast.LENGTH_SHORT).show();


             } else if (resultCode == RESULT_CANCELED) {
                 Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
             } else {
                 Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
             }
         }

         }

     public static File convertImageUriToFile (Uri imageUri, Activity activity)  {

         Cursor cursor = null;
         try {
             String [] proj={MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION};
             cursor = activity.managedQuery( imageUri,
                     proj, // Which columns to return
                     null,       // WHERE clause; which rows to return (all rows)
                     null,       // WHERE clause selection arguments (none)
                     null); // Order-by clause (ascending by name)
             int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
//           int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);
             if (cursor.moveToFirst()) {
//               String orientation =  cursor.getString(orientation_ColumnIndex);
                 return new File(cursor.getString(file_ColumnIndex));
             }
             return null;
         } finally {
             if (cursor != null) {
                 cursor.close();
             }
         }
         }

}

But in this code it opens the camera and user has to click the button to take the photo. What I want is to take the photo automatically (without a preview) and saves it in the memory card.

like image 504
9T9 Avatar asked Mar 17 '12 18:03

9T9


2 Answers

Android does not allow you to take picture without showing the preview window. So you have to make the surface view very small. Like 1*1 pixel and put it in a corner of any control.

Or show a dummy surface view to do this.

SurfaceView view = new SurfaceView(this);
c.setPreviewDisplay(view.getHolder());
c.startPreview();
c.takePicture(shutterCallback, rawPictureCallback, jpegPictureCallback);

Check (broken link removed), this and this out.

like image 165
san Avatar answered Sep 20 '22 23:09

san


This code is capturing an image using the front camera and save it automatically

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    int index = getFrontCameraId();
    if (index == -1){
        Toast.makeText(getApplicationContext(), "No front camera", Toast.LENGTH_LONG).show();
    }
    else
    {
         iv_image = (ImageView) findViewById(R.id.imageView);   
         sv = (SurfaceView) findViewById(R.id.surfaceView);  
         sHolder = sv.getHolder();  
         sHolder.addCallback(this);  
         sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
    }
}   

@Override  
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)  
{  
     parameters = mCamera.getParameters();
     mCamera.setParameters(parameters);  
     mCamera.startPreview();  

     Camera.PictureCallback mCall = new Camera.PictureCallback()  
     {  
         @Override  
         public void onPictureTaken(byte[] data, Camera camera)  
         {          
            Uri uriTarget = getContentResolver().insert//(Media.EXTERNAL_CONTENT_URI, image);
            (Media.EXTERNAL_CONTENT_URI, new ContentValues());

             OutputStream imageFileOS;
             try {
                 imageFileOS = getContentResolver().openOutputStream(uriTarget);
                 imageFileOS.write(data);
                 imageFileOS.flush();
                 imageFileOS.close();

                 Toast.makeText(TakePictureActivity.this,
                         "Image saved: " + uriTarget.toString(), Toast.LENGTH_LONG).show();
             }
             catch (FileNotFoundException e) {
                 e.printStackTrace();
             }catch (IOException e) {
                 e.printStackTrace();
             }
             //mCamera.startPreview();

             bmp = BitmapFactory.decodeByteArray(data, 0, data.length);  
             iv_image.setImageBitmap(bmp);  
         }  
     };  

     mCamera.takePicture(null, null, mCall);
}  

int getFrontCameraId() {
    CameraInfo ci = new CameraInfo();
    for (int i = 0 ; i < Camera.getNumberOfCameras(); i++) {
        Camera.getCameraInfo(i, ci);
        if (ci.facing == CameraInfo.CAMERA_FACING_FRONT) return i;
        }
return -1; // No front-facing camera found
}

@Override  
public void surfaceCreated(SurfaceHolder holder)  
 {
    int index = getFrontCameraId();
    if (index == -1){
        Toast.makeText(getApplicationContext(), "No front camera", Toast.LENGTH_LONG).show();
    }
    else
    {
        mCamera = Camera.open(index);
        Toast.makeText(getApplicationContext(), "With front camera", Toast.LENGTH_LONG).show();
    }
      mCamera = Camera.open(index);  
      try {  
         mCamera.setPreviewDisplay(holder);  

      } catch (IOException exception) {  
          mCamera.release();  
          mCamera = null;  
      }  

 }

@Override  
public void surfaceDestroyed(SurfaceHolder holder)  
{  
    mCamera.stopPreview();  
    mCamera.release();  
    mCamera = null;  
}

}

like image 31
shankz Avatar answered Sep 19 '22 23:09

shankz