无法在onCreate中创建相机预览?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了将
setPreviewDisplay
添加到onCreate
之外,尝试执行 ->实现 SurfaceHolder.Callback
在
onSurfaceChanged
方法中使用传递给该方法的新 SurfaceHolder 重置setPreviewDisplay
。这对我有用..
In addition to adding the
setPreviewDisplay
to theonCreate
try doing ->Implement the
SurfaceHolder.Callback
In
onSurfaceChanged
method reset thesetPreviewDisplay
with the new surfaceHolder that is passed to that method.This works for me..
您检查过清单中的权限吗?另外,尽量不要在 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.