4

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

}

Stephen
  • 9,899
  • 16
  • 90
  • 137
user3396698
  • 43
  • 1
  • 5

2 Answers2

3

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

            }


  }
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69
  • Using a byte[] is what I'm trying to do now, I just can't get it into a bitmap format or any other useful format ( what I'm trying: http://stackoverflow.com/questions/7620401/how-to-convert-byte-array-to-bitmap ) – user3396698 Mar 13 '14 at 04:10
  • I edited my answer to include your suggestions, in this case the image would be stored under the file name image ( I think ). I still don't understand how this is going to work out thou – user3396698 Mar 13 '14 at 07:21
  • Did you try to run it? Could you post your logcat – Luis Lavieri Mar 13 '14 at 14:04
  • I don't understand why this is relevant. Isn't URI the same as file path? It's still saving the image. – pete Aug 03 '20 at 02:33
1

The API to directly take a picture is here.

GraphicsMuncher
  • 4,583
  • 4
  • 35
  • 50
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127