gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:40:38 25 4
gpt4 key购买 nike

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

list 文件:

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

主.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>

最佳答案

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

实现SurfaceHolder.Callback

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

这对我有用..

关于android - 无法在 onCreate 中创建相机预览?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6239440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com