【问题标题】:Cant create Camera preview in onCreate?无法在 onCreate 中创建相机预览?
【发布时间】:2011-06-04 21:13:21
【问题描述】:

我在 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>

【问题讨论】:

    标签: android camera android-camera oncreate


    【解决方案1】:

    除了将setPreviewDisplay 添加到onCreate 之外尝试做->

    实现SurfaceHolder.Callback

    onSurfaceChanged 方法中,使用传递给该方法的新surfaceHolder 重置setPreviewDisplay

    这对我有用..

    【讨论】:

      【解决方案2】:

      您是否检查了清单中的权限?也尽量不要在 OnCreate 中做太多,这是一种不好的做法,看在上帝的份上,如果您需要帮助,请评论代码。

      好的,试着改变布局的方向,告诉我会发生什么。

      【讨论】:

      • 对此很抱歉,主要是在做一些测试 atm,所以我想我对 cme​​ts 很懒。
      • 我坚持,请发布清单和 main.xml ,也许这就是问题所在,因为您的方向问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多