【问题标题】:Android - camera preview not workingAndroid - 相机预览不起作用
【发布时间】:2013-03-23 04:21:12
【问题描述】:

我正在尝试为 Android 编写相机应用程序,但无法让预览正常工作。我尝试按照 android 提供的步骤 here,但它不起作用,应用程序不断崩溃。我不确定我做错了什么,但我很确定我按照正确的步骤操作。

这是我目前所拥有的: 相机活动类

public class CameraActivity extends Activity
{
    private CameraPreview mPreview = null;
    private Camera mCamera = null;


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

        mCamera = getCameraInstance();

        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);

    }
    public static Camera getCameraInstance()
    {
        Camera c = null;

        try
        {
            //attempt to get a Camera instance
            c = Camera.open();
        }
        catch(Exception e)
        {
            //Camera is not available(in use or does not exist)
        }
        return c;//returns null if camera is unavailable
    }


    public void onPause()
    {
        super.onPause();
        mCamera.stopPreview();
        releaseCamera();
    }


    private void releaseCamera()
    {
        if(mCamera != null)
        {
            mCamera.release();
            mCamera = null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.camera, menu);
        return true;
    }
}

相机预览类

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
{
    private final String TAG = "CameraPreview";
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera)
    {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        //deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder)
    {
        //the Surface has been created, now tell the camera where to draw the preview
            try
            {
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();
            }
            catch(IOException e)
            {
                Log.d(TAG, "Error setting camera preview: " + e.getMessage());  
            }
    }

    public void surfaceDestroyed(SurfaceHolder holder)
    {
        //empty. take care of releasing the Camera preview in your activity
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
    {
        //if your preview can change or rotate, take care of those events here.
        //make sure to stop the preview before resizing of reformatting it.
        if(mHolder.getSurface() == null)
        {
            //preview surface does not exist
            return;
        }

        //stop preview before making changes
        try
        {
            mCamera.stopPreview();
        }
        catch(Exception e)
        {
            //ignore: tried to stop a non-existent preview
        }

        //set preview size and make any resize, rotate or
        //reformatting changes here
        try
        {
            //start preview with new settings
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
        }
        catch(Exception e)
        {
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
}

这是我用于相机活动的 .xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <FrameLayout
        android:id="@+id/camera_preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        />


    <Button
        android:id="@+id/button_capture"
        android:text="Capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />
</LinearLayout> 

据我所知,我正确地遵循了指南,它应该可以工作。由于下一部分与录制有关,我认为在此之前的所有步骤都应该产生一个工作预览,但它不起作用。任何人都可以看到我需要更改的错误吗? 谢谢

编辑:添加了我的清单 清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ics466.project.warpwalker"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.ics466.project.warpwalker.WarpIntro"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
            </activity>
            <activity
            android:name="com.ics466.project.warpwalker.CameraActivity"
            android:label="@string/title_activity_camera" >
        </activity>
    </application>

</manifest>

【问题讨论】:

标签: android camera preview


【解决方案1】:

android:minSdkVersion="8" 应该是android:minSdkVersion="9"

如果您使用了 API 9 中添加的一些功能

并阅读此链接How To Enable Camera in Android Emulator

或者在有摄像头的真机上测试这个应用。

【讨论】:

  • 好的,所以在将最低版本更改为 9 后它已经停止崩溃,但现在屏幕只是黑屏。我有我正在测试它以模拟相机的虚拟设备,所以我认为它不应该是黑色的......?
  • 如果你在模拟器中运行它......相机和模拟器中的预览在哪里,所以它不应该是黑色的??
  • 整个屏幕是黑色的还是只是camera_preview framelayout是黑色的??
  • minSdkVersion 必须为 9 的原因是什么?
  • 只是预览应该在的框架布局。当我运行模拟器上已经存在的相机应用程序时,屏幕上会出现一个框。我认为这就是应该发生的事情。
【解决方案2】:

在大多数设备上,相机预览的默认方向是横向。

尝试将android:orientation="horizontal" 添加到您的 .xml 文件的 LinearLayout 中。

【讨论】:

    【解决方案3】:

    试试这个
    将 CameraId 设为整数
    Camera.open(0);
    http://developer.android.com/reference/android/hardware/Camera.html#open(int)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 2017-09-22
      • 1970-01-01
      • 1970-01-01
      • 2017-01-25
      • 2018-02-19
      • 1970-01-01
      相关资源
      最近更新 更多