【问题标题】:android camera preview blank screenandroid相机预览空白屏幕
【发布时间】:2012-12-13 20:12:28
【问题描述】:

我想手动拍摄照片,而不是使用现有的相机应用。所以我做了这个Activity

  public class CameraActivity extends Activity {

    private Camera mCamera;
    private Preview mPreview;

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


         // Create an instance of Camera
        mCamera = Camera.open(0); if(mCamera !=null) Log.i("CameraActivity.onCreate()", "mCamera initialized");

        // Create our Preview view and set it as the content of our activity.
        mPreview = new Preview(this); if(mPreview !=null) Log.i("CameraActivity.onCreate()", "mPreview initialized");

        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);

        preview.addView(mPreview); Log.i("CameraActivity.onCreate()", "mPreview added to FrameLayout");

        mPreview.setCamera(mCamera); Log.i("CameraActivity.onCreate()", "mPreview.setCamera(mCamera)");

    }

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

    @Override
    public void onPause() {
        super.onPause();
        mPreview.stopPreviewAndFreeCamera();
    }

}

这是我要使用的 CameraPreview:

    public class Preview extends ViewGroup implements SurfaceHolder.Callback {

    SurfaceView mSurfaceView;
    SurfaceHolder mHolder;

    private Camera mCamera;
    List<Size> mSupportedPreviewSizes;

    /**
     * @param context
     */
    public Preview(Context context) {
        super(context);

        mSurfaceView = new SurfaceView(context); Log.i("CameraPreview.constructor(...)", "mSurfaceView initialized");
        if(mSurfaceView==null) {Log.i("CameraPreview.constructor(...)", "mSurfaceView is null");}
        addView(mSurfaceView);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = mSurfaceView.getHolder(); Log.i("CameraPreview.constructor(...)", "mHolder setup");
        if(mHolder==null) {Log.e("PreviewCamera", "mHolder is null");}

        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    /**
     * @param context
     * @param attrs
     */
    public Preview(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public Preview(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    /* (non-Javadoc)
     * @see android.view.ViewGroup#onLayout(boolean, int, int, int, int)
     */
    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(width, height);
        requestLayout();
        mCamera.setParameters(parameters);

        /*
          Important: Call startPreview() to start updating the preview surface. Preview must be
          started before you can take a picture.
        */
        mCamera.startPreview(); Log.i("CameraPreview.surfaceChanged(...)", "mCamera.startPreview()");

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        if (mCamera != null) {
            /*
              Call stopPreview() to stop updating the preview surface.
            */
            mCamera.stopPreview(); Log.i("CameraPreview.surfaceDestroyed(...)", "mCamera.stopPreview()");
        }

    }



    /**
      * When this function returns, mCamera will be null.
      */
    public void stopPreviewAndFreeCamera() {

        if (mCamera != null) {
            /*
              Call stopPreview() to stop updating the preview surface.
            */
            mCamera.stopPreview(); Log.i("CameraPreview.stopPreviewAndFreeCamera()", "mCamera.stopPreview()");

            /*
              Important: Call release() to release the camera for use by other applications. 
              Applications should release the camera immediately in onPause() (and re-open() it in
              onResume()).
            */
            mCamera.release(); Log.i("CameraPreview.stopPreviewAndFreeCamera()", "mCamera.release()");

            mCamera = null;
        }
    }


    /***
     * 
     * @param camera
     */
    public void setCamera(Camera camera) {
        if (mCamera == camera) { Log.i("CameraPreview.setCamera()", "mCamera equals the Camera you want to set"); return; }

        stopPreviewAndFreeCamera(); Log.i("CameraPreview.setCamera()", "stopPreviewAndFreeCamera()");

        mCamera = camera; Log.i("CameraPreview.setCamera()", "setup  new Camera");

        if (mCamera != null) {
            List<Size> localSizes = mCamera.getParameters().getSupportedPreviewSizes();
            mSupportedPreviewSizes = localSizes;
            requestLayout();

            try {
                mCamera.setPreviewDisplay(mHolder);
                Log.i("CameraPreview.setCamera()", "mCamera.setPreviewDisplay(mHolder)");
            } catch (IOException e) {
                e.printStackTrace();
            }

            /*
              Important: Call startPreview() to start updating the preview surface. Preview must 
              be started before you can take a picture.
              */
            mCamera.startPreview(); Log.i("CameraPreview.setCamera()", "mCamera.startPreview()");
        }
    }


    /***
     * 
     * @param id
     * @return
     */
    public boolean safeCameraOpen(int id) {
        boolean qOpened = false;

        try {
            releaseCameraAndPreview();
            mCamera = Camera.open(id);

            qOpened = (mCamera != null);
        } catch (Exception e) {
          //  Log.e(R.string.app_name, "failed to open Camera");
            e.printStackTrace();
        }

        return qOpened;    
    }

    /**
     * 
     */
    Camera.PictureCallback mPicCallback = new Camera.PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            // TODO Auto-generated method stub
            Log.d("lala", "pic is taken");
        }
    };

    /**
     * 
     */
    public void takePic() {

    //  mCamera.takePicture(null, mPicCallback, mPicCallback);

    }
    /**
     * 
     */
    private void releaseCameraAndPreview() {
        this.setCamera(null);
        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }
    }

}

