简体   繁体   English

如何在Android中使用我的OpenCV C ++代码?

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

I'm trying for the first time to create an Android application. 我正在尝试第一次创建一个Android应用程序。 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 在视频上应用OpenCV过滤器并将帧保存为图像

Here is what I have right now. 这就是我现在所拥有的。

Recording the video and saving it on my phone (Done on Android studio and working fine) 录制视频并将其保存在手机上(在Android工作室完成并正常工作)

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) 应用OpenCV过滤器(在下面的代码中,没有应用过滤器,因为它是一个测试代码,目标是保存视频中的帧),并保存一个帧(使用OpenCV 3.1完成Qt并正常工作)

#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. 尽管如此,我需要做的第一件事就是在Android中传输我的OpenCV C ++代码,但是已经有多个在线教程可以做到这一点,所以这不是问题所在。

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 ? 问题是,一旦我的C ++代码在我的Android项目中,我如何在我的Android Studio代码和C ++文件之间进行通信? 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 ? 基本上,如何使用我的C ++ OpenCV代码将手机上保存的.mp4视频保存到手机上?

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. 首先,您必须使用Android选项编译OpenCV。 Later on give below tutorials a try: 稍后在下面的教程中尝试一下:

This is a nice video tutorial. 是一个很好的视频教程。 This is another nice tutorial. 是另一个很好的教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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