0

两天来,我一直试图在视图中显示相机。我遵循了很多这样的教程:

http://code.google.com/p/openmobster/wiki/CameraTutorial http://developer.android.com/training/camera/cameradirect.html

但没有成功在屏幕上显示相机。

我终于找到了这个直接有效的方法。只需删除一种已弃用的方法。从那个我尝试构建与前两个站点中的示例类似的东西,但我无法让它工作。

在这一行:

surfaceView = (PreView)findViewById(R.id.camerapreview);

我试图通过以下方式修改它:

surfaceView = new PreView(this);
      FrameLayout layout = (FrameLayout)findViewById(R.id.camerapreview); 
      layout.addView(surfaceView);

但它不起作用。然后我也改变SurfaceViewLinearLayout. activity_main.xml我没工作。的构造函数PReView甚至都没有运行(我在里面放了一个断点,但是这个bug就在那之前)。我是否需要嵌套布局才能使其正常工作?我尝试了很多东西。我希望有人会看看:

MainActivity.java:

package com.example.cameratest2;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.os.Bundle;

public class MainActivity extends Activity{
PreView surfaceView;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

      getWindow().setFormat(PixelFormat.UNKNOWN);
      surfaceView = (PreView)findViewById(R.id.camerapreview);
      //surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  }
}

预览.java:

package com.example.cameratest2;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class PreView extends SurfaceView implements SurfaceHolder.Callback {
Camera camera;
boolean previewing = false;
SurfaceHolder surfaceHolder;

public PreView(Context context) {
    super(context);

    surfaceHolder = this.getHolder();
    surfaceHolder.addCallback(this);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, 
    int width,int height)   {
    if(previewing){
     camera.stopPreview();
     previewing = false;
    }

    if (camera != null){
     try {
      camera.setPreviewDisplay(surfaceHolder);
      camera.startPreview();
      previewing = true;
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
    camera = null;
    previewing = false;
}
  }

activity_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"
 >
 <SurfaceView
 android:id="@+id/camerapreview"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 />
 </LinearLayout>

日志猫:

    11-09 04:22:58.856: W/dalvikvm(1612): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
    11-09 04:22:58.975: E/AndroidRuntime(1612): FATAL EXCEPTION: main
    11-09 04:22:58.975: E/AndroidRuntime(1612): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cameratest2/com.example.cameratest2.MainActivity}: java.lang.ClassCastException: android.view.SurfaceView cannot be cast to com.example.cameratest2.PreView
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.os.Handler.dispatchMessage(Handler.java:99)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.os.Looper.loop(Looper.java:137)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread.main(ActivityThread.java:4745)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at java.lang.reflect.Method.invokeNative(Native Method)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at java.lang.reflect.Method.invoke(Method.java:511)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at dalvik.system.NativeStart.main(Native Method)
    11-09 04:22:58.975: E/AndroidRuntime(1612): Caused by: java.lang.ClassCastException: android.view.SurfaceView cannot be cast to com.example.cameratest2.PreView
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at com.example.cameratest2.MainActivity.onCreate(MainActivity.java:19)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.Activity.performCreate(Activity.java:5008)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
    11-09 04:22:58.975: E/AndroidRuntime(1612):     ... 11 more

[编辑]

我发现了这个:从布局 xml 绘制 SurfaceView
如此链接中所述,我在activity_main.xml替换和修改 MainActivity.java 中进行了修改以<SurfaceView>替换<com.example.cameratest2.PreView>

 surfaceView = (PreView)findViewById(R.id.camerapreview);

 surfaceView = new PreView(this);

...但它仍然不起作用,我收到此错误:

11-09 05:01:14.114: E/AndroidRuntime(1700): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.example.cameratest2.PreView
4

0 回答 0