Ask Your Question
0

bitmap to mat error

asked 2013-05-23 16:52:04 -0600

uysal10 gravatar image

hi guys i am new user about android eclipse and opencv. i get some error when bitmap to mat converting.could you look my logcat and say something about this error?

code;


public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            if (!OpenCVLoader.initDebug()) {
                // Handle initialization error
            }
            ImageView goster=(ImageView)findViewById(R.id.imageView1);
            Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.testx);
           BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Config.RGB_565;
           Bitmap input = BitmapFactory.decodeFile("testx.jpg", options);
           Mat mat = new Mat();
           // Utils.bitmapToMat(input, mat);
       //     Highgui.imwrite("mat.jpg", mat);
            goster.setImageBitmap(input);


        }
    }

and logcat

    05-24 00:41:11.080: E/AndroidRuntime(7964): FATAL EXCEPTION: main
05-24 00:41:11.080: E/AndroidRuntime(7964): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.new2/com.example.new2.MainActivity}: java.lang.IllegalArgumentException: bmp == null
05-24 00:41:11.080: E/AndroidRuntime(7964):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-24 00:41:11.080: E/AndroidRuntime(7964):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-24 00:41:11.080: E/AndroidRuntime(7964):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-24 00:41:11.080: E/AndroidRuntime(7964):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-24 00:41:11.080: E/AndroidRuntime(7964):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 00:41:11.080: E/AndroidRuntime(7964):     at android.os.Looper.loop(Looper.java:137)
05-24 00:41:11.080: E/AndroidRuntime(7964):     at android.app.ActivityThread.main(ActivityThread.java:4745)
05-24 00:41:11.080: E/AndroidRuntime(7964):     at java.lang.reflect.Method.invokeNative(Native Method)
05-24 00:41:11.080: E/AndroidRuntime(7964):     at java.lang.reflect.Method.invoke(Method.java:511)
05-24 00:41:11.080: E/AndroidRuntime(7964):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-24 00:41:11.080: E/AndroidRuntime(7964):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-24 00:41:11.080: E/AndroidRuntime(7964):     at dalvik.system.NativeStart.main(Native Method)
05-24 00:41:11.080: E/AndroidRuntime(7964): Caused by: java.lang.IllegalArgumentException: bmp == null
05-24 00:41:11.080: E/AndroidRuntime(7964):     at org.opencv.android.Utils.bitmapToMat(Utils.java:90)
05-24 00:41:11.080: E/AndroidRuntime(7964):     at org.opencv.android.Utils.bitmapToMat(Utils.java:102)
05-24 00:41:11.080: E/AndroidRuntime(7964):     at com.example.new2.MainActivity.onCreate(MainActivity.java:41)
05-24 00:41:11.080: E/AndroidRuntime(7964):     at android.app.Activity.performCreate(Activity.java:5008)
05-24 00:41:11.080: E/AndroidRuntime(7964):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-24 00:41:11.080: E/AndroidRuntime(7964):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-24 00:41:11.080: E/AndroidRuntime(7964):     ... 11 more
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-12-11 03:00:35 -0600

itay gravatar image

updated 2013-12-11 03:01:30 -0600

Your problem is that the mat object are known only after LoaderCallbackInterface is SUCCESS

try to do this(I didn't check your code) :

public class MainActivity extends Activity implements CvCameraViewListener2 {

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

    ImageView goster=(ImageView)findViewById(R.id.imageView1);
    Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.testx);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Config.RGB_565;
    Bitmap input = BitmapFactory.decodeFile("testx.jpg", options);



}
@Override
public void onResume() {
    super.onResume();
    overridePendingTransition(0, 0);
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this, mLoaderCallback);
}
 public void onPause()
 {
     super.onPause();
     if (mOpenCvCameraView != null)
         mOpenCvCameraView.disableView();
 }
 public void onDestroy() {
     super.onDestroy();
     if (mOpenCvCameraView != null)
         mOpenCvCameraView.disableView();
 }
@Override
public void onCameraViewStarted(int width, int height) {
    // TODO Auto-generated method stub

}
@Override
public void onCameraViewStopped() {
    // TODO Auto-generated method stub

}
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    // TODO Auto-generated method stub
    return null;
}
Date startdate, enddate;
 private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {

                    Mat mat = new Mat();
                    // Utils.bitmapToMat(input, mat);
                    //     Highgui.imwrite("mat.jpg", mat);
                    goster.setImageBitmap(input);

                } //break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };

}

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-05-23 16:52:04 -0600

Seen: 2,636 times

Last updated: Dec 11 '13