我正在跟踪一个教程,该教程从屏幕上显示的内容中滤除某些颜色。我在OpenCV和JavaCameraView上遇到的一个常见问题是摄像机显示的内容在屏幕上旋转了90度。
一种解决方案更改JavaCameraView.java文件以添加以下代码:
/* Finally we are ready to start the preview */
Log.d(TAG, "startPreview");
//inserted code
setDisplayOrientation(mCamera, 90);
mCamera.setPreviewDisplay(getHolder());
//end
mCamera.startPreview();
}
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)
{
}
}
这成功地使屏幕显示了与相机相同的方向,但是缺少了效果。
在另一个解决方案中,我在onCreate方法中添加了以下代码行:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
这只会阻止整个应用程序从纵向更改为横向,而不会对屏幕上的图像进行任何操作。
如何旋转相机显示的内容而不丢失已处理的内容?
这是我的整个MainAtivity.java:
public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {
JavaCameraView javaCameraView;
Mat mat1, mat2;
Scalar scalarLow, scalarHigh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OpenCVLoader.initDebug();
javaCameraView = (JavaCameraView)findViewById(R.id.cameraView);
javaCameraView.setCameraIndex(0);
scalarLow = new Scalar(45,20,10);
scalarHigh = new Scalar(75,255,255);
javaCameraView.setCvCameraViewListener(this);
javaCameraView.enableView();
}
@Override
protected void onPause() {
super.onPause();
javaCameraView.disableView();
}
@Override
protected void onResume() {
super.onResume();
javaCameraView.enableView();
}
@Override
protected void onDestroy() {
javaCameraView.disableView();
super.onDestroy();
}
@Override
public void onCameraViewStopped() {
}
@Override
public void onCameraViewStarted(int width, int height) {
mat1 = new Mat(width,height,CvType.CV_16UC4);
mat2 = new Mat(width,height,CvType.CV_16UC4);
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
Imgproc.cvtColor(inputFrame.rgba(),mat1,Imgproc.COLOR_BGR2HSV);
Core.inRange(mat1,scalarLow,scalarHigh,mat2);
return mat2;
}
}