2
votes

I'm writing an app using the OpenCV android library. I've created a CameraActivity which I want to call, and return an image.

public class CameraActivity extends Activity implements CvCameraViewListener2 {

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
        case LoaderCallbackInterface.SUCCESS: {
            Log.i(TAG, "OpenCV loaded successfully");
            mOpenCvCameraView.enableView();
        }
            break;
        default: {
            super.onManagerConnected(status);
        }
            break;
        }
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    ...

    Button snap = (Button) findViewById(R.id.snap);
    snap.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            processVideo();
        }
    });
    snap.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            processPicture();
            return true;
        }
    });
}

@Override
public void onPause() {
    ... 
}

@Override
public void onResume() {
    ...
}

public void onDestroy() {
    ...
}

public void onCameraViewStarted(int width, int height) {
    ...
}

@Override
public void onCameraViewStopped() {
    ...
}


public Mat onCameraFrame(CvCameraViewFrame cvf) {
    // Grab frame
    cvf.rgba().copyTo(frame);

    // Filter frame
    filterLaser();

    // Retrieve contour center point
    contourLaser();

    // Draw 
    draw();

    // Display the result.
    return cvf.rgba();
}

private void draw(){
    ...
}

private void filterLaser(){
    ...
}

void processVideo(){
    ...
}

void processPicture(){
    Intent result = new Intent();    

    if(frame != null)
    {
        // Convert the processed Mat to Bitmap
        Bitmap resultBitmap = Bitmap.createBitmap(frame.cols(),  frame.rows(),Bitmap.Config.ARGB_8888);;
        Utils.matToBitmap(frame, resultBitmap);

        setResult(Activity.RESULT_OK, result);
        result.putExtra("BITMAP", resultBitmap);
    } else {
        setResult(Activity.RESULT_CANCELED); // No frame found
    }

    Log.d("LastPoint", "FINISHED");
    finish();
}
};

This Activity gets called using the following code in MainActivity.

  Button reply = (Button) findViewById(R.id.buttonReply);
    reply.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext(), CameraActivity.class);
            startActivityForResult(i, CAMERAREQUEST);
        }
    });

And the result is extracted in MainActivity.

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("LastPoint", "WRITING");

    switch(requestCode) {
        case (CAMERAREQUEST) : 
            if (resultCode == Activity.RESULT_OK) {
                Bitmap bmp = (Bitmap) data.getParcelableExtra("Bitmap");

                writeBitmap(bmp);
            }
            break;
    }
}

"FINISHED" gets printed to logcat. "WRITING" isn't. Why can't I exit the CameraActivity? The view itself isn't closing either.

1
You should use result.putExtra("BITMAP", resultBitmap); before setResult(Activity.RESULT_OK, result);, not after.joao2fast4u
Thanks! But this is not solving my problem.Wouter
Did you try to put a Log inside if(frame != null) {} block, to make sure it is passing there?joao2fast4u
I did, and verified again. It is passing.Wouter

1 Answers

4
votes

The problem was the line:

    result.putExtra("BITMAP", resultBitmap);

Solved it by saving the picture to sdcard in my CameraActivity and passing the Uri through the result intent.