0

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

在此处输入图像描述

所以不管我拿着手机的方向是正确的,我都会得到 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>
4

2 回答 2

1

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

 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);
            }
        }
    }
于 2013-10-05T20:05:45.220 回答
0

在 Manifest.xml 文件中,添加代码<activity>

android:screenOrientation="portrait"

如果你想锁定肖像,

在里面添加代码<manifest>

<uses-permission android:name="android.permission.WAKE_LOCK" />
于 2015-01-10T02:13:41.117 回答