问题
i am trying to set a video to fullscreen using opencv. the input is coming from the camera. it shows on the screen in the center but not as full screen. relevant code:
public class Sample3Native extends Activity implements CvCameraViewListener {
private static final String TAG = "OCVSample::Activity";
private Mat mRgba;
private Mat mGrayMat;
private Mat mFinalMat;
private CameraBridgeViewBase mOpenCvCameraView;
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("native_sample");
mOpenCvCameraView.enableView();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
public Sample3Native() {
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);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.tutorial3_surface_view);
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial4_activity_surface_view);
mOpenCvCameraView.setCvCameraViewListener(this);
}
i have tried using:
//mOpenCvCameraView.setScaleX(0.8f);
// mOpenCvCameraView.setScaleY(0.4f);
//mOpenCvCameraView.setMinimumHeight(800);
// mOpenCvCameraView.setMinimumWidth(1000);
but it doesnot work.
how can i strech the frames to fill all of the screen?
回答1:
You can get a list of the available resolutions or setup manually.
If you want to get the list of the available sizes and get the optimal camera size.
if (mOpenCvCameraView != null) {
List<Size> sizes = mOpenCvCameraView.getSupportedPreviewSizes();
int mFrameWidth = width;
int mFrameHeight = height;
// selecting optimal camera size
{
double minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - height) < minDiff) {
mFrameWidth = (int) size.width;
mFrameHeight = (int) size.height;
minDiff = Math.abs(size.height - height);
}
}
}
mOpenCvCameraView.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, mFrameWidth);
mOpenCvCameraView.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
}
If the optimal camera size is not fullscreen, it is easy to get the great size that the camera support from the list.
Other option is to set manually the size:
mOpenCvCameraView.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, 1000);
mOpenCvCameraView.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, 800);
The source of the code is from Face Detection OpenCV version 2.4.2.
回答2:
For those who google here and just want to change the resolution,
mOpenCvCameraView.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, 1000)
method is not working.
Try to use mOpenCvCameraView.setMaxFrameSize(640, 480);
It works for OCV4Android2.4.5, tutorial 2, mixed processing.
回答3:
For anyone still looking for this, I have solved the issue (sort-of). I cannot seem to get this to work for OpenCV v 2.4.3. I have tried everything on the internet. However, in OpenCV 2.4.3.2 they have added a way to set the camera size
disconnectCamera();
mMaxHeight = //your desired height
mMaxWidth = //your desired width
connectCamera(mMaxWidth, mMaxHeight);
Check out the OpenCV Tutorial 5, specifically the SampleJavaCameraView -> setResolution Method
回答4:
Not the best solution, but you can go to the AndroidManifest.xml
and set android:screenOrientation="landscape"
With this you won't need to rotate de camera 90º, but your application will always be in landscape mode.
回答5:
Natural camera orientation of the OpenCV camera is landscape so make your activity is like
android:screenOrientation="landscape"
and apply this theme in the Activity
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
...
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
this trick gives you a fullscreen camera.
回答6:
Try this replace
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
code in the onCreate with
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
来源:https://stackoverflow.com/questions/13376420/setting-opencv-video-to-fullscreen-android