无法在onCreate中创建相机预览?

发布于 2024-11-13 06:28:39 字数 2934 浏览 4 评论 0原文

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

I'm having some issues with the preview of the camera on my Android.

Right now i have a button you need to press before you get any preview of the camera.
but id like to have a preview as soon as you start the app.

If i try to take the part that starts the preview out of the button and put it into the onCreate, it wont work, the preview wont start.

How can i create a preview without user having to touch a button?

Also, i have a HTC Desire HD, and the preview is turn 90 degrees for me.
Why is this happening?

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)
                {
                }
            }
        }
    });
}

Manifest file:

     <?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的快感 2024-11-20 06:28:39

除了将 setPreviewDisplay 添加到 onCreate 之外,尝试执行 ->

实现 SurfaceHolder.Callback

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

这对我有用..

In addition to adding the setPreviewDisplay to the onCreate try doing ->

Implement the SurfaceHolder.Callback

In onSurfaceChanged method reset the setPreviewDisplay with the new surfaceHolder that is passed to that method.

This works for me..

ぇ气 2024-11-20 06:28:39

您检查过清单中的权限吗?另外,尽量不要在 OnCreate 中做太多事情,这是一个不好的做法,如果您需要帮助,请在上帝的爱下对代码进行评论。

好的,尝试更改布局的方向并告诉我会发生什么。

Have you checked the permissions in the manifest? Also try not to do that much in the OnCreate, it's a bad practice and please, for the love of god, comment the code if you want help.

Ok, try changing the orientation of the layout and tell me what happens.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文