不能在创建的onCreate相机preVIEW? [英] Cant create Camera preview in onCreate?
问题描述
我在与我的Android相机preVIEW一些问题。
现在,我中有你拿相机的任何preVIEW之前,你需要preSS的按钮。
但我还想尽快启动应用程序有一个preVIEW。
如果我试图采取启动preVIEW了按钮的一部分,并把它放入的onCreate,它不会工作,在preVIEW无法启动。
如何创建一个preVIEW无需触摸一个按钮,用户?
另外,我有一个HTC的Desire HD,和preVIEW被转90度我。
这究竟是为什么?
照相机凸轮;
SurfaceView冲浪;
SurfaceHolder持有人;/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); 按钮btnStart =(按钮)findViewById(R.id.startCam preV); 。getWindow()和setFormat(PixelFormat.UNKNOWN); 冲浪=(SurfaceView)findViewById(R.id.surfaceView); 支架= surf.getHolder();
holder.addCallback(本);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); btnStart.setOnClickListener(新Button.OnClickListener(){ 公共无效的onClick(查看为arg0){
凸轮= Camera.open();
如果(凸轮!= NULL)
{
尝试
{
cam.set previewDisplay(支架);
cam.start preVIEW();
}
赶上(IOException异常E)
{
}
}
}
});
}
清单文件:
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=LSAB.CamTest
安卓版code =1
机器人:=的versionName1.0>
<应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME>
<活动机器人:名字=。CameraTest
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/>
<类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT; < /用途>
<采用-SDK安卓的minSdkVersion =4/> <使用许可权的android:NAME =android.permission.CAMERA>< /使用许可权>
< /清单>
main.xml中:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
<的TextView
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=世界您好,CameraTest
/>
<按钮
机器人:ID =@ + ID / startCam preV
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文本= - 开始preVIEW -
/>
< SurfaceView
机器人:ID =@ + ID / surfaceView
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
/>
< / LinearLayout中>
在除了添加集previewDisplay
到的onCreate
尝试做 - >
实施 SurfaceHolder.Callback
在 onSurfaceChanged
方法重置集previewDisplay
与传递给方法的新surfaceHolder。
这对我的作品。
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>
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..
这篇关于不能在创建的onCreate相机preVIEW?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!