Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

opencv - convert Bitmap to Mat after capture image using android camera

Mat b = new Mat();
Bitmap bmp = getIntent().getExtras().getParcelable("image_send");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_image);
    Mat tmp = new Mat (bmp.getWidth(), bmp.getHeight(), CvType.CV_8UC1);
    Utils.bitmapToMat(bmp, tmp);
    Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);
    //Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_GRAY2RGB, 4);
    Utils.matToBitmap(tmp, bmp);

    iv = (ImageView) findViewById(R.id.imageView1);
    iv.setImageBitmap(bmp);
}

Can't display the bmp. My app has stopped after taking a picture.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Utils.bitmapToMat converts an Android Bitmap to an OpenCV Mat. It requires a bitmap of type ARGB_8888 or RGB_565.

import org.opencv.android.Utils;

Mat mat = new Mat();    
Bitmap bmp32 = bmp.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bmp32, mat);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...