3

Android のカメラのプレビューに問題があります。

現在、カメラのプレビューを取得する前に押す必要があるボタンがあります。アプリを起動するとすぐにプレビューが表示されます。

プレビューを開始する部分をボタンから取り出して onCreate に入れようとすると、機能せず、プレビューが開始されません。

ユーザーがボタンに触れずにプレビューを作成するにはどうすればよいですか?

また、私は HTC Desire HD を持っていますが、プレビューは 90 度回転しています。なぜこうなった?

Camera cam;
SurfaceView surf;
SurfaceHolder holder;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button btnStart= (Button)findViewById(R.id.startCamPrev);

    getWindow().setFormat(PixelFormat.UNKNOWN);

    surf = (SurfaceView)findViewById(R.id.surfaceView);

    holder = surf.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    btnStart.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View arg0) {
            cam = Camera.open();
            if(cam != null)
            {
                try
                {
                    cam.setPreviewDisplay(holder);
                    cam.startPreview();
                }
                catch(IOException e)
                {
                }
            }
        }
    });
}

マニフェスト ファイル:

     <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
         package="LSAB.CamTest"
         android:versionCode="1"
         android:versionName="1.0">
       <application android:icon="@drawable/icon" android:label="@string/app_name">
           <activity android:name=".CameraTest"
                     android:label="@string/app_name">
               <intent-filter>
                   <action android:name="android.intent.action.MAIN" />
                   <category android:name="android.intent.category.LAUNCHER" />
               </intent-filter>
           </activity>

       </application>
       <uses-sdk android:minSdkVersion="4" />

    <uses-permission android:name="android.permission.CAMERA"></uses-permission>
    </manifest>

Main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Hello World, CameraTest"
    />
<Button
    android:id = "@+id/startCamPrev"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="- Start Preview -"
    />
 <SurfaceView
     android:id="@+id/surfaceView"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     />
</LinearLayout>
4

2 に答える 2

2

setPreviewDisplayやってみることに加えonCreateて ->

実装するSurfaceHolder.Callback

メソッドで、そのメソッドに渡された新しい surfaceHolder を使用して をonSurfaceChangedリセットします。setPreviewDisplay

これは私のために働く..

于 2011-06-14T12:04:55.457 に答える
0

マニフェストの権限を確認しましたか? また、OnCreate ではあまり多くのことを行わないようにしてください。これは悪い習慣です。助けが必要な場合は、コードにコメントしてください。

わかりました。レイアウトの向きを変えてみて、どうなるか教えてください。

于 2011-06-04T21:28:27.197 に答える