我是第一次安装android程序员。 我正在进行的项目要求我做(简单?)实时视频处理。 该应用程序一旦完成需要这样做: 当我们点击内置的相机应用程序时,它会打开。然后我继续选择视频录制选项。使用它我可以看到周围环境而无需记录。我想要完成的是将显示延迟几百毫秒。我的同事可以使用笔记本电脑摄像头和openCV(用于计算机)使用延迟选项轻松完成此操作。我试图用Android手机完成同样的事情。
也许我在解释这种情况方面表现不佳。请尽早回复。 我现在正在编写代码,并且是第一次花费一些时间的程序员。 兴奋开始使用Android编程!
答案 0 :(得分:3)
不知道这个任务是否真的需要 opencv(可能有点过分杀戮)但是如果你选择那个,那就相当容易了。
看到我们这里所做的就是连续记录帧,并在某些事件上切换实时/回放模式(onTouch为了简单起见):
package com.berak.echo;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.app.Activity;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import com.berak.echo.R;
public class EchoActivity extends Activity implements CvCameraViewListener2, OnTouchListener {
CameraBridgeViewBase mOpenCvCameraView;
List<Mat> ring = new ArrayList<Mat>(); // recording buffer
int delay = 100; // delay == length of buffer
boolean delayed = false; // state
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_echo);
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.cam3_surface_view);
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.setOnTouchListener(this); // setup as touchlistener
}
// lots of boilerplate, ugly, but needed.
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
mOpenCvCameraView.enableView();
break;
default:
super.onManagerConnected(status);
break;
}
}
};
@Override
public void onResume() {;
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5,this, mLoaderCallback);
}
@Override
public void onPause() {
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onCameraViewStarted(int width, int height) { }
@Override
public void onCameraViewStopped() { }
// here's the bread & butter stuff:
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
Mat mRgba = inputFrame.rgba();
ring.add(mRgba.clone()); // add one at the end
if ( ring.size() >= delay ) { // pop one from the front
ring.get(0).release();
ring.remove(0);
}
Mat ret;
String txt;
if ( delayed && ring.size()>0 ) { // depending on 'delayed' return either playback
ret = ring.get(0); // return the 'oldest'
txt = "playback";
} else {
ret = mRgba; // or realtime frame
txt = "realtime";
}
Core.putText(ret, txt, new Point(20,20), Core.FONT_HERSHEY_PLAIN, 1.2, new Scalar(200,0,0));
return ret;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// just toggle between delayed an realtime view:
delayed = ! delayed;
return false;
}
}