Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

again, just move your code out of onCreate(), you can only call opencv code after BaseLoaderCallback finished loading.:

public class PicActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pic);
}

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        if (status == LoaderCallbackInterface.SUCCESS ) {
            // now we can call opencv code !
            helloworld();
        } else {
            super.onManagerConnected(status);
        }
    }
};

@Override
public void onResume() {;
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5,this, mLoaderCallback);
    // you may be tempted, to do something here, but it's *async*, and may take some time,
    // so any opencv call here will lead to unresolved native errors.
}

public void helloworld() {
    // make a mat and draw something
    Mat m = Mat.zeros(100,400, CvType.CV_8UC3);
    Core.putText(m, "hi there ;)", new Point(30,80), Core.FONT_HERSHEY_SCRIPT_SIMPLEX, 2.2, new Scalar(200,200,0),2);

    // convert to bitmap:
    Bitmap bm = Bitmap.createBitmap(m.cols(), m.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(m, bm);
    // find the imageview and draw it!
    ImageView iv = (ImageView) findViewById(R.id.imageView1);
    iv.setImageBitmap(bm);
}

}

again, just move your code out of onCreate(), you can only call opencv code after BaseLoaderCallback finished loading.:

public class PicActivity extends Activity  {

{

    @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_pic);
}

    }

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
     @Override
     public void onManagerConnected(int status) {
         if (status == LoaderCallbackInterface.SUCCESS ) {
             // now we can call opencv code !
             helloworld();
         } else {
             super.onManagerConnected(status);
        }
    }
    }
        }
    };

 @Override
 public void onResume() {;
     super.onResume();
     OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5,this, mLoaderCallback);
     // you may be tempted, to do something here, but it's *async*, and may take some time,
     // so any opencv call here will lead to unresolved native errors.
}

    }

    public void helloworld() {
     // make a mat and draw something
     Mat m = Mat.zeros(100,400, CvType.CV_8UC3);
     Core.putText(m, "hi there ;)", new Point(30,80), Core.FONT_HERSHEY_SCRIPT_SIMPLEX, 2.2, new Scalar(200,200,0),2);

     // convert to bitmap:
     Bitmap bm = Bitmap.createBitmap(m.cols(), m.rows(),Bitmap.Config.ARGB_8888);
     Utils.matToBitmap(m, bm);
     // find the imageview and draw it!
     ImageView iv = (ImageView) findViewById(R.id.imageView1);
     iv.setImageBitmap(bm);
    }
}

}