这是CameraActivity的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CameraActivity" >


  <FrameLayout
    android:id="@+id/camera_preview"
    android:layout_width="0dip"
    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>

但是当我启动 CameraActivity 时,我只看到一个空白的白色背景和 Capture-Button ???为什么我看不到相机屏幕?

编辑:Logcat:

12-16 13:09:39.941: I/CameraActivity.onCreate()(439): mCamera initialized
12-16 13:09:39.941: I/CameraPreview.constructor(...)(439): mSurfaceView initialized
12-16 13:09:39.941: I/CameraPreview.constructor(...)(439): mHolder setup
12-16 13:09:39.941: I/CameraActivity.onCreate()(439): mPreview initialized
12-16 13:09:39.952: I/CameraActivity.onCreate()(439): mPreview added to FrameLayout
12-16 13:09:39.952: I/CameraPreview.setCamera()(439): stopPreviewAndFreeCamera()
12-16 13:09:39.952: I/CameraPreview.setCamera()(439): setup  new Camera
12-16 13:09:39.961: D/Camera(439): app passed NULL surface
12-16 13:09:39.961: I/CameraPreview.setCamera()(439): mCamera.setPreviewDisplay(mHolder)
12-16 13:09:39.971: I/CameraPreview.setCamera()(439): mCamera.startPreview()
12-16 13:09:39.971: I/CameraActivity.onCreate()(439): mPreview.setCamera(mCamera)
12-16 13:09:40.622: I/ActivityManager(60): Displayed com.example.popup/.CameraActivity: +810ms

【问题讨论】:

    标签: android camera android-camera


    【解决方案1】:

    我认为你应该在addView() 之后调用open() 方法。
    相机预览大小由addView() 设置。

    【讨论】:

    • 你的意思是 camera.open()?我已经在方法 safeCameraOpen() 中使用了它
    • 现在我使用 setCamera() 方法编辑了我的 CameraActivity
    【解决方案2】:

    您没有使用相机调用 setCamera(仅 null),因此您从未设置预览显示。

    【讨论】:

    • 是的,但我使用方法 safeCameraOpen()。但是我在主主题中编辑了我的活动
    • 啊,但是 safeCameraOpen 不调用 mCamera.setPreviewDisplay(mHolder) 并且你已经设置了 mCamera setCamera() 将返回而不做任何事情。确保调用 mCamera.setPreviewDisplay(mHolder); android.util.Log 是你的朋友
    • 好吧,你是对的。当我调用 preview.setCamera(cam) 时得到消息:应用程序通过了 NULL 表面。但是我在那之前设置了表面和支架,所以我不知道为什么
    【解决方案3】:

    我遇到了同样的问题。然后我将它扩展到 SurfaceView 而不是 ViewGroup。在一个神奇的时刻,它起作用了......

    【讨论】:

      【解决方案4】:

      您可以检查预览的layout_width和layout_height属性是否设置为match_parent。

      【讨论】:

        【解决方案5】:

        看起来 OP 遵循了来自 http://developer.android.com/training/camera/cameradirect.html 的“控制相机”教程。我尝试了同样的方法,但很快发现本教程不起作用。

        为了避免其他初学者在尝试使教程正常工作时遇到同样的挫败感,这里有另一个可以工作的教程:http://www.tutorialspoint.com/android/android_camera.htm

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-11-09
          • 1970-01-01
          • 2011-10-24
          • 1970-01-01
          • 1970-01-01
          • 2021-08-26
          • 1970-01-01
          相关资源
          最近更新 更多