简体   繁体   中英

How can I use my OpenCV C++ code in Android ?

I'm trying for the first time to create an Android application. I'm starting with something quite basic I suppose but since I'm new to this I don't know how to do it.

The goal is :

  • Record a video using my phone and save it

  • Apply an OpenCV filter on the video and save a frame as an image

Here is what I have right now.

Recording the video and saving it on my phone (Done on Android studio and working fine)

package com.example.adrien.myfirstapp;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends Activity {

    private final int VIDEO_REQUEST_CODE = 100;

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

    public void captureVideo(View view){

        Intent camera_intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        File video_file = getFilepath();
        Uri video_uri = Uri.fromFile(video_file);
        camera_intent.putExtra(MediaStore.EXTRA_OUTPUT,video_uri);
        camera_intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivityForResult(camera_intent, VIDEO_REQUEST_CODE);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(requestCode == VIDEO_REQUEST_CODE){

            if(resultCode==RESULT_OK){

                Toast.makeText(getApplicationContext(), "Video Successfully Recorded", Toast.LENGTH_LONG).show();

            }else{

                Toast.makeText(getApplicationContext(), "Video Capture Failed", Toast.LENGTH_LONG).show();
            }

        }
    }

    public File getFilepath(){

        File folder = new File("sdcard/video_app");

        if(!folder.exists()){

            folder.mkdir();

        }

        File video_file = new File(folder, "sample_video.mp4");

        return video_file;
    }
}

Apply an OpenCV filter (In the code below, no filter was applied because it's a test code and the goal was juste to save a frame from a video) on the video and save a frame (Done on Qt with OpenCV 3.1 and working fine)

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFileDialog>
#include <QString>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv/cv.h>
#include <opencv2/objdetect/objdetect.hpp>

#include <qdebug.h>

using namespace cv;


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{

    QString filename = "C:\\Users\\Adrien\\Desktop\\feZLKRY.mp4";

    VideoCapture capture(filename.toUtf8().constData());

    Mat img;

    capture >>(img);

    imshow("Video", img);
    imwrite("C:\\Users\\Adrien\\Desktop\\image.jpg", img);

    waitKey(30);

}

With all that, the first thing I'll need to do is transfer my OpenCV C++ code in Android but there are already multiple tutorials online to do it so it's not the question here.

The question is, once my C++ code is in my Android project, how can I communicate between my Android Studio code and the C++ file ? Basically, how can I use the .mp4 video saved on my phone with my C++ OpenCV code to then save a frame on my phone ?

Actually it's a whole tutorial long question. I just want to propose some external sources you can consult. First, you have to compile OpenCV with Android option. Later on give below tutorials a try:

This is a nice video tutorial. This is another nice tutorial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM