Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Android] Retrieve pixel array after an image is taken

  1. Jul 27, 2012 #1
    I am working on an app that needs to access an array of pixels from a picture after it is taken. The main Activity is below. I have a good amount of java experience but extremely limited experience with images past displaying them on the screen. I see the byte array being passed to the picture callback method but I do not know how it is formatted. How can I get a pixel array that includes the RGB components from the captured image?

    Code (Text):

    public class ConverterActivity extends Activity
        private Camera mCamera;
        private CameraPreview mPreview;
    private PictureCallback mPicture = new PictureCallback() {
    private String TAG;
    public void onPictureTaken(byte[] data, Camera camera) {
    File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
    if (pictureFile == null){
    // Log.d(TAG, "Error creating media file, check storage permissions: " +
    //  e.getMessage());
    try {
    FileOutputStream fos = new FileOutputStream(pictureFile);
    } catch (FileNotFoundException e) {
    Log.d(TAG, "File not found: " + e.getMessage());
    } catch (IOException e) {
    Log.d(TAG, "Error accessing file: " + e.getMessage());

        public void onCreate(Bundle savedInstanceState)

    // Add a listener to the Capture button
    Button captureButton = (Button) findViewById(R.id.button_capture);
    new View.OnClickListener() {
    public void onClick(View v) {
    // get an image from the camera
    mCamera.takePicture(null, null, mPicture);
            // Create an instance of Camera
            mCamera = Camera.open(this.getBackCamera());
            // Create our Preview view and set it as the content of our activity.
            mPreview = new CameraPreview(this, mCamera);
            FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        protected void onPause()
            releaseCamera();              // release the camera immediately on pause event
        private void releaseCamera(){
            if (mCamera != null){
                mCamera.release();        // release the camera for other applications
                mCamera = null;
    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;
    /** Create a file Uri for saving an image or video */
    private static Uri getOutputMediaFileUri(int type){
    return Uri.fromFile(getOutputMediaFile(type));
    /** Create a File for saving an image or video */
    private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
    Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.
    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
    if (! mediaStorageDir.mkdirs()){
    Log.d("MyCameraApp", "failed to create directory");
    return null;
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
    mediaFile = new File(mediaStorageDir.getPath() + File.separator +
    "IMG_"+ timeStamp + ".jpg");
    } else if(type == MEDIA_TYPE_VIDEO) {
    mediaFile = new File(mediaStorageDir.getPath() + File.separator +
    "VID_"+ timeStamp + ".mp4");
    } else {
    return null;
    return mediaFile;
    public int getBackCamera()
    int numCameras = Camera.getNumberOfCameras();
    CameraInfo cInfo = new CameraInfo();
    for (int i = 0; i < numCameras; i++)
    Camera.getCameraInfo(i, cInfo);
    if (cInfo.facing == CameraInfo.CAMERA_FACING_BACK)
    return i;
    return -1;
  2. jcsd
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted