title: Convert from bitmap rgb_565 to Mat
body: <p>I am using Android Intent to take a photo. </p>
  
  <pre><code>startActivityForResult(intent, 0);
  </code></pre>
  
  <p>This opens the native camera and i can take a picture.
  In the Activity where the picture is returned to , in the onActivityResult method , I can get a hold of the returned Bitmap like so. </p>
  
  <pre><code>if (requestCode == 0 &amp;&amp; resultCode == RESULT_OK) {
          Bundle extras = data.getExtras();
          Bitmap imageBitmap = (Bitmap) extras.get("data");
  </code></pre>
  
  <p>I then wish to convert this to a Mat to do some openCV image processing. 
  I am attempting to convert like this. </p>
  
  <pre><code>Mat src = new Mat(imageBitmap.getHeight(), imageBitmap.getWidth(), CvType.CV_8UC4);
          Utils.bitmapToMat(imageBitmap, src);
  </code></pre>
  
  <p>The line above causes a crash with the following stack trace . </p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
                Process: com.example.i330155.testing123, PID: 16349
                java.lang.UnsatisfiedLinkError: No implementation found for long org.opencv.core.Mat.n_Mat(int, int, int) (tried Java_org_opencv_core_Mat_n_1Mat and Java_org_opencv_core_Mat_n_1Mat__III)
                    at org.opencv.core.Mat.n_Mat(Native Method)
                    at org.opencv.core.Mat.&lt;init&gt;(Mat.java:37)
                    at com.example.testing123.MainActivity.onActivityResult(MainActivity.java:48)
                    at android.app.Activity.dispatchActivityResult(Activity.java:7022)
                    at android.app.ActivityThread.deliverResults(ActivityThread.java:4248)
                    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4295)
                    at android.app.ActivityThread.-wrap20(ActivityThread.java)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1583)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:154)
                    at android.app.ActivityThread.main(ActivityThread.java:6290)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
  </code></pre>
  
  <p>After searching i found the following post. 
       <a href="http://answers.opencv.org/question/52722/what-is-the-correct-way-to-convert-a-mat-to-a-bitmap/" rel="nofollow noreferrer">http://answers.opencv.org/question/52722/what-is-the-correct-way-to-convert-a-mat-to-a-bitmap/</a></p>
  
  <p>Which says that. </p>
  
  <pre><code>mat is a valid input Mat object of the types 'CV_8UC1', 'CV_8UC3' or 'CV_8UC4'.
  bmp is a valid Bitmap object of the same size as the Mat and of type 'ARGB_8888' or 'RGB_565'.
  </code></pre>
  
  <p>As you can see in the constructor for the Mat I supply these args. 
  I have checked the config of the imageBitmap  , which confirms that it is indeed 
  a 'RGB_565'. </p>
  
  <p>What am i missing here? I dont understand why this does not work. 
  Thanks in advance.</p>
  
  <pre><code>import android.content.Intent;
  import android.graphics.Bitmap; 
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.ImageView;
  import org.opencv.android.Utils;
  import org.opencv.core.CvType;
  import org.opencv.core.Mat;
  import org.opencv.core.Scalar;
  import org.opencv.imgproc.Imgproc;
  
  
  public class MainActivity extends AppCompatActivity {
  
     Button button ;
     ImageView mImageView ;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          button = (Button) findViewById(R.id.capture) ;
          mImageView = (ImageView)findViewById(R.id.imageView) ;
          button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
              startActivityForResult(intent, 0);
          }
      });
  
  }
  
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == 0 &amp;&amp; resultCode == RESULT_OK) {
          Bundle extras = data.getExtras();
          Bitmap imageBitmap = (Bitmap) extras.get("data");
  
          Bitmap.Config config = imageBitmap.getConfig();
          //this is throwing error
          Mat src = new Mat(imageBitmap.getHeight(), imageBitmap.getWidth(), CvType.CV_8UC3);
          Utils.bitmapToMat(imageBitmap, src);
  
          Imgproc.Canny(src, src, 10, 100);
          Bitmap edgeBitmap = Bitmap.createBitmap(imageBitmap.getWidth(), imageBitmap.getHeight(), Bitmap.Config.ARGB_8888);
         // Utils.matToBitmap(dest, edgeBitmap);
  
          mImageView.setImageBitmap(imageBitmap);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>When debugging this method call : 
      Mat src = new Mat(imageBitmap.getHeight(), imageBitmap.getWidth(), 
      CvType.CV_8UC3); </p>
  
  <p>calls another method </p>
  
  <pre><code> nativeObj = n_Mat(rows, cols, type);
  </code></pre>
  
  <p>this n_Mat is declared like so. </p>
  
  <pre><code>   // C++: Mat::Mat(int rows, int cols, int type)
     private static native long n_Mat(int rows, int cols, int type);
  </code></pre>
  
  <p>and is highlighted in red.</p>