user3396698
user3396698

Reputation: 43

How to take pictures without saving / displaying image

I'm working with eclipse to develop a custom camera app for android. I've figured out how to display the preview of the camera, but I can't figure out how to take a picture without saving the image. When a picture is taken, I don't want to save the image or display it to the screen, I simply want it saved to a data type that I can use within the app and overwrite when another picture is taken.

Edit:

public class MainActivity extends Activity {

private Camera mCamera;
private CameraPreview mCameraPreview;
private Bitmap picture;
private static Uri mImageCaptureUri;
private static Bitmap photo = null;
private byte[] image;
private int PICK_FROM_CAMERA = 1;

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

@Override
protected void onCreate(Bundle savedInstanceState) {

    releaseCameraAndPreview();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mCamera = getCameraInstance();
    mCameraPreview = new CameraPreview(this, mCamera);

    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);

    preview.addView(mCameraPreview);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (resultCode != Activity.RESULT_OK) return;

    else{

        Bundle extras = data.getExtras();

        if (extras != null) {                        
            photo = extras.getParcelable("data");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.PNG, 100, bos); 
            image = bos.toByteArray();
        }

        File f = new File(mImageCaptureUri.getPath());            

        if (f.exists()){
            f.delete();
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

//creates camera class for functions
private Camera getCameraInstance() {

    Camera camera = null;

    try {
        camera = Camera.open();
    } catch (Exception e) {
        Log.v("Schweigen", "camera will not open");
    }
    return camera;
}

//releases camera on completion
private void releaseCameraAndPreview() {
    if (mCamera != null) {
        mCamera.release();
        mCamera = null;
    }
}

ShutterCallback shutterCallback = new ShutterCallback() {
    public void onShutter() {
        Log.v("Schweigen", "onShutter");
    }
};

/** Handles data for raw picture */
 PictureCallback rawCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.v("Schweigen", "onPictureTaken");
    }
};

/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
    //picture = BitmapFactory.decodeByteArray(data , 0, data.length);

    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,   mImageCaptureUri);    

        try {
            intent.putExtra("return-data", true);
            startActivityForResult(intent, PICK_FROM_CAMERA);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }
};

}

Upvotes: 4

Views: 4897

Answers (2)

Luis Lavieri
Luis Lavieri

Reputation: 4129

Make a Uri from the picture.

Intent cameraIntent = new    Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        Uri uriSavedImage=Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString()+"/ImagesFolder/image.jpg"));
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

Edit: After that you may store it in a byte[].

As I told you, you have to get the the Intent from the Camera and set it to a Uri:

private static Uri mImageCaptureUri;

private static Bitmap photo = null;

private byte[] image;

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                                                               "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

try {
    intent.putExtra("return-data", true);
    startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
    e.printStackTrace();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (resultCode != Activity.RESULT_OK) return;

    else{

                Bundle extras = data.getExtras();

                if (extras != null) {                        
                    photo = extras.getParcelable("data");
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.PNG, 100, bos); 
                    image = bos.toByteArray();
                }

                File f = new File(mImageCaptureUri.getPath());            

                if (f.exists()) f.delete();

            }


  }

Upvotes: 3

Gabe Sechan
Gabe Sechan

Reputation: 93688

The API to directly take a picture is here.

Upvotes: 1

Related Questions