我正在尝试使用Java在Android Studio中打开VideoCapture,但出于某种原因,每当我运行videoCapture.isOpened()时,我都被告知VideoCapture未被打开。我无法弄清楚为什么这可能是我可以做什么或我可以做什么来打开VideoCapture时我问。我的代码是:
public class SecondActivity extends Activity
{
private static final String TAG = "SecondActivity";
static
{
if(!OpenCVLoader.initDebug())
{
Log.d(TAG, "OpenCV not loaded");
}
else
{
Log.d(TAG, "OpenCV loaded");
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
public void videoClicked(View view)
{
VideoCapture videoCapture = new VideoCapture();
videoCapture.open(CameraBridgeViewBase.CAMERA_ID_FRONT);
if(videoCapture.isOpened()) Log.i(TAG, "Opened!");
else Log.i(TAG, "Not opened");
}
}
我有一个更复杂的活动,它显示屏幕上摄像机的信号,所以我知道摄像机正在工作,但我不明白为什么VideoCapture无法工作。该活动的代码如下,您可以看到我在那里尝试打开视频捕获的位置。
public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener2
{
private static final String TAG = "MainActivity";
private CameraBridgeViewBase mOpenCvCameraView;
private static HashMap<String, Integer> images = new HashMap<String, Integer>();
private BackgroundSubtractorMOG2 bgSub = new BackgroundSubtractorMOG2(30, 4, false);
private VideoCapture videoCapture;
private Size mPreviewSize;
private Mat mCurrentFrame, mFilteredFrame, mInRangeResult, mCurrentFrameHsv;
private ConcurrentLinkedQueue<Bitmap> mResultBitmaps = new ConcurrentLinkedQueue<Bitmap>();
public static final int RESULT_MATRIX_BUFFER_SIZE = 3;
static
{
if(!OpenCVLoader.initDebug())
{
Log.d(TAG, "OpenCV not loaded");
}
else
{
Log.d(TAG, "OpenCV loaded");
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
Log.i(TAG, "On Create");
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
}
//Edits the pixels of the image in image view to highlight certain colors
public void editPixelsClicked(View view)
{
OpenCVManager manager = new OpenCVManager();
ImageView imageView = (ImageView) findViewById(R.id.greenImage);
Bitmap bmp = manager.editPixels(this, imageView, R.mipmap.colorfulimage);
imageView.setImageBitmap(bmp);
}
public void captureClicked(View view)
{
mOpenCvCameraView.disableView();
setupCamera(0);
if(videoCapture.grab()) Log.i(TAG, "Grabbed");
else Log.i(TAG, "Failed to grab");
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
public void setupCamera(int cameraID)
{
if(videoCapture != null)
{
VideoCapture camera = videoCapture;
videoCapture = null;
camera.release();
}
videoCapture = new VideoCapture();
Log.i(TAG, "Recreated video capture");
videoCapture.open(1);
if(videoCapture.isOpened()) Log.i(TAG, "Video capture opened");
else Log.i(TAG, "Video capture failed to open");
/*List<Size> previewSizes = videoCapture.getSupportedPreviewSizes();
double largestPreviewSize = 1280 * 720; // We should be smaller than this...
double smallestWidth = 480; // Let's not get a smaller width than this...
for (Size previewSize : previewSizes) {
if (previewSize.area() < largestPreviewSize && previewSize.width >= smallestWidth) {
mPreviewSize = previewSize;
}
}*/
videoCapture.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, 500);
videoCapture.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, 300);
}
private void initMatrices() {
mCurrentFrame = new Mat();
mCurrentFrameHsv = new Mat();
mFilteredFrame = new Mat();
mInRangeResult = new Mat();
// Since drawing to screen occurs on a different thread than the processing,
// we use a queue to handle the bitmaps we will draw to screen
mResultBitmaps.clear();
for (int i = 0; i < RESULT_MATRIX_BUFFER_SIZE; i++) {
Bitmap resultBitmap = Bitmap.createBitmap((int) mPreviewSize.width, (int) mPreviewSize.height,
Bitmap.Config.ARGB_8888);
mResultBitmaps.offer(resultBitmap);
}
}
@Override
public void onPause()
{
super.onPause();
if(mOpenCvCameraView != null)
{
mOpenCvCameraView.disableView();
}
}
public void onDestroy()
{
super.onDestroy();
if(mOpenCvCameraView != null)
{
mOpenCvCameraView.disableView();
}
}
public void onCameraViewStarted(int width, int height)
{
Mat mat = new Mat(2,2, CvType.CV_8UC3, new Scalar(0,0,255));
}
public void onCameraViewStopped()
{
}
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
{
return inputFrame.rgba();
}
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;
}
}
};
@Override
public void onResume()
{
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_11, this, mLoaderCallback);
}
}