我有一个简单的项目,只是用org.opencv.
android.
JavaCameraView显示相机.
我的问题是,在默认情况下,相机是在横向模式,我不能改变这个原因,我需要定义CameraBridgeViewBase而不是一般的相机意图.
这是我的代码的一部分:
XML代码:
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" > <org.opencv.android.JavaCameraView android:layout_width="fill_parent" android:layout_height="300dp" android:visibility="gone" android:id="@+id/HelloOpenCvView" opencv:show_fps="true" opencv:camera_id="1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/BtnVideo" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:layout_width="0dp" style="@style/button" android:layout_height="wrap_content" android:layout_weight="1.00" android:text="@string/videoBtn" android:textSize="18dip" /> </LinearLayout>
Java代码:
CameraBridgeViewBase mOpenCvCameraView; Button VideoButton; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); overridePendingTransition(0,0); VideoButton = (Button) this.findViewById(R.id.BtnVideo); VideoButton.setOnClickListener(onClickListener); mOpenCvCameraView= (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView); mOpenCvCameraView.setVisibility(SurfaceView.INVISIBLE); } private OnClickListener onClickListener = new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()){ case R.id.BtnVideo: if(mOpenCvCameraView.getVisibility() == SurfaceView.VISIBLE) { mOpenCvCameraView.setVisibility(SurfaceView.INVISIBLE); } else { mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE); } break; default : break; } } }; public void onResume() { super.onResume(); overridePendingTransition(0,0); OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3,this,mLoaderCallback); } 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) { } public void onCameraViewStopped() { } public Mat onCameraFrame(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; } } };
那么如何更改默认方向?
谢谢!
解决方法
好的,我发现这是一个解决方案:
首先我进入OpenCV库中的JavaCameraView.Java类 – 2.4.5
然后在mCamera.startPreview()之前的initializeCamera函数中;我添加了这个2功能:
setDisplayOrientation(mCamera,90); mCamera.setPreviewDisplay(getHolder());
第一个功能如下:
protected void setDisplayOrientation(Camera camera,int angle){ Method downPolymorphic; try { downPolymorphic = camera.getClass().getMethod("setDisplayOrientation",new Class[] { int.class }); if (downPolymorphic != null) downPolymorphic.invoke(camera,new Object[] { angle }); } catch (Exception e1) { } }
我只是提醒我使用OpenCV.
希望这个有帮助的人