使用JavaCameraView的自定义类会使应用程序崩溃

时间:2015-05-23 10:12:33

标签: java android opencv android-camera opencv4android

我正在使用OpenCV for android,我正在尝试设置自定义帧速率。那么现在我正在尝试设置帧速率的范围。我也在尝试设置自定义分辨率

我正在关注openCV教程3 Camera Control。当我开始在我的设备上运行时,应用程序崩溃了。我使用的是Nexus 7 2013平板电脑

以下是我的代码:

public class Tutorial2Activity extends Activity implements CvCameraViewListener2

{
    private static final String    TAG = "OCVSample::Activity";

    private static final int       VIEW_MODE_RGBA     = 0;
    private static final int       VIEW_MODE_GRAY     = 1;
    private static final int       VIEW_MODE_CANNY    = 2;
    private static final int       VIEW_MODE_FEATURES = 5;

    private int                    mViewMode;
    private Mat                    mRgba;
    private Mat                    mIntermediateMat;
    private Mat                    mGray;

    private MenuItem               mItemPreviewRGBA;
    private MenuItem               mItemPreviewGray;
    private MenuItem               mItemPreviewCanny;
    private MenuItem               mItemPreviewFeatures;

    private CustomizeCameraView   mOpenCvCameraView;
    private List<Size> mResolutionList;

    private BaseLoaderCallback  mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i(TAG, "OpenCV loaded successfully");

                    // Load native library after(!) OpenCV initialization
                    System.loadLibrary("mixed_sample");

                    mOpenCvCameraView.enableView();
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };

    public Tutorial2Activity() {
        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.tutorial2_surface_view);

        mOpenCvCameraView = (CustomizeCameraView) findViewById(R.id.tutorial2_activity_surface_view);
        mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
        mOpenCvCameraView.setCvCameraViewListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.i(TAG, "called onCreateOptionsMenu");
        mItemPreviewRGBA = menu.add("Preview RGBA");
        mItemPreviewGray = menu.add("Preview GRAY");
        mItemPreviewCanny = menu.add("Canny");
        mItemPreviewFeatures = menu.add("Find features");



        return true;
    }

    @Override
    public void onPause()
    {
        super.onPause();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
    }

    public void onDestroy() {
        super.onDestroy();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }

    public void onCameraViewStarted(int width, int height) {
        mRgba = new Mat(height, width, CvType.CV_8UC4);
        mIntermediateMat = new Mat(height, width, CvType.CV_8UC4);
        mGray = new Mat(height, width, CvType.CV_8UC1);

        mResolutionList = mOpenCvCameraView.getResolutionList();


        int mFrameWidth = (int) mResolutionList.get(7).width;
        int mFrameHeight = (int) mResolutionList.get(7).height;

        mOpenCvCameraView.setResolution(mFrameHeight,mFrameWidth);
        //mOpenCvCameraView.setPreviewFPS(1000, 31000);


    }

    public void onCameraViewStopped() {
        mRgba.release();
        mGray.release();
        mIntermediateMat.release();

    }

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

        return inputFrame.rgba();
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);

        if (item == mItemPreviewRGBA) {
            mViewMode = VIEW_MODE_RGBA;
        } else if (item == mItemPreviewGray) {
            mViewMode = VIEW_MODE_GRAY;
        } else if (item == mItemPreviewCanny) {
            mViewMode = VIEW_MODE_CANNY;
        } else if (item == mItemPreviewFeatures) {
            mViewMode = VIEW_MODE_FEATURES;
        }

        return true;
    }

    public native void FindFeatures(long matAddrGr, long matAddrRgba);
}

JavaCameraView自定义类

 public class CustomizeCameraView extends JavaCameraView{

    public CustomizeCameraView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

     //@SuppressWarnings("deprecation")
    public void setPreviewFPS(double  min, double max){
            Camera.Parameters params = mCamera.getParameters();
            params.setPreviewFpsRange((int)(min*1000), (int)(max*1000));
            //params.setPreviewFrameRate(min);
            mCamera.setParameters(params);
           // mCamera.getSupportedPreviewFpsRange();
        }

     public List<Size> getResolutionList() {
         return mCamera.getParameters().getSupportedPreviewSizes();
          // return mCamera.getParameters().getPictureSize();
        }

     public void setResolution(int h,int w){
         Camera.Parameters params = mCamera.getParameters();
         params.setPreviewSize(mFrameWidth, mFrameHeight);
            mCamera.setParameters(params); // mCamera is a Camera object

     }

     public Size getResolution() {
            return mCamera.getParameters().getPreviewSize();
        }
}

并且logcat输出如下:

05-23 14:27:18.192: E/AndroidRuntime(29339): FATAL EXCEPTION: main
05-23 14:27:18.192: E/AndroidRuntime(29339): Process: org.opencv.samples.tutorial2, PID: 29339
05-23 14:27:18.192: E/AndroidRuntime(29339): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.opencv.samples.tutorial2/org.opencv.samples.tutorial2.Tutorial2Activity}: java.lang.ClassCastException: org.opencv.android.JavaCameraView cannot be cast to org.opencv.samples.tutorial2.CustomizeCameraView

1 个答案:

答案 0 :(得分:1)

我找到了解决方案

问题在于我的布局文件。我插入了用于直接使用JavaCameraView的视频显示标签,而不是使用JavaCameraView中的继承类

问题:

< org.opencv.JavaCameraView />

校正:

< "package_name"."Class_name_that_you_inherited_from_JavaCameraView" />

我希望它有助于其他人:)