Ask Your Question
3

Display image?

asked 2013-07-18 03:57:44 -0600

Dannyk gravatar image

updated 2020-11-30 03:39:26 -0600

Hi? I was trying to display an image on opencv-android. Actually there are lots of samples regarding camera modules, but I couldn't find a nice tutorial for displaying an image on opencv with android platform.

Could someone let me share some tutorials?

Thanks in advance, Danny

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
10

answered 2013-07-18 05:56:33 -0600

berak gravatar image

let's try to build a most simple one from scratch ;)

step 1

create a new android application project, and add the opencv libraries to it.

( you have the opencv project in your workspace, do you ? )

step2

we'll need an imageview to show our image:

step3

get your hands dirty with code ;)

package com.example.pic;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;

import android.os.Bundle;
import android.widget.ImageView;
import android.app.Activity;
import android.graphics.Bitmap;

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);
    }
}
edit flag offensive delete link more

Comments

Submit it as a basic android tutorial? :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-07-18 06:00:18 -0600 )edit
1

What a perfect&kind tutorial!! Thank you so much :D

Dannyk gravatar imageDannyk ( 2013-07-18 19:11:45 -0600 )edit

Again @berak, push it in a tutorial! Don't let your work go to waste! :D

StevenPuttemans gravatar imageStevenPuttemans ( 2013-07-19 01:00:43 -0600 )edit

oh, thanks will do, still cleaning up my repo ..

berak gravatar imageberak ( 2013-07-19 01:17:44 -0600 )edit
1

it's very helpful. :)

stupidteam gravatar imagestupidteam ( 2015-04-07 13:15:03 -0600 )edit
1

Really worked like a charm. Thanks for the steps!!!!

One small change. I used OpenCV 3.4.1, so in that, instead of Core.putText(), one should use Imgproc.putText() with same parameters.

Hats off Sir. =D !!

QuickTungsten gravatar imageQuickTungsten ( 2018-12-30 00:44:14 -0600 )edit

THanks for the update for the newer OpenCV versions :)

StevenPuttemans gravatar imageStevenPuttemans ( 2019-01-01 15:16:25 -0600 )edit

Very useful, thanks! :D

ruskiikoshka gravatar imageruskiikoshka ( 2019-01-15 16:53:30 -0600 )edit

Question Tools

3 followers

Stats

Asked: 2013-07-18 03:57:44 -0600

Seen: 20,004 times

Last updated: Jul 18 '13