OpenCV 相机方向问题 [英] OpenCV camera orientation issue
问题描述
我有一个简单的项目,它只显示带有 org.opencv.android.JavaCameraView 的相机.
I have a simple project that just show the camera with org.opencv.android.JavaCameraView.
我的问题是,默认情况下,相机处于横向模式,我无法更改这一点,因为我需要定义 CameraBridgeViewBase 而不是常规相机意图.
my problem is that in default the camera is on landscape mode and I can't change this cause I need to define CameraBridgeViewBase instead of a regular camera intent.
这是我代码的一部分:
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;
}
}
};
那么如何更改默认方向?
So how can I change the default orientation?
谢谢!
推荐答案
好的,我发现这是一个解决方案:
Ok, I found this as a solution:
首先我进入 OpenCV 库 - 2.4.5
然后在 mCamera.startPreview();
之前的 initializeCamera()
函数中,我添加了这两个函数:
and then in initializeCamera()
function before mCamera.startPreview();
I added these 2 function:
setDisplayOrientation(mCamera, 90);
mCamera.setPreviewDisplay(getHolder());
第一个函数是这样实现的:
and the first function implemented like this:
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)
{
e1.printStackTrace();
}
}
我只是提醒我使用 OpenCV.
I just reminding that I work with OpenCV.
希望这对某人有所帮助.
Hope this help someone.
这篇关于OpenCV 相机方向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!