运行openCV示例时没有菜单(Android Studio)

时间:2015-06-26 13:09:49

标签: android opencv android-studio menu

运行openCV-Android示例无法按预期工作。我在Windows上安装了Android Studio,提取了OpenCV-Android 2.4.11,示例是tutorial-1-camerapreview(但其他样本似乎也不起作用)

如果我使用'导入模块'并在设备上运行Tutorial1CameraView,一切正常。我有3个普通按钮(link)加上第4个按钮,打开一个菜单。

如果我使用'新项目'并且就像我自己编写的那样(复制/粘贴教程中的代码和文件),它运行相同的应用程序,但没有第4个按钮。所以我无法打开菜单。

这正是我所做的:

  • 新项目 - >空白活动

  • 导入openCV库(I followed the instructions on this page

  • 在我的MainActivity中复制/粘贴Tutorial1CameraView的源代码

  • 将教程中的res文件夹与Android studio创建的res文件夹合并。我删除了一些文件,比如旧的菜单文件夹 (在我创建项目时生成,并且没有被教程使用), 确保他们不会干涉。 (但如果我继续下去,它仍然无效 它们)

  • 在java和xml文件中重命名一些琐碎的东西,比如包/类,以便编译

如果有帮助,我的java代码是:

        import org.opencv.android.BaseLoaderCallback;
        import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
        import org.opencv.android.LoaderCallbackInterface;
        import org.opencv.android.OpenCVLoader;
        import org.opencv.core.Mat;
        import org.opencv.android.CameraBridgeViewBase;
        import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;

        import android.app.Activity;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.SurfaceView;
        import android.view.WindowManager;
        import android.widget.Toast;

public class MainActivity extends Activity implements CvCameraViewListener2 {
    private static final String TAG = "OCVSample::Activity";

    private CameraBridgeViewBase mOpenCvCameraView;
    private boolean              mIsJavaCamera = true;
    private MenuItem             mItemSwitchCamera = null;

    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;
            }
        }
    };

    public MainActivity() {
        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.tutorial1_surface_view);

        if (mIsJavaCamera)
            mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view);
        else
            mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_native_surface_view);

        mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);

        mOpenCvCameraView.setCvCameraViewListener(this);
    }

    @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();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.i(TAG, "called onCreateOptionsMenu");
        mItemSwitchCamera = menu.add("Toggle Native/Java camera");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        String toastMesage = new String();
        Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);

        if (item == mItemSwitchCamera) {
            mOpenCvCameraView.setVisibility(SurfaceView.GONE);
            mIsJavaCamera = !mIsJavaCamera;

            if (mIsJavaCamera) {
                mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view);
                toastMesage = "Java Camera";
            } else {
                mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_native_surface_view);
                toastMesage = "Native Camera";
            }

            mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
            mOpenCvCameraView.setCvCameraViewListener(this);
            mOpenCvCameraView.enableView();
            Toast toast = Toast.makeText(this, toastMesage, Toast.LENGTH_LONG);
            toast.show();
        }

        return true;
    }

    public void onCameraViewStarted(int width, int height) {
    }

    public void onCameraViewStopped() {
    }

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        return inputFrame.rgba();
    }
}

我可以加入所需的任何其他文件(它可能与java无关......)

可能是什么问题?为什么应用程序有效,但只有菜单没有?

2 个答案:

答案 0 :(得分:2)

要解决此问题,如果AndroidManifest文件中您的活动的theme属性以.Fullscreen结尾,请移除.Fullscreen

编辑:显然删除.Fullscreen不起作用,但将theme替换为没有.Fullscreen / .NoActionBar属性的android:theme="@style/ThemeOverlay.AppCompat.ActionBar"。在这种情况下.... public void QuerySQL(String SQL) { try { Statement statement = connect.createStatement(); rs = statement.executeQuery(SQL); List<Map<String, String>> data = null; data = new ArrayList<Map<String,String>>(); while(rs.next()) { Map<String, String> datanum = new HashMap<String, String>(); datanum.put("A", rs.getString("RoomNumber")); datanum.put("B", rs.getString("RoomStatus")); datanum.put("C", rs.getString("RoomType")); data.add(datanum); } String[] from = {"A","B","C"}; int[] views = {R.id.txt_number,R.id.status,R.id.txt_type}; ADAhere = new SimpleAdapter(this, data, R.layout.layout_list_item, from, views); listViewStats.setAdapter(ADAhere); } catch (Exception e) { Toast.makeText(Main.this, e.getMessage().toString(),Toast.LENGTH_LONG).show(); } } .... 完成了这个伎俩。

答案 1 :(得分:0)

在清单文件中删除android:theme =“@ style / Theme”。