两天来,我一直试图在视野中显示相机。我遵循了很多这样的教程:
http://code.google.com/p/openmobster/wiki/CameraTutorial http://developer.android.com/training/camera/cameradirect.html
但没有成功在屏幕上显示相机。
我终于找到了直接工作的this。只需删除一个已弃用的方法。 从那个开始,我尝试在两个第一个站点中构建类似于示例的东西,但是我无法使它工作。
在这一行:
surfaceView = (PreView)findViewById(R.id.camerapreview);
我试图通过以下方式修改它:
surfaceView = new PreView(this);
FrameLayout layout = (FrameLayout)findViewById(R.id.camerapreview);
layout.addView(surfaceView);
但它不起作用。然后我还更改了SurfaceView
中的LinearLayout
activity_main.xml
。我没工作。 PReView
的构造函数甚至没有运行(我在其中放了一个断点,但错误就在此之前)。
我是否需要使用嵌套布局才能使其正常工作?我尝试了很多东西。我希望有人看看:
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);
}
}
Preview.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>
logCat:
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
[编辑]
我发现了这个:Draw SurfaceView from layout xml。
如本链接所述,我在activity_main.xml
用<SurfaceView>
替换<com.example.cameratest2.PreView>
并修改MainActivity.java以进行修改
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