Metropolis
Metropolis

Reputation: 6622

Is there a way to call a class function from another class directly without an instance of it in Java?

I have an application that I have taking a picture, and then it is supposed to send the data from the picture to another activity using an intent.

I am trying to call the intent inside the jpegCallback, but the problem is I also need to release the camera through the preview class before calling the intent. However, I can not get to the original preview object from inside the callback, so I need a way to call MainActivity.doPictureResults() from inside the callback. Or I need a way to have a listener that fires after all of the picture callbacks are done.

Here is my MainActivity class which holds an instance of Preview class in the mPreview variable. The jpegCallback is at the bottom, and I want to call the doPictureResults from inside that, or setup another callback for after that function is done.

public class MainActivity extends Activity {

    private final String TAG = "MainActivity";
    private Preview mPreview;
    Camera mCamera;
    int numberOfCameras;
    int cameraCurrentlyLocked;

    //The first rear facing camera
    int defaultCameraId;

    /**
     * Constructor
     * @param savedInstanceState 
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {Log.e(TAG, "onCreate");
        super.onCreate(savedInstanceState);

        //Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //Create a RelativeLayout container that will hold a SurfaceView,
        //and set it as the content of our activity.
        this.mPreview = new Preview(this);
        setContentView(this.mPreview);

        //Find the total number of cameras available
        this.numberOfCameras = Camera.getNumberOfCameras();

        //Find the ID of the default camera
        CameraInfo cameraInfo = new CameraInfo();
        for(int i = 0; i < this.numberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if(cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
                this.defaultCameraId = i;
            }
        }
    }

    @Override
    protected void onResume() {Log.e(TAG, "onResume");
        super.onResume();

        //Open the default i.e. the first rear facing camera.
        this.mCamera = Camera.open();
        this.cameraCurrentlyLocked = this.defaultCameraId;
        this.mPreview.setCamera(mCamera);
    }

    @Override
    protected void onPause() {Log.e(TAG, "onPause");
        super.onPause();

        //Because the Camera object is a shared resource, it's very
        //Important to release it when the activity is paused.
        this.mPreview.releaseCamera();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Inflate our menu which can gather user input for switching camera
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.camera_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //Handle item selection
        switch (item.getItemId()) {
            case R.id.switchCam:
                //Check for availability of multiple cameras
                if(this.numberOfCameras == 1) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage(this.getString(R.string.camera_alert)).setNeutralButton("Close", null);
                    AlertDialog alert = builder.create();
                    alert.show();
                    return true;
                }

                //OK, we have multiple cameras.
                //Release this camera -> cameraCurrentlyLocked
                this.mPreview.releaseCamera();

                //Acquire the next camera and request Preview to reconfigure parameters.
                this.mCamera = Camera.open((this.cameraCurrentlyLocked + 1) % this.numberOfCameras);
                this.cameraCurrentlyLocked = (this.cameraCurrentlyLocked + 1) % this.numberOfCameras;
                this.mPreview.switchCamera(mCamera);

                //Start the preview
                this.mCamera.startPreview();
                return true;

            case R.id.takePicture:
                this.mCamera.takePicture(null, null, jpegCallback);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void doPictureResults(byte[] data) {
        this.mPreview.releaseCamera();

        //Release the camera and send the results of the image to the GetResults view
        Intent resultsIntent = new Intent(MainActivity.this, ImageProcessorActivity.class);
        resultsIntent.putExtra("image_data", data);
        startActivity(resultsIntent);
    }

    /**
     * Handles data for jpeg picture when the picture is taken
     */
    PictureCallback jpegCallback = new PictureCallback() { 
        public void onPictureTaken(byte[] data, Camera mCamera) {Log.e(TAG, "jpegCallback");
            String baseExternalDir = Environment.getExternalStorageDirectory().getAbsolutePath();
            String fileName = String.format("Assist/%d.jpg", System.currentTimeMillis());

            FileOutputStream outStream = null;
            try {
                //Create the directory if needed
                File assistDirectory = new File(baseExternalDir + File.separator + "Assist");
                assistDirectory.mkdirs();

                // Write to SD Card
                outStream = new FileOutputStream(baseExternalDir + File.separator + fileName);
                outStream.write(data);
                outStream.close();
            } 
            catch (FileNotFoundException e) { 
                Log.e(TAG, "IOException caused by PictureCallback()", e);
            } 
            catch (IOException e) {
                Log.e(TAG, "IOException caused by PictureCallback()", e);
            } 

            //This is the type of thing I WANT to do....but its not possible.
            MainActivity.doPictureResults();
        }
    };
}

Upvotes: 1

Views: 200

Answers (2)

Dave Newton
Dave Newton

Reputation: 160321

One options would be to create a PictureCallback implementation that saved the information was required in doPictureResults. It's not clear if doPictureResults will be called anywhere else; if it's not, this is clean and isolates the functionality.

Another would be to have the activity itself implement PictureCallback so you have direct access to all the member variables without having to do any work at all. This allows doPictureResults to be called from other places.

public class MainActivity extends Activity implements PictureCallback {
    ...
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            ....
            case R.id.takePicture:
                this.mCamera.takePicture(null, null, this);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }
    ...
    public void onPictureTaken(byte[] data, Camera mCamera) {
        Log.d(TAG, "jpegCallback");
        String baseExternalDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        String fileName = String.format("%d.jpg", System.currentTimeMillis());
        ...
        doPictureResults();
    }
}

Upvotes: 1

Thomas
Thomas

Reputation: 5094

The only methods you can call on a class without an instance are static methods, but I don't know if that will do what you want here.

Upvotes: 0

Related Questions