1

我正在尝试在姜饼 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(参数); 我已经使用此代码将相机预览设置为游戏的背景。但我真的不知道发生了什么,现在它不起作用。:(

4

1 回答 1

1

它给出了错误,因为我认为您正在尝试设置无效的预览大小。

在此处查看 Roman Nurik 的答案

于 2013-02-20T17:20:32.047 回答