How to read the image data of the camera application into OpenCV

Asked 2 years ago, Updated 2 years ago, 132 views

After taking a picture with a camera, we are creating an app that allows you to enter any character into the image.

I'm thinking about how to do it as follows:

  • Launch the camera application created by Android Studio, take pictures, and obtain image data
  • Read the image data into opencv and insert characters into the image.

Currently, the camera application has been completed, so I don't know how to import the image data taken in opencv from here.

Could someone please tell me?

Thank you for your cooperation.

Camera app source code

import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;

    public class MainActivity extensions Activity {

    static final int REQUEST_CAPTURE_IMAGE=100;

    Button button 1;
    ImageView imageView1;

    @ Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        setListeners();
    }

    protected void findViews() {
        button1 = (Button) findViewById (R.id.button1);
        imageView 1=(ImageView) findViewById (R.id.imageView1);
    }

    protected void setListeners() {
        button1.setOnClickListener(newOnClickListener(){
            @ Override
            public void onClick (View v) {
                Intent = new Intent(
                        MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(
                        int,
                        REQUEST_CAPTURE_IMAGE);
            }
        });
    }

    @ Override
    protected void onActivityResult(
            int requestCode,
            int resultCode,
            US>Intent data) {
        if(REQUEST_CAPTURE_IMAGE==requestCode
                &resultCode==Activity.RESULT_OK){
            Bitmap capturedImage=
                    (Bitmap) data.getExtras().get("data");
            imageView 1.setImageBitmap(capturedImage);
        }
    }

}

java android opencv

2022-09-30 21:28

1 Answers

Do you have an environment to develop OpenCV on Android?
If not, download opencv from the formula and set up the library.
Create your own project if it's a simple opencv capture and put it in AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera2"android:required="false"/>
<uses-feature android:name="android.hardware.camera2.autofocus"android:required="false"/>

Add to layout

<org.opencv.android.JavaCameraView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:visibility="gone"
    android:id="@+id/tutorial1_activity_java_surface_view"
    opencv:show_fps="true"
    openscv:camera_id="any"/>

Add to Main

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
 
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.Toast;
 
public class MainActivity extensions AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener{
    private static final String TAG = "OCVSample::Activity";
 
    private CameraBridgeViewBase mOpenCvCameraView;
    private boolean mIsJavaCamera=true;
    private MenuItemmItemSwitchCamera=null;
 
    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this){
        @ Override
        public void onManagerConnected (int status) {
            switch(status){
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i(TAG, "OpenCV loaded successfully");
                    mOpenCvCameraView.enableView();
                } break;
                default:
                {
                    super.onManagerConnected (status);
                } break;
            }
        }
    };
 
    publicMainActivity(){
        Log.i(TAG, "Instantiated new" + this.getClass());
    }
 
    /** Called when the activity is first created.*/
    @ Override
    public void onCreate (Bundle savedInstanceState) {
        Log.i(TAG, "called onCreate");
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
 
        setContentView(R.layout.activity_main);
 
        mOpenCvCameraView= (CameraBridgeViewBase) findViewById (R.id.tutorial1_activity_java_surface_view);
 
        mOpenCvCameraView.setVisibility (SurfaceView.VISIBLE);
 
        mOpenCvCameraView.setCvCameraViewListener(this);
    }
 
    @ Override
    public void onPause()
    {
        super.onPause();
        if(mOpenCvCameraView!=null)
            mOpenCvCameraView.disableView();
    }
 
    @ Override
    public void onResume()
    {
        super.onResume();
        if(!OpenCVLoader.initDebug()){
            Log.d(TAG, "Internal OpenCV library not found.Using OpenCV Manager for initialization");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
        } else{
            Log.d(TAG, "OpenCV library found inside package.Using it!");
            mLoaderCallback.onManagerConnected (LoaderCallbackInterface.SUCCESS);
        }
    }
 
    public void onDestroy() {
        super.onDestroy();
        if(mOpenCvCameraView!=null)
            mOpenCvCameraView.disableView();
    }
 
    public void onCameraViewStarted(int width, int height) {
    }
 
    public void onCameraViewStopped(){
    }
 
    public MatonCameraFrame (MatinputFrame) {
        return inputFrame;
    }
}

Try to run the .
I think there will be errors in build.grable and main, but please adjust it to your environment.
By the way, my code is OpenCV 3.2.


2022-09-30 21:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.