Android中的Camera Preview显示运行时错误

时间:2015-05-27 05:25:44

标签: android camera

这是我以前在我的应用中使用相机预览的代码,但它显示了java运行时错误,但没有指定错误所在的行。我发现https://github.com/commonsguy/cw-advandroid/tree/master/Camera/Preview/,which上的这个代码是作为有关相机预览的一个问题的解决方案,但它显示一个空白屏幕,应用程序终止。任何帮助将不胜感激。

package com.example.smartmeasure;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
public class CameraPreview extends Activity {
    private SurfaceView preview=null;
    private SurfaceHolder previewHolder=null;
    private Camera camera=null;
    private boolean inPreview=false;
    private boolean cameraConfigured=false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera_preview);
    preview=(SurfaceView)findViewById(R.id.camera_preview);
    previewHolder=preview.getHolder();
    previewHolder.addCallback(surfaceCallback);
    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
    @Override
    public void onResume() {
    super.onResume();
    camera=Camera.open();
    startPreview();
    }
    @Override
    public void onPause() {
    if (inPreview) {
    camera.stopPreview();
    }
    camera.release();
    camera=null;
    inPreview=false;
    super.onPause();
    }
    private Camera.Size getBestPreviewSize(int width, int height,
    Camera.Parameters parameters) {
    Camera.Size result=null;
    for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
    if (size.width<=width && size.height<=height) {
    if (result==null) {
    result=size;
    }
    else {
    int resultArea=result.width*result.height;
    int newArea=size.width*size.height;
    if (newArea>resultArea) {
    result=size;
    }
    }
    }
    }
    return(result);
    }
    private void initPreview(int width, int height) {
    if (camera!=null && previewHolder.getSurface()!=null) {
    try {
    camera.setPreviewDisplay(previewHolder);
    }
    catch (Throwable t) {
    Log.e("PreviewDemo-surfaceCallback",
    "Exception in setPreviewDisplay()", t);
    Toast
    .makeText(CameraPreview.this, t.getMessage(), Toast.LENGTH_LONG)
    .show();
    }
    if (!cameraConfigured) {
    Camera.Parameters parameters=camera.getParameters();
    Camera.Size size=getBestPreviewSize(width, height,
    parameters);
    if (size!=null) {
    parameters.setPreviewSize(size.width, size.height);
    camera.setParameters(parameters);
    cameraConfigured=true;
    }
    }
    }
    }
    private void startPreview() {
    if (cameraConfigured && camera!=null) {
    camera.startPreview();
    inPreview=true;
    }
    }
    SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() {
    public void surfaceCreated(SurfaceHolder holder) {
    // no-op -- wait until surfaceChanged()
    }
    public void surfaceChanged(SurfaceHolder holder,
    int format, int width,
    int height) {
    initPreview(width, height);
    startPreview();
    }
    public void surfaceDestroyed(SurfaceHolder holder) {
    // no-op
    }
    };
}

这是错误:

05-27 10:34:37.812: E/AndroidRuntime(2559): FATAL EXCEPTION: main 05-27 10:34:37.812: E/AndroidRuntime(2559): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.smartmeasure/com.example.smartmeasure.CameraPreview}: java.lang.ClassCastException: android.widget.FrameLayout 05-27 10:34:37.812: E/AndroidRuntime(2559): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 

这是xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <FrameLayout
        android:id="@+id/camera_preview"
        android:layout_width="match_parent"
        android:layout_height="241dp" />

</LinearLayout>

3 个答案:

答案 0 :(得分:0)

camera_previewFrameLayout,您将其投入SurfaceView错误,将其转换为FrameLayout

答案 1 :(得分:0)

您在Activity onCreate中将FrameLayout强制转换为SurfaceView,将SurfaceView而不是FrameLayout放入XML中,然后将其id命名为camera_preview并在Activity中初始化。

编辑后,您的XML代码将如下所示:

<强> XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

   <SurfaceView
    android:id="@+id/camera_preview"
    android:layout_width="match_parent"
    android:layout_height="241dp"/>

</LinearLayout>

答案 2 :(得分:0)

只是添加上面的建议,我们应该在AndroidManifest.xml文件中的相机用户权限来访问相机。权限行是:

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