启动活动时,Camera Preview崩溃

时间:2013-02-20 14:28:16

标签: android camera surfaceview

我试图在姜饼2.3.3手机上显示相机预览。但是每次活动开始时我的应用程序都会在设备上崩溃。

这是我的xml:

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" 
android:layout_marginLeft="10dp">

<ImageView
  android:id="@+id/loadbtn"
  android:src="@drawable/loadbtn"
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:layout_margin="10dp"
 />

<ImageView
  android:id="@+id/capturebtn"
  android:src="@drawable/capturebtn"
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:layout_margin="10dp"
 />

<ImageView
  android:id="@+id/editbtn"
  android:src="@drawable/editbtn"
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:layout_margin="10dp"
 />

<ImageView
  android:id="@+id/closebtn"
  android:src="@drawable/closebtn"
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:layout_margin="10dp"
  android:onClick="backToStart"
 />

</LinearLayout>

 <SurfaceView
  android:id="@+id/camerapreview"
  android:layout_width="300dp"
  android:layout_height="300dp"
  android:layout_alignParentBottom="true"
  android:layout_centerHorizontal="true"
  android:layout_marginBottom="10dp"
  />

我的代码:

public class PhotoHome extends Activity implements SurfaceHolder.Callback {

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photo_home);

    getWindow().setFormat(PixelFormat.UNKNOWN);
     surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
     surfaceHolder = surfaceView.getHolder();  
     surfaceHolder.addCallback(this);

}

public void backToStart(View v)
{
    finish();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
         int height) {
    // TODO Auto-generated method stub
    if(previewing){
         camera.stopPreview();
         previewing = false;
        }

        if (camera != null){
            Parameters parameters = camera.getParameters();
            Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

            if(display.getRotation() == Surface.ROTATION_0)
            {
                parameters.setPreviewSize(height, width);                           
                camera.setDisplayOrientation(90);
            }

            if(display.getRotation() == Surface.ROTATION_90)
            {
                parameters.setPreviewSize(width, height);                           
            }

            if(display.getRotation() == Surface.ROTATION_180)
            {
                parameters.setPreviewSize(height, width);               
            }

            if(display.getRotation() == Surface.ROTATION_270)
            {
                parameters.setPreviewSize(width, height);
                camera.setDisplayOrientation(180);
            }

            camera.setParameters(parameters);
            try 
            {           
                camera.setPreviewDisplay(surfaceHolder);          
                camera.startPreview();
                previewing = true;
            }
            catch(Exception e)
            {
                Log.d("Error", "Cannot start preview", e);    
            }
        }
}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
    // TODO Auto-generated method stub
    camera = Camera.open();
    try {
        camera.setPreviewDisplay(surfaceHolder);
        } catch (IOException e) {
            camera.release();
            camera = null;
        }


}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    // TODO Auto-generated method stub
     if (camera != null) {
         camera.stopPreview();
         camera.setPreviewCallback(null);
         camera.release();
         camera = null;
        }
}

这是我的权限:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

我试图在屏幕上的某个300 x 300区域显示相机预览。但正如我所说,应用程序在设备上崩溃,但似乎在模拟器中,由于300 x 300区域的白色背景,它正在工作。有什么想法吗?

更新:Logcat

    02-20 23:59:34.969: E/AndroidRuntime(15365): FATAL EXCEPTION: main
    02-20 23:59:34.969: E/AndroidRuntime(15365): java.lang.RuntimeException: setParameters failed
   02-20 23:59:34.969: E/AndroidRuntime(15365):     at android.hardware.Camera.native_setParameters(Native Method)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.hardware.Camera.setParameters(Camera.java:1070)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at com.example.photoexpress.PhotoHome.surfaceChanged(PhotoHome.java:77)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.view.SurfaceView.updateWindow(SurfaceView.java:558)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.view.SurfaceView.dispatchDraw(SurfaceView.java:350)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644) 
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.view.View.draw(View.java:6905)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.view.View.draw(View.java:6905)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.view.View.draw(View.java:6905)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1895)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.view.ViewRoot.draw(ViewRoot.java:1542)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1269)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1883)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.os.Handler.dispatchMessage(Handler.java:99)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.os.Looper.loop(Looper.java:130)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at android.app.ActivityThread.main(ActivityThread.java:3737)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at java.lang.reflect.Method.invokeNative(Native Method)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at java.lang.reflect.Method.invoke(Method.java:507)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
    02-20 23:59:34.969: E/AndroidRuntime(15365):    at dalvik.system.NativeStart.main(Native Method)
    02-20 23:59:34.979: E/AndroidRuntime(15365): [Blue Error Handler] Make Debugging Report file for main
    02-20 23:59:34.979: E/AndroidRuntime(15365): java.lang.RuntimeException: setParameters failed
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.hardware.Camera.native_setParameters(Native Method)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.hardware.Camera.setParameters(Camera.java:1070)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at com.example.photoexpress.PhotoHome.surfaceChanged(PhotoHome.java:77)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.view.SurfaceView.updateWindow(SurfaceView.java:558)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.view.SurfaceView.dispatchDraw(SurfaceView.java:350)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.view.View.draw(View.java:6905)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.view.View.draw(View.java:6905)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.view.View.draw(View.java:6905)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1895)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.view.ViewRoot.draw(ViewRoot.java:1542)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1269)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1883)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.os.Handler.dispatchMessage(Handler.java:99)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.os.Looper.loop(Looper.java:130)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at android.app.ActivityThread.main(ActivityThread.java:3737)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at java.lang.reflect.Method.invokeNative(Native Method)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at java.lang.reflect.Method.invoke(Method.java:507)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
    02-20 23:59:34.979: E/AndroidRuntime(15365):    at dalvik.system.NativeStart.main(Native Method)

在我的Logcat上,据说我在此行设置参数失败。 camera.setParameters(参数);我已经使用此代码将相机预览设置为游戏的背景。但我真的不知道发生了什么,现在它不会起作用。 :(

1 个答案:

答案 0 :(得分:1)

它会出错,因为我认为您试图设置无效的预览尺寸。

见Roman Nurik回答here