convert Mat to Bitmap Opencv for Android

前端 未结 2 650
旧巷少年郎
旧巷少年郎 2020-12-03 03:31

I\'m trying to covert a MAt to a Bitmap use following code :

    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Mat tmp = new          


        
相关标签:
2条回答
  • 2020-12-03 04:06

    Try this code for mat to bitmap conversion

    **Mat mRgba;
    public void onCameraViewStarted(int width, int height) {
            mRgba = new Mat(height, width, CvType.CV_8UC4);
    }
    public Mat onCameraFrame(Mat inputFrame) {
       inputFrame.copyTo(mRgba);
       return mRgba;
    }
    private void captureBitmap(){
           bitmap = Bitmap.createBitmap(mOpenCvCameraView.getWidth()/4,mOpenCvCameraView.getHeight()/4, Bitmap.Config.ARGB_8888);
        try {
              bitmap = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
                    Utils.matToBitmap(mRgba, bitmap);
                   mBitmap.setImageBitmap(bitmap);
                   mBitmap.invalidate();
        }catch(Exception ex){
              System.out.println(ex.getMessage());
        }
     }**
    
    0 讨论(0)
  • 2020-12-03 04:08

    1) OpenCV Mat constructor expects <rows, cols> pair instead of <width, height> as its arguments. So you have to change your second line to

    Mat tmp = new Mat (height, width, CvType.CV_8U, new Scalar(4));
    

    2) Imgproc.cvtColor can change the dimensions of the tmp object. So it is safe to create a bitmap after the color conversion:

    Bitmap bmp = null;
    Mat tmp = new Mat (height, width, CvType.CV_8U, new Scalar(4));
    try {
        //Imgproc.cvtColor(seedsImage, tmp, Imgproc.COLOR_RGB2BGRA);
        Imgproc.cvtColor(seedsImage, tmp, Imgproc.COLOR_GRAY2RGBA, 4);
        bmp = Bitmap.createBitmap(tmp.cols(), tmp.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(tmp, bmp);
    }
    catch (CvException e){Log.d("Exception",e.getMessage());}
    
    0 讨论(0)
提交回复
热议问题