我已经看到了类似的问题,但我仍然无法找到解决方案。我对Android开发很新,所以我无法追踪我的问题。无论如何,我正在尝试使用我创建的CameraSurfaceView类来创建相机预览,该类扩展了SurfaceView并实现了SurfaceHolder.Callback。无论我在StartCamera类中尝试什么,都不会调用surfaceCreated()方法,因此我的相机永远不会启动。任何帮助都会很棒,谢谢!
StartCamera.java
import net.peterd.zombierun.R;
import android.hardware.Camera;
import android.os.Bundle;
import android.widget.FrameLayout;
public class StartCamera extends BaseActivity {
private Camera mCamera;
private CameraSurfaceView mView;
public void onCreate(Bundle state) {
// TODO Auto-generated constructor stub
super.onCreate(state);
setContentView(R.layout.start_camera);
}
public void onResume() {
super.onResume();
mView = new CameraSurfaceView(this);
FrameLayout preview = (FrameLayout) findViewById(R.id.cPreview);
preview.addView(mView);
}
public void onPause() {
mCamera.stopPreview();
mCamera.release();
}
}
CameraSurfaceView.java
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback
{
private SurfaceHolder holder;
private Camera camera;
public CameraSurfaceView(Context context)
{
super(context);
//Initiate the Surface Holder properly
this.holder = this.getHolder();
this.holder.addCallback(this);
this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
try
{
//Open the Camera in preview mode
this.camera = Camera.open();
this.camera.setPreviewDisplay(this.holder);
}
catch(IOException ioe)
{
ioe.printStackTrace(System.out);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(width, height);
camera.setParameters(parameters);
camera.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
// Surface will be destroyed when replaced with a new screen
//Always make sure to release the Camera instance
camera.stopPreview();
camera.release();
camera = null;
}
public Camera getCamera() {
return camera;
}
}
start_camera.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">
<FrameLayout android:id="@+id/cPreview"
android:layout_weight="1" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
</LinearLayout>
答案 0 :(得分:6)
显然这是因为FrameLayout是空的,因此没有创建任何类型的SurfaceView。您可以尝试手动强制父LinearLayout设置高度和宽度,或者只是在FrameLayout中添加imageView。由于FrameLayout可能包含许多小部件,但它只显示最后一个小部件,您可以在FrameLayout中放置一个ImageView,仅显示要创建的SurfaceView的小图像。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout android:id="@+id/cPreview"
android:layout_weight="1" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Adding small dummy image -->
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
src="@drawable/small_picture">
</FrameLayout>
</LinearLayout>
答案 1 :(得分:5)
我遇到了同样的问题。对我来说,这是因为视图大小设置为0,因此从未创建表面。我通过设置一个特定的大小来检查这一点,并且突然调用了回调。 (如果View在屏幕外布局,显然也是如此。)
答案 2 :(得分:2)
@jvnane我尝试了你的代码,它有效,但有一点小错误。
surfaceChanged
被称为没有问题。你认为它以什么方式不起作用?尝试添加调试消息并在LogCat中查看:
@Override
public void surfaceCreated(final SurfaceHolder holder)
{
Log.i("Camera Test", "It works!");
try
{
//Open the Camera in preview mode
this.camera = Camera.open();
this.camera.setPreviewDisplay(this.holder);
}
catch(final IOException ioe)
{
ioe.printStackTrace(System.out);
}
}
小错误:Camera
对象已在surfaceDestroyed
中发布,因此您无需在活动onPause
中再次发布该对象。此外,您需要在覆盖super.onPause()
方法时调用onPause()
。
答案 3 :(得分:0)
我在CameraSurfaceView
方法surfaceChanged
方法中遇到此行的错误:
camera.setParameters(parameters);
一旦我评论出来,它工作得很好。