始终在openCV \ javaCV中以前景摄像头的横向模式方向打开

时间:2013-10-04 21:15:50

标签: android opencv javacv

我制作了一个应用程序,用于在我的Android手机中打开前置摄像头。 问题是它总是打开相机,但显示的图像仅处于横向模式。

enter image description here

因此无论我是否正确地按住手机,我都会获得90度的方向差异。

如何解决这个问题。

我的布局代码 -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:opencv="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".AppLockMainActivity" >

<!-- <ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:src="@drawable/bol" />-->

  <org.opencv.android.NativeCameraView
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:visibility="visible"        
        android:id="@+id/nativeCamera"
        opencv:show_fps="false"
        opencv:camera_id="front" />

和Application.xml -

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
    <activity
        android:name="com.example.applock.AppLockMainActivity"
        android:screenOrientation="portrait"
        android:label="@string/app_name" 
        android:configChanges="keyboardHidden|orientation" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

2 个答案:

答案 0 :(得分:1)

这是一个非常常见的问题,常见的解决方案是在CameraBridgeViewBase,deliverrandDraw方法中设置Orientation。它不是永久性的修复,它是一种黑客攻击。

 protected void deliverAndDrawFrame(CvCameraViewFrame frame) {
        Mat modified;

        if (mListener != null) {
            modified = mListener.onCameraFrame(frame);
        } else {
            modified = frame.rgba();
        }

        boolean bmpValid = true;
        if (modified != null) {
            try {
                Utils.matToBitmap(modified, mCacheBitmap);
            } catch(Exception e) {
                Log.e(TAG, "Mat type: " + modified);
                Log.e(TAG, "Bitmap type: " + mCacheBitmap.getWidth() + "*" + mCacheBitmap.getHeight());
                Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage());
                bmpValid = false;
            }
        }

        if (bmpValid && mCacheBitmap != null) {
            Canvas canvas = getHolder().lockCanvas();
            if (canvas != null) {
//                canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
//                Log.d(TAG, "mStretch value: " + mScale);
//
//                if (mScale != 0) {
//                    canvas.drawBitmap(mCacheBitmap, new Rect(0,0,mCacheBitmap.getWidth(), mCacheBitmap.getHeight()),
//                         new Rect((int)((canvas.getWidth() - mScale*mCacheBitmap.getWidth()) / 2),
//                         (int)((canvas.getHeight() - mScale*mCacheBitmap.getHeight()) / 2),
//                         (int)((canvas.getWidth() - mScale*mCacheBitmap.getWidth()) / 2 + mScale*mCacheBitmap.getWidth()),
//                         (int)((canvas.getHeight() - mScale*mCacheBitmap.getHeight()) / 2 + mScale*mCacheBitmap.getHeight())), null);
//                } else {
//                     canvas.drawBitmap(mCacheBitmap, new Rect(0,0,mCacheBitmap.getWidth(), mCacheBitmap.getHeight()),
//                         new Rect((canvas.getWidth() - mCacheBitmap.getWidth()) / 2,
//                         (canvas.getHeight() - mCacheBitmap.getHeight()) / 2,
//                         (canvas.getWidth() - mCacheBitmap.getWidth()) / 2 + mCacheBitmap.getWidth(),
//                         (canvas.getHeight() - mCacheBitmap.getHeight()) / 2 + mCacheBitmap.getHeight()), null);
//                }

                //ABC : Fixed for image rotation
                //TODO Why portrait is not opening in fulls creen
                Matrix matrix = new Matrix();
                int height_Canvas = canvas.getHeight();
                int width_Canvas = canvas.getWidth();

                int width = mCacheBitmap.getWidth();
                int height = mCacheBitmap.getHeight();

                float f1 = (width_Canvas - width) / 2;
                float f2 = (height_Canvas - height) / 2;
                matrix.preTranslate(f1, f2);
                if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
                matrix.postRotate(270f,(width_Canvas) / 2,(height_Canvas) / 2);
                canvas.drawBitmap(mCacheBitmap, matrix, new Paint());



                if (mFpsMeter != null) {
                    mFpsMeter.measure();
                    mFpsMeter.draw(canvas, 20, 30);
                }
                getHolder().unlockCanvasAndPost(canvas);
            }
        }
    }

答案 1 :(得分:0)

在Manifest.xml文件中,在<activity>

中添加代码
android:screenOrientation="portrait"

如果你想锁定肖像,

<manifest>

中添加代码
<uses-permission android:name="android.permission.WAKE_LOCK" />